Exemple #1
0
        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();
     }
 }
Exemple #3
0
        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);
 }
Exemple #7
0
 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);
         }
     }
 }
Exemple #8
0
        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);
Exemple #11
0
        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);
 }
Exemple #15
0
 public void SetOnIsFoxArmedResponse(OnResponseDelegate onIsFoxArmedResponse)
 {
     this.onIsFoxArmedResponse = onIsFoxArmedResponse ?? throw new ArgumentNullException(nameof(onIsFoxArmedResponse));
 }
Exemple #16
0
 public void SetOnSetEndingToneDurationResponse(OnResponseDelegate onSetEndingToneDurationResponse)
 {
     this.onSetEndingToneDurationResponse = onSetEndingToneDurationResponse ?? throw new ArgumentNullException(nameof(onSetEndingToneDurationResponse));
 }
Exemple #17
0
 public void SetOnSwitchToProfileResponse(OnResponseDelegate onSwitchToProfileResponse)
 {
     this.onSwitchToProfileResponse = onSwitchToProfileResponse ?? throw new ArgumentNullException(nameof(onSwitchToProfileResponse));
 }
Exemple #18
0
 public void SetOnGetCurrentProfileIdResponse(OnResponseDelegate onGetCurrentProfileIdResponse)
 {
     this.onGetCurrentProfileIdResponse = onGetCurrentProfileIdResponse ?? throw new ArgumentNullException(nameof(onGetCurrentProfileIdResponse));
 }
Exemple #19
0
 public void SetOnGetAntennaMatchingStatusResponse(OnResponseDelegate onGetAntennaMatchingStatusResponse)
 {
     _onGetAntennaMatchingStatusResponse = onGetAntennaMatchingStatusResponse
                                           ?? throw new ArgumentNullException(nameof(onGetAntennaMatchingStatusResponse));
 }
 public abstract void Post(string url, Dictionary <string, string> requestParams, OnResponseDelegate callback);
Exemple #21
0
 public void SetOnGetIdentificationDataResponse(OnResponseDelegate onGetIdentificationDataResponse)
 {
     this.onGetIdentificationDataResponse = onGetIdentificationDataResponse ?? throw new ArgumentNullException(nameof(onGetIdentificationDataResponse));
 }
Exemple #22
0
 public void SetOnGetUBattADCResponse(OnResponseDelegate onGetUBattADCResponse)
 {
     _onGetUBattADCResponse = onGetUBattADCResponse ?? throw new ArgumentNullException(nameof(onGetUBattADCResponse));
 }
Exemple #23
0
 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);
 }
Exemple #25
0
 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);
 }
Exemple #27
0
 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);
        }
Exemple #29
0
 public void SetOnGetBatteryLevelResponse(OnResponseDelegate onGetBatteryLevelResponse)
 {
     this.onGetBatteryLevelResponse = onGetBatteryLevelResponse ?? throw new ArgumentNullException(nameof(onGetBatteryLevelResponse));
 }
Exemple #30
0
 public void SetOnGetU80mVoltsResponse(OnResponseDelegate onGetU80mVoltsResponse)
 {
     this.onGetU80mVoltsResponse = onGetU80mVoltsResponse ?? throw new ArgumentNullException(nameof(onGetU80mVoltsResponse));
 }
Exemple #31
0
 public void SetOnGetLastFailureCodeResponse(OnResponseDelegate onGetLastFailureCodeResponse)
 {
     this.onGetLastFailureCodeResponse = onGetLastFailureCodeResponse ?? throw new ArgumentNullException(nameof(onGetLastFailureCodeResponse));
 }
Exemple #32
0
 public void SetOnSetFoxPowerResponse(OnResponseDelegate onSetFoxPowerResponse)
 {
     this.onSetFoxPowerResponse = onSetFoxPowerResponse ?? throw new ArgumentNullException(nameof(onSetFoxPowerResponse));
 }
Exemple #33
0
 /*
  * 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);
      */
 }