Beispiel #1
0
    public Http.HttpResponse GetUploadResponse(string Uri, NameValueCollection Headers, NameValueCollection Fields, params Http.UploadData[] Upload)
    {
      Http.HttpResponse httpResponse = new Http.HttpResponse();
      HttpWebResponse httpWebResponse = (HttpWebResponse) null;
      string str1 = Guid.NewGuid().ToString().Replace("-", "");
label_1:
      try
      {
        if (string.IsNullOrEmpty(Uri))
        {
          httpResponse = new Http.HttpResponse();
          httpResponse.Exception = (object) new Exception("Uri was empty.");
          return httpResponse;
        }
        if (Fields == null && Upload == null)
        {
          httpResponse = this.GetResponse(Uri, "", (NameValueCollection) null);
        }
        else
        {
          if (this.ForceHttps && Uri.StartsWith("http"))
            Uri = "https" + Uri.Substring(Uri.IndexOf("://"));
          this._LastResponseUri = Uri;
          string str2 = Conversions.ToString(Interaction.IIf(Uri.Contains("/") && !Uri.EndsWith("/"), (object) Uri.Substring(0, Uri.LastIndexOf("/")), (object) Uri));
          this.Request = (HttpWebRequest) WebRequest.Create(Uri);
          HttpWebRequest httpWebRequest1 = this.Request;
          ServicePoint servicePoint = httpWebRequest1.ServicePoint;
          servicePoint.GetType().GetProperty("HttpBehaviour", BindingFlags.Instance | BindingFlags.NonPublic).SetValue((object) servicePoint, (object) (byte) 0, (object[]) null);
          httpWebRequest1.Method = "POST";
          if (this.DebugMode)
            Debug.Print(string.Format("[{0}] POST (Multi-Part) >> {1}", (object) DateAndTime.Now.ToString("hh:mm:ss tt").ToLower(), (object) Uri));
          httpWebRequest1.AllowWriteStreamBuffering = false;
          httpWebRequest1.AllowAutoRedirect = false;
          httpWebRequest1.KeepAlive = this.KeepAlive;
          httpWebRequest1.UserAgent = this.Useragent;
          httpWebRequest1.ContentType = Conversions.ToString(Interaction.IIf(Operators.CompareString(httpWebRequest1.Method, "POST", true) == 0, (object) this.ContentType, (object) string.Empty));
          httpWebRequest1.ContentType = "multipart/form-data; boundary=" + str1;
          httpWebRequest1.AutomaticDecompression = DecompressionMethods.None;
          httpWebRequest1.Accept = this.Accept;
          httpWebRequest1.Timeout = this.TimeOut;
          httpWebRequest1.ProtocolVersion = this.Version;
          httpWebRequest1.ServicePoint.Expect100Continue = this.AllowExpect100;
          if (!string.IsNullOrEmpty(this.Referer))
            httpWebRequest1.Referer = this.Referer;
          if (this.SendCookies)
          {
            string cookies = this.GetCookies(new Uri(Uri).Host);
            if (!string.IsNullOrEmpty(cookies))
              httpWebRequest1.Headers.Add("Cookie", cookies);
          }
          Http.HttpProxy proxy1 = this.Proxy;
          if (!string.IsNullOrEmpty(proxy1.Server))
          {
            HttpWebRequest httpWebRequest2 = httpWebRequest1;
            Http.HttpProxy proxy2 = this.Proxy;
            WebProxy webProxy = new WebProxy(proxy2.Server, this.Proxy.Port);
            httpWebRequest2.Proxy = (IWebProxy) webProxy;
            if (!string.IsNullOrEmpty(this.Proxy.UserName))
            {
              IWebProxy proxy3 = httpWebRequest1.Proxy;
              proxy2 = this.Proxy;
              string userName = proxy2.UserName;
              proxy1 = this.Proxy;
              string password = proxy1.Password;
              NetworkCredential networkCredential = new NetworkCredential(userName, password);
              proxy3.Credentials = (ICredentials) networkCredential;
            }
          }
          if (!string.IsNullOrEmpty(this.AcceptEncoding))
            httpWebRequest1.Headers.Add(HttpRequestHeader.AcceptEncoding, this.AcceptEncoding);
          if (!string.IsNullOrEmpty(this.AcceptLanguage))
            httpWebRequest1.Headers.Add(HttpRequestHeader.AcceptLanguage, this.AcceptLanguage);
          if (!string.IsNullOrEmpty(this.AcceptCharset))
            httpWebRequest1.Headers.Add(HttpRequestHeader.AcceptCharset, this.AcceptCharset);
          if (Headers != null)
          {
            int num1 = 0;
            int num2 = checked (Headers.Count - 1);
            int index = num1;
            while (index <= num2)
            {
              if (Operators.CompareString(Headers.Keys[index], "Content-Type", true) == 0)
                httpWebRequest1.ContentType = Headers[index];
              else
                httpWebRequest1.Headers.Add(Headers.Keys[index], Headers[index]);
              checked { ++index; }
            }
          }
          MemoryStream memoryStream = new MemoryStream();
          StreamWriter streamWriter = new StreamWriter((Stream) memoryStream);
          if (Fields != null)
          {
            int num1 = 0;
            int num2 = checked (Fields.Count - 1);
            int index = num1;
            while (index <= num2)
            {
              streamWriter.Write("--" + str1 + "\r\n");
              streamWriter.Write("Content-Disposition: form-data; name=\"{0}\"{1}{1}{2}{1}", (object) Fields.Keys[index], (object) "\r\n", (object) Fields[index]);
              checked { ++index; }
            }
          }
          if (Upload != null)
          {
            Http.UploadData[] uploadDataArray = Upload;
            int index = 0;
            while (index < uploadDataArray.Length)
            {
              Http.UploadData uploadData = uploadDataArray[index];
              streamWriter.Write("--" + str1 + "\r\n");
              streamWriter.Write("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"{2}", (object) uploadData.FieldName, (object) uploadData.FileName, (object) "\r\n");
              streamWriter.Write("Content-Type: " + this.GetContentType(uploadData.FileName) + "\r\n\r\n");
              streamWriter.Flush();
              if (uploadData.Contents != null)
                memoryStream.Write(uploadData.Contents, 0, uploadData.Contents.Length);
              streamWriter.Write("\r\n");
              checked { ++index; }
            }
          }
          streamWriter.Write("--{0}--{1}", (object) str1, (object) "\r\n");
          streamWriter.Flush();
          httpWebRequest1.ContentLength = memoryStream.Length;
          using (Stream requestStream = httpWebRequest1.GetRequestStream())
            memoryStream.WriteTo(requestStream);
          memoryStream.Close();
          Fields = (NameValueCollection) null;
          Upload = (Http.UploadData[]) null;
          HttpWebResponse Response = (HttpWebResponse) httpWebRequest1.GetResponse();
          if (this.StoreCookies)
            this.ParseCookies(Response);
          httpResponse.WebResponse = Response;
          httpResponse.ResponseUri = httpResponse.WebResponse.ResponseUri.ToString();
          StringBuilder stringBuilder = new StringBuilder();
          string[] allKeys1 = httpResponse.WebResponse.Headers.AllKeys;
          int index1 = 0;
          while (index1 < allKeys1.Length)
          {
            string index2 = allKeys1[index1];
            string str3 = httpResponse.WebResponse.Headers[index2];
            stringBuilder.AppendLine(index2 + ": " + str3);
            checked { ++index1; }
          }
          httpResponse.ResponseHeaders = stringBuilder.ToString();
          httpResponse.WebRequest = this.Request;
          httpResponse.RequestUri = Uri.ToString();
          string[] allKeys2 = httpResponse.WebRequest.Headers.AllKeys;
          int index3 = 0;
          while (index3 < allKeys2.Length)
          {
            string index2 = allKeys2[index3];
            string str3 = httpResponse.WebRequest.Headers[index2];
            stringBuilder.AppendLine(index2 + ": " + str3);
            checked { ++index3; }
          }
          httpResponse.RequestHeaders = stringBuilder.ToString();
          if (this.AutoRedirect)
          {
            switch (httpResponse.WebResponse.StatusCode)
            {
              case HttpStatusCode.MovedPermanently:
              case HttpStatusCode.Found:
              case HttpStatusCode.SeeOther:
                Uri = httpResponse.WebResponse.Headers["Location"];
                try
                {
                  Uri uri = new Uri(Uri);
                }
                catch (Exception ex)
                {
                  ProjectData.SetProjectError(ex);
                  Uri = Conversions.ToString(Operators.ConcatenateObject((object) str2, Interaction.IIf(Uri.StartsWith("/"), (object) Uri, (object) ("/" + Uri))));
                  ProjectData.ClearProjectError();
                }
                bool flag1 = true;
                List<string>.Enumerator enumerator1;
                try
                {
                  enumerator1 = this.RedirectBlacklist.GetEnumerator();
                  while (enumerator1.MoveNext())
                  {
                    string current = enumerator1.Current;
                    if (Uri.ToLower().Contains(current.ToLower()))
                    {
                      flag1 = false;
                      break;
                    }
                  }
                }
                finally
                {
                  enumerator1.Dispose();
                }
                if (!flag1)
                  break;
                goto label_1;
            }
          }
          if (httpResponse.WebResponse != null)
            httpResponse.Headers = httpResponse.WebResponse.Headers.ToString();
          if (httpResponse.WebResponse.Headers[System.Net.HttpResponseHeader.ContentType] != null && !(httpResponse.WebResponse.Headers[System.Net.HttpResponseHeader.ContentType].Contains("text/") | httpResponse.WebResponse.Headers[System.Net.HttpResponseHeader.ContentType].Contains("/xml") | httpResponse.WebResponse.Headers[System.Net.HttpResponseHeader.ContentType].Contains("/json") | httpResponse.WebResponse.Headers[System.Net.HttpResponseHeader.ContentType].Contains("application/")))
          {
            if (httpResponse.WebResponse.Headers[System.Net.HttpResponseHeader.ContentType].Contains("image/"))
            {
              if (this.GetImage)
                httpResponse.Image = Image.FromStream(Response.GetResponseStream());
            }
            else
              Debug.Print("Unknown content type: " + httpResponse.WebResponse.Headers[System.Net.HttpResponseHeader.ContentType]);
          }
          else
          {
            if (httpResponse.WebResponse != null)
              httpResponse.Html = HttpUtility.HtmlDecode(this.ProcessResponse(httpResponse.WebResponse));
            if (this.AutoRedirect && httpResponse.Html.ToLower().Contains("<meta http-equiv=\"refresh"))
            {
              Uri = this.ParseMetaRefreshUrl(httpResponse.Html);
              try
              {
                Uri uri = new Uri(Uri);
              }
              catch (Exception ex)
              {
                ProjectData.SetProjectError(ex);
                Uri = Conversions.ToString(Operators.ConcatenateObject((object) str2, Interaction.IIf(Uri.StartsWith("/"), (object) Uri, (object) ("/" + Uri))));
                ProjectData.ClearProjectError();
              }
              bool flag2 = true;
              List<string>.Enumerator enumerator2;
              try
              {
                enumerator2 = this.RedirectBlacklist.GetEnumerator();
                while (enumerator2.MoveNext())
                {
                  string current = enumerator2.Current;
                  if (Uri.ToLower().Contains(current.ToLower()))
                  {
                    flag2 = false;
                    break;
                  }
                }
              }
              finally
              {
                enumerator2.Dispose();
              }
              if (flag2)
                goto label_1;
            }
          }
        }
      }
      catch (WebException ex)
      {
        ProjectData.SetProjectError((Exception) ex);
        WebException webException = ex;
        httpResponse.Exception = (object) webException;
        if (webException.Response != null)
        {
          httpResponse.WebResponse = (HttpWebResponse) webException.Response;
          httpResponse.Html = this.ProcessResponse(httpResponse.WebResponse);
        }
        ProjectData.ClearProjectError();
      }
      catch (Exception ex)
      {
        ProjectData.SetProjectError(ex);
        Exception exception = ex;
        httpResponse.Exception = (object) exception;
        ProjectData.ClearProjectError();
      }
      finally
      {
        this.Request = (HttpWebRequest) null;
        httpWebResponse = (HttpWebResponse) null;
        this.Referer = string.Empty;
      }
      return httpResponse;
    }
Beispiel #2
0
    public Http.HttpResponse GetResponse(string Uri, string PostData = "", NameValueCollection Headers = null)
    {
      Http.HttpResponse httpResponse = new Http.HttpResponse();
      HttpWebResponse httpWebResponse = (HttpWebResponse) null;
      this._LastRequestUri = Uri;
label_1:
      try
      {
        if (string.IsNullOrEmpty(Uri))
        {
          httpResponse = new Http.HttpResponse();
          httpResponse.Exception = (object) new Exception("Uri was empty.");
          return httpResponse;
        }
        if (this.ForceHttps && Uri.StartsWith("http"))
          Uri = "https" + Uri.Substring(Uri.IndexOf("://"));
        if (this.DebugMode)
          Debug.Print(string.Format("[{0}] {1} >> {2}", (object) DateAndTime.Now.ToString("hh:mm:ss tt").ToLower(), RuntimeHelpers.GetObjectValue(Interaction.IIf(string.IsNullOrEmpty(PostData), (object) "GET", (object) "POST")), (object) Uri));
        string Url = Uri;
        if (!this.IsValidUri(Url))
        {
          if (Url.StartsWith("&") | Url.StartsWith("?"))
          {
            Debug.Print("Last request Uri: " + this._LastRequestUri);
            Debug.Print("Last response Uri: " + this._LastResponseUri);
            Debug.Print("Uri: " + Uri);
            Debug.Print("Referer: " + this._Referer);
            Debug.Print("");
          }
          else if (Url.StartsWith("/"))
          {
            Debug.Print("Last request Uri: " + this._LastRequestUri);
            Debug.Print("Last response Uri: " + this._LastResponseUri);
            Debug.Print("Uri: " + Uri);
            Debug.Print("Referer: " + this._Referer);
            Debug.Print("");
          }
          else
          {
            Debug.Print("Last request Uri: " + this._LastRequestUri);
            Debug.Print("Last response Uri: " + this._LastResponseUri);
            Debug.Print("Uri: " + Uri);
            Debug.Print("Referer: " + this._Referer);
            Debug.Print("");
          }
        }
        this.Request = (HttpWebRequest) WebRequest.Create(Uri);
        HttpWebRequest httpWebRequest1 = this.Request;
        ServicePoint servicePoint = httpWebRequest1.ServicePoint;
        servicePoint.GetType().GetProperty("HttpBehaviour", BindingFlags.Instance | BindingFlags.NonPublic).SetValue((object) servicePoint, (object) (byte) 0, (object[]) null);
        httpWebRequest1.Method = Conversions.ToString(Interaction.IIf(string.IsNullOrEmpty(PostData), (object) "GET", (object) "POST"));
        httpWebRequest1.AllowWriteStreamBuffering = false;
        httpWebRequest1.AllowAutoRedirect = false;
        httpWebRequest1.KeepAlive = this.KeepAlive;
        httpWebRequest1.UserAgent = this.Useragent;
        httpWebRequest1.ContentType = Conversions.ToString(Interaction.IIf(Operators.CompareString(httpWebRequest1.Method, "POST", true) == 0, (object) this.ContentType, (object) string.Empty));
        httpWebRequest1.AutomaticDecompression = DecompressionMethods.None;
        httpWebRequest1.Accept = this.Accept;
        httpWebRequest1.Timeout = this.TimeOut;
        httpWebRequest1.ProtocolVersion = this.Version;
        httpWebRequest1.ServicePoint.Expect100Continue = this.AllowExpect100;
        if (!string.IsNullOrEmpty(this.Referer))
          httpWebRequest1.Referer = this.Referer;
        if (!string.IsNullOrEmpty(this.Proxy.Server))
        {
          HttpWebRequest httpWebRequest2 = httpWebRequest1;
          Http.HttpProxy proxy1 = this.Proxy;
          string Host = proxy1.Server;
          Http.HttpProxy proxy2 = this.Proxy;
          int Port = proxy2.Port;
          WebProxy webProxy = new WebProxy(Host, Port);
          httpWebRequest2.Proxy = (IWebProxy) webProxy;
          proxy2 = this.Proxy;
          if (!string.IsNullOrEmpty(proxy2.UserName))
          {
            IWebProxy proxy3 = httpWebRequest1.Proxy;
            proxy1 = this.Proxy;
            NetworkCredential networkCredential = new NetworkCredential(proxy1.UserName, this.Proxy.Password);
            proxy3.Credentials = (ICredentials) networkCredential;
          }
        }
        if (this.SendCookies)
        {
          string cookies = this.GetCookies(new Uri(Uri).Host);
          if (!string.IsNullOrEmpty(cookies))
            httpWebRequest1.Headers.Add("Cookie", cookies);
        }
        if (!string.IsNullOrEmpty(this.AcceptEncoding))
          httpWebRequest1.Headers.Add(HttpRequestHeader.AcceptEncoding, this.AcceptEncoding);
        if (!string.IsNullOrEmpty(this.AcceptLanguage))
          httpWebRequest1.Headers.Add(HttpRequestHeader.AcceptLanguage, this.AcceptLanguage);
        if (!string.IsNullOrEmpty(this.AcceptCharset))
          httpWebRequest1.Headers.Add(HttpRequestHeader.AcceptCharset, this.AcceptCharset);
        if (Headers != null)
        {
          int num1 = 0;
          int num2 = checked (Headers.Count - 1);
          int index = num1;
          while (index <= num2)
          {
            if (Operators.CompareString(Headers.Keys[index], "Content-Type", true) == 0)
              httpWebRequest1.ContentType = Headers[index];
            else
              httpWebRequest1.Headers.Add(Headers.Keys[index], Headers[index]);
            checked { ++index; }
          }
        }
        if (!string.IsNullOrEmpty(PostData))
        {
          byte[] bytes = Encoding.UTF8.GetBytes(PostData);
          httpWebRequest1.ContentLength = (long) bytes.Length;
          Stream requestStream = httpWebRequest1.GetRequestStream();
          requestStream.Write(bytes, 0, bytes.Length);
          requestStream.Close();
          requestStream.Dispose();
        }
        PostData = string.Empty;
        HttpWebResponse Response = (HttpWebResponse) httpWebRequest1.GetResponse();
        if (this.StoreCookies)
          this.ParseCookies(Response);
        httpResponse.WebResponse = Response;
        httpResponse.ResponseUri = httpResponse.WebResponse.ResponseUri.ToString();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.AppendLine("ResponseUri: " + httpResponse.ResponseUri);
        string[] allKeys1 = httpResponse.WebResponse.Headers.AllKeys;
        int index1 = 0;
        while (index1 < allKeys1.Length)
        {
          string index2 = allKeys1[index1];
          string str = httpResponse.WebResponse.Headers[index2];
          stringBuilder.AppendLine(index2 + ": " + str);
          checked { ++index1; }
        }
        httpResponse.ResponseHeaders = stringBuilder.ToString();
        httpResponse.WebRequest = this.Request;
        httpResponse.RequestUri = httpResponse.WebRequest.RequestUri.ToString();
        stringBuilder.AppendLine("RequestUri: " + httpResponse.RequestUri);
        string[] allKeys2 = httpResponse.WebRequest.Headers.AllKeys;
        int index3 = 0;
        while (index3 < allKeys2.Length)
        {
          string index2 = allKeys2[index3];
          string str = httpResponse.WebRequest.Headers[index2];
          stringBuilder.AppendLine(index2 + ": " + str);
          if (!string.IsNullOrEmpty(PostData))
            stringBuilder.AppendLine("POST: " + PostData);
          checked { ++index3; }
        }
        httpResponse.RequestHeaders = stringBuilder.ToString();
        if (this.AutoRedirect)
        {
          switch (httpResponse.WebResponse.StatusCode)
          {
            case HttpStatusCode.MovedPermanently:
            case HttpStatusCode.Found:
            case HttpStatusCode.SeeOther:
              Uri = httpResponse.WebResponse.Headers["Location"];
              Debug.Print("Before: " + Uri);
              Debug.Print("Fullpath: " + Url);
              if (!this.IsValidUri(Uri))
              {
                Uri = Conversions.ToString(Operators.ConcatenateObject((object) Url, Interaction.IIf(Uri.StartsWith("/"), (object) Uri, (object) ("/" + Uri))));
                Debug.Print("After: " + Uri);
                Debug.Print("");
              }
              bool flag1 = true;
              List<string>.Enumerator enumerator1;
              try
              {
                enumerator1 = this.RedirectBlacklist.GetEnumerator();
                while (enumerator1.MoveNext())
                {
                  string current = enumerator1.Current;
                  if (Uri.ToLower().Contains(current.ToLower()))
                  {
                    flag1 = false;
                    break;
                  }
                }
              }
              finally
              {
                enumerator1.Dispose();
              }
              PostData = string.Empty;
              if (!flag1)
                break;
              goto label_1;
          }
        }
        if (httpResponse.WebResponse != null)
          httpResponse.Headers = httpResponse.WebResponse.Headers.ToString();
        if (httpResponse.WebResponse.Headers[System.Net.HttpResponseHeader.ContentType] != null && !(httpResponse.WebResponse.Headers[System.Net.HttpResponseHeader.ContentType].Contains("text/") | httpResponse.WebResponse.Headers[System.Net.HttpResponseHeader.ContentType].Contains("/xml") | httpResponse.WebResponse.Headers[System.Net.HttpResponseHeader.ContentType].Contains("/json") | httpResponse.WebResponse.Headers[System.Net.HttpResponseHeader.ContentType].Contains("application/")))
        {
          if (httpResponse.WebResponse.Headers[System.Net.HttpResponseHeader.ContentType].Contains("image/"))
          {
            if (this.GetImage)
              httpResponse.Image = Image.FromStream(Response.GetResponseStream());
          }
          else
            Debug.Print("Unknown content type: " + httpResponse.WebResponse.Headers[System.Net.HttpResponseHeader.ContentType]);
        }
        else
        {
          if (httpResponse.WebResponse != null)
          {
            httpResponse.Html = HttpUtility.HtmlDecode(this.ProcessResponse(httpResponse.WebResponse));
            this._LastResponseUri = Uri;
          }
          if (this.AutoRedirect && httpResponse.Html.ToLower().Contains("<meta http-equiv=\"refresh"))
          {
            Uri = this.ParseMetaRefreshUrl(httpResponse.Html);
            Debug.Print("Before meta: " + Uri);
            if (!this.IsValidUri(Uri))
            {
              Uri = Conversions.ToString(Operators.ConcatenateObject((object) Url, Interaction.IIf(Uri.StartsWith("/"), (object) Uri, (object) ("/" + Uri))));
              Debug.Print("After meta: " + Uri);
              Debug.Print("");
            }
            bool flag2 = true;
            List<string>.Enumerator enumerator2;
            try
            {
              enumerator2 = this.RedirectBlacklist.GetEnumerator();
              while (enumerator2.MoveNext())
              {
                string current = enumerator2.Current;
                if (Uri.ToLower().Contains(current.ToLower()))
                {
                  flag2 = false;
                  break;
                }
              }
            }
            finally
            {
              enumerator2.Dispose();
            }
            PostData = string.Empty;
            if (flag2)
              goto label_1;
          }
        }
      }
      catch (WebException ex)
      {
        ProjectData.SetProjectError((Exception) ex);
        WebException webException = ex;
        httpResponse.Exception = (object) webException;
        if (webException.Response != null)
        {
          httpResponse.WebResponse = (HttpWebResponse) webException.Response;
          httpResponse.Html = this.ProcessResponse(httpResponse.WebResponse);
        }
        ProjectData.ClearProjectError();
      }
      catch (Exception ex)
      {
        ProjectData.SetProjectError(ex);
        Exception exception = ex;
        httpResponse.Exception = (object) exception;
        ProjectData.ClearProjectError();
      }
      finally
      {
        this.Request = (HttpWebRequest) null;
        httpWebResponse = (HttpWebResponse) null;
        this.Referer = string.Empty;
      }
      return httpResponse;
    }