internal async static void DoRest(String url, String body, OnResponseDelegate callback) { try { HttpContent content = new StringContent(body); HttpClientHandler aHandler = new HttpClientHandler(); aHandler.ClientCertificateOptions = ClientCertificateOption.Automatic; HttpClient aClient = new HttpClient(aHandler); Uri requestUri = new Uri(url); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, requestUri); var result = await aClient.PostAsync(requestUri, content); var responseHeader = result.Headers; var responseBody = await result.Content.ReadAsStringAsync(); if (result.IsSuccessStatusCode) { callback(null, responseBody); } else { callback(new OrtcPresenceException(responseBody), null); } } catch (Exception e) { callback(new OrtcPresenceException(e.Message), null); } }
private void CheckOnResponseDelegate(OnResponseDelegate del) { if (del == null) { throw new InvalidOperationException(); } }
internal async static void DoRestGet(String url, OnResponseDelegate callback) { OrtcPresenceException cbError = null; String cbSuccess = null; try { HttpClientHandler aHandler = new HttpClientHandler(); aHandler.ClientCertificateOptions = ClientCertificateOption.Automatic; HttpClient aClient = new HttpClient(aHandler); Uri requestUri = new Uri(url); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUri); var result = await aClient.GetAsync(requestUri, HttpCompletionOption.ResponseContentRead); var responseHeader = result.Headers; var responseBody = await result.Content.ReadAsStringAsync(); if (result.IsSuccessStatusCode) { cbSuccess = responseBody; } else { cbError = new OrtcPresenceException(responseBody); } } catch (Exception e) { cbError = new OrtcPresenceException(e.Message); } callback(cbError, cbSuccess); }
public ResourceResponse Request(Uri uri, string accept, bool prioritized, OnResponseDelegate onResponse, OnErrorDelegate onError) { var request = new ResourceResponse(uri, accept, onResponse, onError); Request(request, prioritized); return(request); }
/// <summary> /// Attaches an action that is invoked on error /// </summary> /// <param name="request"></param> /// <param name="onResponse"></param> /// <returns></returns> public static Request OnError(this Request request, OnResponseDelegate onResponse) { // Tie into OnError request.onError += req => { onResponse((int)req.unityWebRequest.responseCode, req.unityWebRequest.error); }; return(request); }
public static Request OnResponse(this Request request, OnResponseDelegate onResponse) { request.unityWebRequest.downloadHandler = new DownloadHandlerBuffer(); // Tie into onComplete request.onComplete += req => { onResponse((int)req.unityWebRequest.responseCode, req.unityWebRequest.downloadHandler.text); }; return(request); }
void HandleResponse(HTTPResponse response, OnResponseDelegate callback) { //如果请求错误 HTTPResponse对象会返回null if (callback != null) { if (response != null) { callback(response.Data, response.StatusCode, response.IsSuccess); } else { callback(null, -1, false); } } }
public override void Get(string url, OnResponseDelegate callback) { HTTPRequest request = new HTTPRequest(new Uri(url), delegate(HTTPRequest originalRequest, HTTPResponse response) { HandleResponse(response, callback); }); if (connectTimeout >= 0) { request.ConnectTimeout = TimeSpan.FromSeconds(connectTimeout); } if (requestTimeout >= 0) { request.Timeout = TimeSpan.FromSeconds(requestTimeout); } AddHeads(request); request.Send(); }
public void SetOnNoOperationResponse(OnResponseDelegate onNoOperationResponse) { this.onNoOperationResponse = onNoOperationResponse ?? throw new ArgumentNullException(nameof(onNoOperationResponse)); }
public abstract void Get(string url, OnResponseDelegate callback);
public override void Post(string url, Dictionary <string, string> requestParams, OnResponseDelegate callback) { HTTPRequest request = new HTTPRequest(new Uri(url), HTTPMethods.Post, delegate(HTTPRequest originalRequest, HTTPResponse response) { HandleResponse(response, callback); }); AddParams(request, requestParams); AddHeads(request); request.Send(); }
internal static void PostAsync(String url, String content, OnResponseDelegate callback) { RestWebservice.RequestAsync(url, "POST", content, callback); }
private static void RequestAsync(String url, String method,String content, OnResponseDelegate callback) { var request = (HttpWebRequest)WebRequest.Create(new Uri(url)); ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; request.Proxy = null; request.Timeout = 10000; request.ProtocolVersion = HttpVersion.Version11; request.Method = method; if (String.Compare(method,"POST") == 0 && !String.IsNullOrEmpty(content)) { byte[] postBytes = Encoding.UTF8.GetBytes(content); request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = postBytes.Length; using (Stream requestStream = request.GetRequestStream()) { requestStream.Write(postBytes, 0, postBytes.Length); requestStream.Close(); } } request.BeginGetResponse(new AsyncCallback((asynchronousResult) => { var server = String.Empty; var synchContext = System.Threading.SynchronizationContext.Current; try { #if ENABLE_REST HttpWebRequest asyncRequest = (HttpWebRequest)asynchronousResult.AsyncState; HttpWebResponse response = (HttpWebResponse)asyncRequest.EndGetResponse(asynchronousResult); Stream streamResponse = response.GetResponseStream(); StreamReader streamReader = new StreamReader(streamResponse); var responseBody = streamReader.ReadToEnd(); if (callback != null) { if (!String.IsNullOrEmpty(HttpRuntime.AppDomainAppVirtualPath)) { callback(null, responseBody); } else { if (synchContext != null) { synchContext.Post(obj => callback(null, responseBody), null); } else { ThreadPool.QueueUserWorkItem((o) => callback(null, responseBody)); } } } #endif } catch (WebException wex) { #if ENABLE_REST String errorMessage = String.Empty; if (wex.Response == null) { errorMessage = "Uknown request error"; if (!String.IsNullOrEmpty(HttpRuntime.AppDomainAppVirtualPath)) { callback(new OrtcPresenceException(errorMessage), null); } else { if (synchContext != null) { synchContext.Post(obj => callback(new OrtcPresenceException(errorMessage), null), null); } else { ThreadPool.QueueUserWorkItem((o) => callback(new OrtcPresenceException(errorMessage), null)); } } } else { using (var stream = wex.Response.GetResponseStream()) { using (var reader = new StreamReader(stream)) { errorMessage = reader.ReadToEnd(); } if (!String.IsNullOrEmpty(HttpRuntime.AppDomainAppVirtualPath)) { callback(new OrtcPresenceException(errorMessage), null); } else { if (synchContext != null) { synchContext.Post(obj => callback(new OrtcPresenceException(errorMessage), null), null); } else { ThreadPool.QueueUserWorkItem((o) => callback(new OrtcPresenceException(errorMessage), null)); } } } } #endif } catch (Exception ex) { #if ENABLE_REST if (!String.IsNullOrEmpty(HttpRuntime.AppDomainAppVirtualPath)) { callback(new OrtcPresenceException(ex.Message), null); } else { if (synchContext != null) { synchContext.Post(obj => callback(new OrtcPresenceException(ex.Message), null), null); } else { ThreadPool.QueueUserWorkItem((o) => callback(new OrtcPresenceException(ex.Message), null)); } } #endif } }), request); }
internal static void GetAsync(String url, OnResponseDelegate callback) { RestWebservice.RequestAsync(url, "GET",null, callback); }
public void SetOnIsFoxArmedResponse(OnResponseDelegate onIsFoxArmedResponse) { this.onIsFoxArmedResponse = onIsFoxArmedResponse ?? throw new ArgumentNullException(nameof(onIsFoxArmedResponse)); }
public void SetOnSetEndingToneDurationResponse(OnResponseDelegate onSetEndingToneDurationResponse) { this.onSetEndingToneDurationResponse = onSetEndingToneDurationResponse ?? throw new ArgumentNullException(nameof(onSetEndingToneDurationResponse)); }
public void SetOnSwitchToProfileResponse(OnResponseDelegate onSwitchToProfileResponse) { this.onSwitchToProfileResponse = onSwitchToProfileResponse ?? throw new ArgumentNullException(nameof(onSwitchToProfileResponse)); }
public void SetOnGetCurrentProfileIdResponse(OnResponseDelegate onGetCurrentProfileIdResponse) { this.onGetCurrentProfileIdResponse = onGetCurrentProfileIdResponse ?? throw new ArgumentNullException(nameof(onGetCurrentProfileIdResponse)); }
public void SetOnGetAntennaMatchingStatusResponse(OnResponseDelegate onGetAntennaMatchingStatusResponse) { _onGetAntennaMatchingStatusResponse = onGetAntennaMatchingStatusResponse ?? throw new ArgumentNullException(nameof(onGetAntennaMatchingStatusResponse)); }
public abstract void Post(string url, Dictionary <string, string> requestParams, OnResponseDelegate callback);
public void SetOnGetIdentificationDataResponse(OnResponseDelegate onGetIdentificationDataResponse) { this.onGetIdentificationDataResponse = onGetIdentificationDataResponse ?? throw new ArgumentNullException(nameof(onGetIdentificationDataResponse)); }
public void SetOnGetUBattADCResponse(OnResponseDelegate onGetUBattADCResponse) { _onGetUBattADCResponse = onGetUBattADCResponse ?? throw new ArgumentNullException(nameof(onGetUBattADCResponse)); }
public void SetOnDisarmFoxResponse(OnResponseDelegate onDisarmFoxResponse) { this.onDisarmFoxResponse = onDisarmFoxResponse ?? throw new ArgumentNullException(nameof(onDisarmFoxResponse)); }
internal static void PostAsync(String url,String content, OnResponseDelegate callback) { RestWebservice.RequestAsync(url, "POST",content, callback); }
public void SetOnSetBeginAndEndTimesResponse(OnResponseDelegate onSetBeginAndEndTimesResponse) { this.onSetBeginAndEndTimesResponse = onSetBeginAndEndTimesResponse ?? throw new ArgumentNullException(nameof(onSetBeginAndEndTimesResponse)); }
internal static void GetAsync(String url, OnResponseDelegate callback) { RestWebservice.RequestAsync(url, "GET", null, callback); }
public void SetOnSetCycleResponse(OnResponseDelegate onSetCycleResponse) { _onSetCycleResponse = onSetCycleResponse ?? throw new ArgumentNullException(nameof(onSetCycleResponse)); }
private static void RequestAsync(String url, String method, String content, OnResponseDelegate callback) { var request = (HttpWebRequest)WebRequest.Create(new Uri(url)); ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; request.Proxy = null; request.Timeout = 10000; request.ProtocolVersion = HttpVersion.Version11; request.Method = method; if (String.Compare(method, "POST") == 0 && !String.IsNullOrEmpty(content)) { byte[] postBytes = Encoding.UTF8.GetBytes(content); request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = postBytes.Length; using (Stream requestStream = request.GetRequestStream()) { requestStream.Write(postBytes, 0, postBytes.Length); requestStream.Close(); } } request.BeginGetResponse(new AsyncCallback((asynchronousResult) => { var server = String.Empty; var synchContext = System.Threading.SynchronizationContext.Current; try { #if ENABLE_REST HttpWebRequest asyncRequest = (HttpWebRequest)asynchronousResult.AsyncState; HttpWebResponse response = (HttpWebResponse)asyncRequest.EndGetResponse(asynchronousResult); Stream streamResponse = response.GetResponseStream(); StreamReader streamReader = new StreamReader(streamResponse); var responseBody = streamReader.ReadToEnd(); if (callback != null) { if (!String.IsNullOrEmpty(HttpRuntime.AppDomainAppVirtualPath)) { callback(null, responseBody); } else { if (synchContext != null) { synchContext.Post(obj => callback(null, responseBody), null); } else { ThreadPool.QueueUserWorkItem((o) => callback(null, responseBody)); } } } #endif } catch (WebException wex) { #if ENABLE_REST String errorMessage = String.Empty; if (wex.Response == null) { errorMessage = "Uknown request error"; if (!String.IsNullOrEmpty(HttpRuntime.AppDomainAppVirtualPath)) { callback(new OrtcPresenceException(errorMessage), null); } else { if (synchContext != null) { synchContext.Post(obj => callback(new OrtcPresenceException(errorMessage), null), null); } else { ThreadPool.QueueUserWorkItem((o) => callback(new OrtcPresenceException(errorMessage), null)); } } } else { using (var stream = wex.Response.GetResponseStream()) { using (var reader = new StreamReader(stream)) { errorMessage = reader.ReadToEnd(); } if (!String.IsNullOrEmpty(HttpRuntime.AppDomainAppVirtualPath)) { callback(new OrtcPresenceException(errorMessage), null); } else { if (synchContext != null) { synchContext.Post(obj => callback(new OrtcPresenceException(errorMessage), null), null); } else { ThreadPool.QueueUserWorkItem((o) => callback(new OrtcPresenceException(errorMessage), null)); } } } } #endif } catch (Exception ex) { #if ENABLE_REST if (!String.IsNullOrEmpty(HttpRuntime.AppDomainAppVirtualPath)) { callback(new OrtcPresenceException(ex.Message), null); } else { if (synchContext != null) { synchContext.Post(obj => callback(new OrtcPresenceException(ex.Message), null), null); } else { ThreadPool.QueueUserWorkItem((o) => callback(new OrtcPresenceException(ex.Message), null)); } } #endif } }), request); }
public void SetOnGetBatteryLevelResponse(OnResponseDelegate onGetBatteryLevelResponse) { this.onGetBatteryLevelResponse = onGetBatteryLevelResponse ?? throw new ArgumentNullException(nameof(onGetBatteryLevelResponse)); }
public void SetOnGetU80mVoltsResponse(OnResponseDelegate onGetU80mVoltsResponse) { this.onGetU80mVoltsResponse = onGetU80mVoltsResponse ?? throw new ArgumentNullException(nameof(onGetU80mVoltsResponse)); }
public void SetOnGetLastFailureCodeResponse(OnResponseDelegate onGetLastFailureCodeResponse) { this.onGetLastFailureCodeResponse = onGetLastFailureCodeResponse ?? throw new ArgumentNullException(nameof(onGetLastFailureCodeResponse)); }
public void SetOnSetFoxPowerResponse(OnResponseDelegate onSetFoxPowerResponse) { this.onSetFoxPowerResponse = onSetFoxPowerResponse ?? throw new ArgumentNullException(nameof(onSetFoxPowerResponse)); }
/* * internal static void GetAsync(String url, OnResponseDelegate callback) * { * RestWebservice.RequestAsync(url, "GET",null, callback); * }*/ /* * internal static void PostAsync(String url,String content, OnResponseDelegate callback) * { * RestWebservice.RequestAsync(url, "POST",content, callback); * } */ private static void RequestAsync(String url, String method, String content, OnResponseDelegate callback) { /* * var request = (HttpWebRequest)WebRequest.Create(new Uri(url)); * * //ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3; * * request.Proxy = null; * //request.Timeout = 10000; * //request.ProtocolVersion = HttpVersion.Version11; * request.Method = method; * * if (String.Compare(method,"POST") == 0 && !String.IsNullOrEmpty(content)) * { * byte[] postBytes = Encoding.UTF8.GetBytes(content); * * request.ContentType = "application/x-www-form-urlencoded"; * //request.ContentLength = postBytes.Length; * * using (Stream requestStream = request.GetRequestStream()) * { * requestStream.Write(postBytes, 0, postBytes.Length); * requestStream.Close(); * } * } * * request.BeginGetResponse(new AsyncCallback((asynchronousResult) => * { * var server = String.Empty; * * var synchContext = System.Threading.SynchronizationContext.Current; * * try * { * HttpWebRequest asyncRequest = (HttpWebRequest)asynchronousResult.AsyncState; * * HttpWebResponse response = (HttpWebResponse)asyncRequest.EndGetResponse(asynchronousResult); * Stream streamResponse = response.GetResponseStream(); * StreamReader streamReader = new StreamReader(streamResponse); * * var responseBody = streamReader.ReadToEnd(); * * if (callback != null) * { * if (!String.IsNullOrEmpty(HttpRuntime.AppDomainAppVirtualPath)) * { * callback(null, responseBody); * } * else * { * if (synchContext != null) * { * synchContext.Post(obj => callback(null, responseBody), null); * } * else * { * Task.Factory.StartNew(() => callback(null, responseBody)); * } * } * } * } * catch (WebException wex) * { * String errorMessage = String.Empty; * if (wex.Response == null) * { * errorMessage = "Uknown request error"; * if (!String.IsNullOrEmpty(HttpRuntime.AppDomainAppVirtualPath)) * { * callback(new OrtcPresenceException(errorMessage), null); * } * else * { * if (synchContext != null) * { * synchContext.Post(obj => callback(new OrtcPresenceException(errorMessage), null), null); * } * else * { * Task.Factory.StartNew(() => callback(new OrtcPresenceException(errorMessage), null)); * } * } * } * else * { * using (var stream = wex.Response.GetResponseStream()) * { * using (var reader = new StreamReader(stream)) * { * errorMessage = reader.ReadToEnd(); * } * * if (!String.IsNullOrEmpty(HttpRuntime.AppDomainAppVirtualPath)) * { * callback(new OrtcPresenceException(errorMessage), null); * } * else * { * if (synchContext != null) * { * synchContext.Post(obj => callback(new OrtcPresenceException(errorMessage), null), null); * } * else * { * Task.Factory.StartNew(() => callback(new OrtcPresenceException(errorMessage), null)); * } * } * } * } * } * catch (Exception ex) * { * if (!String.IsNullOrEmpty(HttpRuntime.AppDomainAppVirtualPath)) * { * callback(new OrtcPresenceException(ex.Message), null); * } * else * { * * if (synchContext != null) * { * synchContext.Post(obj => callback(new OrtcPresenceException(ex.Message), null), null); * } * else * { * Task.Factory.StartNew(() => callback(new OrtcPresenceException(ex.Message), null)); * } * } * } * }), request); */ }