Esempio n. 1
0
      public static async Task <StringBuilder> PostWebPage(string Url, Dictionary <string, string> postData,
                                                           string RefererUrl, CookieCollection cookieCollection, Action <string> userNotify = null)
      {
          StringBuilder BodyString = new StringBuilder();
          bool          urlCorrect = Uri.TryCreate(Url, UriKind.Absolute, out Uri URI) &&
                                     (URI.Scheme == Uri.UriSchemeHttp || URI.Scheme == Uri.UriSchemeHttps);

          if (!urlCorrect)
          {
              Debug.Write("GetWeb." + System.Reflection.MethodBase.GetCurrentMethod().Name + " => URL not correct");
              return(BodyString);
          }

          var BaseURI = new Uri(URI.GetLeftPart(UriPartial.Authority));
          var PathURI = new Uri(URI.PathAndQuery);

          using (var handler = new HttpClientHandler())
          {
              if (cookieCollection != null)
              {
                  var cookies = new CookieContainer();
                  cookies.Add(cookieCollection);
                  handler.UseCookies      = true;
                  handler.CookieContainer = cookies;
              }
              else
              {
                  handler.UseCookies = false;
              }

              using (HttpClient client = new HttpClient(handler)
                {
                    Timeout = TimeSpan.FromSeconds(6)
                })
              {
                  try
                  {
                      client.BaseAddress = BaseURI;
                      client.DefaultRequestHeaders.Add("X-Requested-With", "XMLHttpRequest");

                      var keyValues = new List <KeyValuePair <string, string> >();
                      foreach (var item in postData)
                      {
                          keyValues.Add(new KeyValuePair <string, string>(item.Key, item.Value));
                      }

                      var inputContent = new FormUrlEncodedContent(postData);

                      using (HttpResponseMessage response = await client.PostAsync(PathURI, inputContent).ConfigureAwait(false))
                      {
                          if (!response.IsSuccessStatusCode)
                          {
                              Debug.Write("GetAdvertListAsync(AdvertSearch searchParams) => !response.IsSuccessStatusCode");
                              userNotify?.Invoke("Niepoprawna odpowiedź z serwera");
                              return(BodyString);
                          }
                          using (HttpContent content = response.Content)
                          {
                              var byteArray = await content.ReadAsStringAsync();

                              BodyString.Append(byteArray);
                          }
                      }
                  }
                  catch (System.Threading.Tasks.TaskCanceledException)
                  {
                      Debug.Write("PostWebPage => System.Threading.Tasks.TaskCanceledException");
                      userNotify?.Invoke("Błąd połączenia. Przekroczono limit połączenia");
                      return(BodyString);
                  }
                  catch (Exception e)
                  {
                      Debug.Write("PostWebPage => " + e.Message);
                      userNotify?.Invoke("Błąd połączenia z serwerem.");
                      return(BodyString);
                  }
              }
              return(BodyString);
          }
      }