예제 #1
0
 /// <summary>
 /// Ϊ����׼������
 /// </summary>
 ///<param name="objhttpItem">�����б�</param>
 private void SetRequest(HttpItem objhttpItem)
 {
     // ��֤֤��
     SetCer(objhttpItem);
     //����Header����
     if (objhttpItem.Header != null && objhttpItem.Header.Count > 0) foreach (string item in objhttpItem.Header.AllKeys)
         {
             request.Headers.Add(item, objhttpItem.Header[item]);
         }
     // ����
     SetProxy(objhttpItem);
     if (objhttpItem.ProtocolVersion != null) request.ProtocolVersion = objhttpItem.ProtocolVersion;
     request.ServicePoint.Expect100Continue = objhttpItem.Expect100Continue;
     //����ʽGet����Post
     request.Method = objhttpItem.Method;
     request.Timeout = objhttpItem.Timeout;
     request.ReadWriteTimeout = objhttpItem.ReadWriteTimeout;
     //Accept
     request.Accept = objhttpItem.Accept;
     //ContentType��������
     request.ContentType = objhttpItem.ContentType;
     //UserAgent�ͻ��˵ķ������ͣ�����������汾�Ͳ���ϵͳ��Ϣ
     request.UserAgent = objhttpItem.UserAgent;
     // ����
     encoding = objhttpItem.Encoding;
     //����Cookie
     SetCookie(objhttpItem);
     //��Դ��ַ
     request.Referer = objhttpItem.Referer;
     //�Ƿ�ִ����ת����
     request.AllowAutoRedirect = objhttpItem.Allowautoredirect;
     //����Post����
     SetPostData(objhttpItem);
     //�����������
     if (objhttpItem.Connectionlimit > 0) request.ServicePoint.ConnectionLimit = objhttpItem.Connectionlimit;
 }
예제 #2
0
 /// <summary>
 /// ����Post����
 /// </summary>
 /// <param name="objhttpItem">Http����</param>
 private void SetPostData(HttpItem objhttpItem)
 {
     //��֤�ڵõ����ʱ�Ƿ��д�������
     if (request.Method.Trim().ToLower().Contains("post"))
     {
         if (objhttpItem.PostEncoding != null)
         {
             postencoding = objhttpItem.PostEncoding;
         }
         byte[] buffer = null;
         //�Byte����
         if (objhttpItem.PostDataType == PostDataType.Byte && objhttpItem.PostdataByte != null && objhttpItem.PostdataByte.Length > 0)
         {
             //��֤�ڵõ����ʱ�Ƿ��д�������
             buffer = objhttpItem.PostdataByte;
         }//д���ļ�
         else if (objhttpItem.PostDataType == PostDataType.FilePath && !string.IsNullOrEmpty(objhttpItem.Postdata))
         {
             StreamReader r = new StreamReader(objhttpItem.Postdata, postencoding);
             buffer = postencoding.GetBytes(r.ReadToEnd());
             r.Close();
         } //д���ַ���
         else if (!string.IsNullOrEmpty(objhttpItem.Postdata))
         {
             buffer = postencoding.GetBytes(objhttpItem.Postdata);
         }
         if (buffer != null)
         {
             request.ContentLength = buffer.Length;
             request.GetRequestStream().Write(buffer, 0, buffer.Length);
         }
     }
 }
예제 #3
0
 /// <summary>
 /// ����
 /// </summary>
 /// <param name="objhttpItem">��������</param>
 private void SetProxy(HttpItem objhttpItem)
 {
     if (!string.IsNullOrEmpty(objhttpItem.ProxyIp))
     {
         //���������
         if (objhttpItem.ProxyIp.Contains(":"))
         {
             string[] plist = objhttpItem.ProxyIp.Split(':');
             WebProxy myProxy = new WebProxy(plist[0].Trim(), Convert.ToInt32(plist[1].Trim()));
             //��������
             myProxy.Credentials = new NetworkCredential(objhttpItem.ProxyUserName, objhttpItem.ProxyPwd);
             //����ǰ�������
             request.Proxy = myProxy;
         }
         else
         {
             WebProxy myProxy = new WebProxy(objhttpItem.ProxyIp, false);
             //��������
             myProxy.Credentials = new NetworkCredential(objhttpItem.ProxyUserName, objhttpItem.ProxyPwd);
             //����ǰ�������
             request.Proxy = myProxy;
         }
         //���ð�ȫƾ֤
         request.Credentials = CredentialCache.DefaultNetworkCredentials;
     }
 }
예제 #4
0
 /// <summary>
 /// ����Cookie
 /// </summary>
 /// <param name="objhttpItem">Http����</param>
 private void SetCookie(HttpItem objhttpItem)
 {
     if (!string.IsNullOrEmpty(objhttpItem.Cookie))
         //Cookie
         request.Headers[HttpRequestHeader.Cookie] = objhttpItem.Cookie;
     //����Cookie
     if (objhttpItem.CookieCollection != null)
     {
         request.CookieContainer = new CookieContainer();
         request.CookieContainer.Add(objhttpItem.CookieCollection);
     }
 }
예제 #5
0
 /// <summary>
 /// ���ö��֤��
 /// </summary>
 /// <param name="objhttpItem"></param>
 private void SetCerList(HttpItem objhttpItem)
 {
     if (objhttpItem.ClentCertificates != null && objhttpItem.ClentCertificates.Count > 0)
     {
         foreach (X509Certificate item in objhttpItem.ClentCertificates)
         {
             request.ClientCertificates.Add(item);
         }
     }
 }
예제 #6
0
 /// <summary>
 /// ����֤��
 /// </summary>
 /// <param name="objhttpItem"></param>
 private void SetCer(HttpItem objhttpItem)
 {
     if (!string.IsNullOrEmpty(objhttpItem.CerPath))
     {
         //��һ��һ��Ҫд�ڴ������ӵ�ǰ�档ʹ�ûص��ķ�������֤����֤��
         ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
         //��ʼ�����񣬲����������URL��ַ
         request = (HttpWebRequest)WebRequest.Create(objhttpItem.URL);
         SetCerList(objhttpItem);
         //��֤����ӵ�������
         request.ClientCertificates.Add(new X509Certificate(objhttpItem.CerPath));
     }
     else
     {
         //��ʼ�����񣬲����������URL��ַ
         request = (HttpWebRequest)WebRequest.Create(objhttpItem.URL);
         SetCerList(objhttpItem);
     }
 }
예제 #7
0
 /// <summary>
 /// �����ഫ������ݣ��õ���Ӧҳ������
 /// </summary>
 /// <param name="objhttpitem">���������</param>
 /// <returns>����HttpResult����</returns>
 public HttpResult GetHtml(HttpItem objhttpitem)
 {
     //���ز���
     HttpResult result = new HttpResult();
     try
     {
         //׼������
         SetRequest(objhttpitem);
     }
     catch (Exception ex)
     {
         result = new HttpResult();
         result.Cookie = string.Empty;
         result.Header = null;
         result.Html = ex.Message;
         result.StatusDescription = "���ò���ʱ�����" + ex.Message;
         return result;
     }
     try
     {
         #region �õ������response
         using (response = (HttpWebResponse)request.GetResponse())
         {
             result.StatusCode = response.StatusCode;
             result.StatusDescription = response.StatusDescription;
             result.Header = response.Headers;
             if (response.Cookies != null) result.CookieCollection = response.Cookies;
             if (response.Headers["set-cookie"] != null) result.Cookie = response.Headers["set-cookie"];
             MemoryStream _stream = new MemoryStream();
             //GZIIP����
             if (response.ContentEncoding != null && response.ContentEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase))
             {
                 //��ʼ��ȡ�������ñ��뷽ʽ
                 //new GZipStream(response.GetResponseStream(), CompressionMode.Decompress).CopyTo(_stream, 10240);
                 //.net4.0�����
                 _stream = GetMemoryStream(new GZipStream(response.GetResponseStream(), CompressionMode.Decompress));
             }
             else
             {
                 //��ʼ��ȡ�������ñ��뷽ʽ
                 //response.GetResponseStream().CopyTo(_stream, 10240);
                 //.net4.0�����
                 _stream = GetMemoryStream(response.GetResponseStream());
             }
             //��ȡByte
             byte[] RawResponse = _stream.ToArray();
             _stream.Close();
             //�Ƿ񷵻�Byte��������
             if (objhttpitem.ResultType == ResultType.Byte) result.ResultByte = RawResponse;
             //�����↑ʼ����Ҫ���ӱ�����
             if (encoding == null)
             {
                 Match meta = Regex.Match(Encoding.Default.GetString(RawResponse), "<meta([^<]*)charset=([^<]*)[\"']", RegexOptions.IgnoreCase);
                 string charter = (meta.Groups.Count > 1) ? meta.Groups[2].Value.ToLower() : string.Empty;
                 if (charter.Length > 2)
                     encoding = Encoding.GetEncoding(charter.Trim().Replace("\"", "").Replace("'", "").Replace(";", "").Replace("iso-8859-1", "gbk"));
                 else
                 {
                     if (string.IsNullOrEmpty(response.CharacterSet)) encoding = Encoding.UTF8;
                     else encoding = Encoding.GetEncoding(response.CharacterSet);
                 }
             }
             //�õ����ص�HTML
             result.Html = encoding.GetString(RawResponse);
         }
         #endregion
     }
     catch (WebException ex)
     {
         //�������ڷ����쳣ʱ���صĴ�����Ϣ
         response = (HttpWebResponse)ex.Response;
         result.Html = ex.Message;
         if (response != null)
         {
             result.StatusCode = response.StatusCode;
             result.StatusDescription = response.StatusDescription;
         }
     }
     catch (Exception ex)
     {
         result.Html = ex.Message;
     }
     if (objhttpitem.IsToLower) result.Html = result.Html.ToLower();
     return result;
 }
예제 #8
0
 public VersionUpdate()
 {
     httpItem = new HttpItem();
     httpHelper = new HttpHelper();
 }