コード例 #1
0
        public string BlockingCall()
        {
            HttpWebRequest  request  = CreateRequest();
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            return(response.GetContent());
        }
コード例 #2
0
        private async Task ReceivedRequestEliteServerState(ConnectorMessage.RequestEliteServerState message)
        {
            HttpWebRequest  req    = WebRequest.CreateHttp($"{BASE_URL}api-status-v1/elite-server");
            HttpWebResponse resp   = (await req.GetResponseAsync()) as HttpWebResponse;
            string          data   = resp.GetContent();
            var             dataEs = JsonConvert.DeserializeObject <Responses.EliteServerReq>(data);

            this.uiMessenger.Tell(new UiMessengerMessage.Publish(new EliteServerState(dataEs.LastUpdate, dataEs.Message)));
        }
コード例 #3
0
        public string AsyncCallWithWait()
        {
            Task <WebResponse> serviceCallTask = CallService();

            // serviceCallTask.Wait(); // no need to use .Wait() because .Result blocks by itself

            HttpWebResponse response = (HttpWebResponse)serviceCallTask.Result;

            return(response.GetContent());
        }
コード例 #4
0
 private async Task ReceivedRequestSystemData(ConnectorMessage.RequestSystemData message)
 {
     try
     {
         HttpWebRequest reqInfo = WebRequest.CreateHttp($"{BASE_URL}api-v1/system?systemName={message.SystemName}&showId=1&showCoordinates=1&showPermit=1&showInformation=0&showPrimaryStar=1");
         reqInfo.Timeout = 5000;
         HttpWebResponse respInfo = (await reqInfo.GetResponseAsync()) as HttpWebResponse;
         string          dataInfo = respInfo.GetContent();
         if ((String.IsNullOrWhiteSpace(dataInfo)) || (dataInfo == "[]") || (respInfo.StatusCode == HttpStatusCode.NotFound))
         {
             this.uiMessenger.Tell(new UiMessengerMessage.Publish(new NoSystemData(message.SystemName)));
             return;
         }
         Responses.SystemReq objInfo;
         try
         {
             objInfo = JsonConvert.DeserializeObject <Responses.SystemReq>(dataInfo);
         }
         catch (Exception ex)
         {
             log.Error($"Failed to deserialize EDSM systemdata for {message.SystemName} ({dataInfo.Length} chars): {ex.Message}");
             throw;
         }
         HttpWebRequest reqBodies = WebRequest.CreateHttp($"{BASE_URL}api-system-v1/bodies?systemName={message.SystemName}");
         reqBodies.Timeout = 5000;
         HttpWebResponse     respBodies = (await reqBodies.GetResponseAsync()) as HttpWebResponse;
         string              dataBodies = respBodies.GetContent();
         Responses.BodiesReq objBodies;
         try
         {
             objBodies = JsonConvert.DeserializeObject <Responses.BodiesReq>(dataBodies);
         }
         catch (Exception ex)
         {
             log.Error($"Failed to deserialize EDSM bodiesdata for {message.SystemName} ({dataBodies.Length} chars): {ex.Message}");
             throw;
         }
         List <BodyData> bodies = new List <BodyData>();
         foreach (var b in objBodies.Bodies)
         {
             bodies.Add(new BodyData(b.Id64, b.Name, b.BodyId, b.Discovery?.Commander, b.TerraformingState));
         }
         SystemData systemData = new SystemData(objInfo.Name, bodies.ToImmutableArray());
         this.uiMessenger.Tell(new UiMessengerMessage.Publish(systemData));
     }
     catch (WebException ex) when(ex.Status == WebExceptionStatus.Timeout)
     {
         log.Warning($"Timeout while getting system data for {message.SystemName}");
     }
     catch (Exception ex)
     {
         log.Error(ex, $"Failed to get EDSM data for {message.SystemName}");
     }
 }
コード例 #5
0
        public string AsyncCallWithWait()
        {
            HttpWebRequest request     = CreateRequest();
            IAsyncResult   asyncResult = request.BeginGetResponse(null, null);

            asyncResult.AsyncWaitHandle.WaitOne();

            HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult);

            return(response.GetContent());
        }
コード例 #6
0
        private static void OnReceive(HttpWebRequest req, object state, HttpWebResponse res)
        {
            OnDataReceived(res.GetContent());

            if (_Timeout == null)
            {
                return;
            }

            _Timeout.Stop();
            _Timeout = null;
        }
コード例 #7
0
        public void ASyncCallWithCallback(Action <string> handler)
        {
            HttpWebRequest request = CreateRequest();

            request.BeginGetResponse(asyncResult =>
            {
                HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult);

                string result = response.GetContent();

                handler(result);
            }, null);
        }
コード例 #8
0
        private static void EndVerification(HttpWebRequest webReq, string state, HttpWebResponse webRes)
        {
            var content = webRes.GetContent();

            if (Insensitive.Contains(content, "VERIFIED"))
            {
                using (var queries = new WebAPIQueries(state))
                {
                    ProcessTransaction(queries);
                }

                RefreshAdminUI();
            }
        }
コード例 #9
0
ファイル: TestExtensions.cs プロジェクト: fossabot/GenHTTP
        public static HashSet <string> GetSitemap(this HttpWebResponse response)
        {
            var content = response.GetContent();

            var sitemap = XDocument.Parse(content);

            var namespaces = new XmlNamespaceManager(new NameTable());

            namespaces.AddNamespace("n", "http://www.sitemaps.org/schemas/sitemap/0.9");

            return(sitemap.Root.XPathSelectElements("//n:loc", namespaces)
                   .Select(x => new Uri(x.Value).AbsolutePath)
                   .ToHashSet());
        }
コード例 #10
0
        private async Task <string> GetResponseAsync(HttpWebRequest request, string requestUrl, bool throwOnHttpErrorCode)
        {
            string content;

            HttpWebResponse response = null;

            try
            {
                if (throwOnHttpErrorCode)
                {
                    response = (HttpWebResponse)(await request.GetResponseAsync());
                }
                else
                {
                    try
                    {
                        response = (HttpWebResponse)(await request.GetResponseAsync());
                    }
                    catch (WebException ex)
                    {
                        response = (HttpWebResponse)ex.Response;
                    }
                }

                if (UseCookies)
                {
                    request.FixCookies(response);
                    if (Cookies == null)
                    {
                        Cookies = response.Cookies;
                    }
                    else
                    {
                        Cookies.Add(response.Cookies);
                    }
                }
                content = response.GetContent();
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                }
            }

            return(content);
        }
コード例 #11
0
        private static void EndVerification(HttpWebRequest webReq, string state, HttpWebResponse webRes)
        {
            var content = webRes.GetContent();

            File.AppendAllLines("IPN.log", new[] { "\n\nREQUEST:\n", state, "\n\nRESPONSE:\n", content });

            if (Insensitive.Contains(content, "VERIFIED"))
            {
                using (var queries = new WebAPIQueries(state))
                {
                    ProcessTransaction(queries);
                }

                RefreshAdminUI();
            }
        }
コード例 #12
0
        public Task <string> ASyncCallWithContinuation()
        {
            Task <WebResponse> serviceCallTask = CallService();

            var resultSource = new TaskCompletionSource <string>();

            serviceCallTask.ContinueWith(previousTask =>
            {
                HttpWebResponse response = (HttpWebResponse)previousTask.Result;

                string result = response.GetContent();

                resultSource.SetResult(result);
            });

            return(resultSource.Task);
        }