/// <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; }
/// <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); } } }
/// <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; } }
/// <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); } }
/// <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); } } }
/// <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); } }
/// <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; }
public VersionUpdate() { httpItem = new HttpItem(); httpHelper = new HttpHelper(); }