[C#HttpHelper]類1.4正式版教程與升級報告
導讀html
1.升級報告數組
2.HttpHelper1.4正式版下載瀏覽器
3.HttpHelper類使用方法,安全
4.最簡單的Post與Get的寫法服務器
5.HttpHelper設置Header參考的方法cookie
6.HttpHelper獲取圖片的方式app
7.二次或屢次使用Cookie的方式ide
8.使用URL編碼的方式
1.升級報告工具
1.方法 中不要使用類new object(){name=""};這樣的方法,由於net2.0不支持
2. if (response.StatusCode != null)無效,一直爲True ReEnter反映 post
if (response.StatusCode != null)無效,一直爲True ReEnter反映 { result.StatusCode = response.StatusCode; result.StatusDescription = response.StatusDescription; }
解決辦法
if (response != null)無效,一直爲True ReEnter反映 { result.StatusCode = response.StatusCode; result.StatusDescription = response.StatusDescription; }無效,一直爲True ReEnter反映
3.提出的問題
添加屬性ProtocolVersion 獲取或設置用於請求的 HTTP 版本。返回結果:用於請求的 HTTP 版本。默認爲 System.Net.HttpVersion.Version11。
4. 解決的問題須要添加屬性
添加屬性Expect100Continue獲取或設置一個 System.Boolean 值,該值肯定是否使用 100-Continue 行爲。若是 POST 請求須要 100-Continue 響應,則爲 true;不然爲 false。默認值爲 true。
4.多個證書添加的問題
添加屬性ClentCertificates 能夠添加多個證書文件
5.ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;設置安全協議的版本。
增長到項目中,可直接設置屬性 SecurityProtocolType
6.
Match meta = Regex.Match(Encoding.Default.GetString(RawResponse), "<meta([^<]*)charset=([^<]*)[\"']", RegexOptions.IgnoreCase); string charter = (meta.Groups.Count > 2) ? meta.Groups[2].Value.ToLower() : string.Empty;
應該是meta.Groups.Count > 1
7.post數據添加自由設置編碼 只要設置屬性HttpItem.PostEncoding就能夠了
8.服務器提交了協議衝突. Section=ResponseHeader Detail=標頭名稱無效
9.增長Url編碼與解碼方法
/// <summary> /// 根據指定的編碼對RUl進行解碼 /// </summary> /// <param name="text">要解碼的字符串</param> /// <param name="encoding">要進行解碼的編碼方式</param> /// <returns></returns> public static string URLDecode(string text, Encoding encoding) { return HttpUtility.UrlDecode(text, encoding); } /// <summary> /// 根據指定的編碼對URL進行編碼 /// </summary> /// <param name="text">要編碼的URL</param> /// <param name="encoding">要進行編碼的編碼方式</param> /// <returns></returns> public static string URLEncode(string text, Encoding encoding) { return HttpUtility.UrlEncode(text, encoding); }
2.HttpHelper1.4正式版下載
下載地址:http://www.sufeinet.com/thread-3-1-1.html
在線測試工具:http://www.sufeinet.com/thread-3690-1-1.html
專用Cookie分析類:http://www.sufeinet.com/thread-2382-1-1.html
要學習更多關於C# Http的請是看個人文章 C# HttpWebRequest 絕技
代碼以下所示
/// <summary> /// 類說明:HttpHelper類,用來實現Http訪問,Post或者Get方式的,直接訪問,帶Cookie的,帶證書的等方式,能夠設置代理 /// 重要提示:請不要自行修改本類,若是由於你本身修改後將沒法升級到新版本。若是確實有什麼問題請到官方網站提建議, /// 咱們必定會及時修改 /// 編碼日期:2011-09-20 /// 編 碼 人:蘇飛 /// 聯繫方式:361983679 /// 官方網址:http://www.sufeinet.com/thread-3-1-1.html /// 修改日期:2013-07-02 /// </summary> using System; using System.Collections.Generic; using System.Text; using System.Net; using System.IO; using System.Text.RegularExpressions; using System.IO.Compression; using System.Security.Cryptography.X509Certificates; using System.Net.Security; using System.Web; namespace DotNet.Utilities { /// <summary> /// Http鏈接操做幫助類 /// </summary> public class HttpHelper { #region 預約義方法或者變動 //默認的編碼 private Encoding encoding = Encoding.Default; //Post數據編碼 private Encoding postencoding = Encoding.Default; //HttpWebRequest對象用來發起請求 private HttpWebRequest request = null; //獲取影響流的數據對象 private HttpWebResponse response = null; /// <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() { Cookie = "", Header = null, Html = ex.Message, 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; } /// <summary> /// 4.0如下.net版本取數據使用 /// </summary> /// <param name="streamResponse">流</param> private static MemoryStream GetMemoryStream(Stream streamResponse) { MemoryStream _stream = new MemoryStream(); int Length = 256; Byte[] buffer = new Byte[Length]; int bytesRead = streamResponse.Read(buffer, 0, Length); while (bytesRead > 0) { _stream.Write(buffer, 0, bytesRead); bytesRead = streamResponse.Read(buffer, 0, Length); } return _stream; } /// <summary> /// 爲請求準備參數 /// </summary> ///<param name="objhttpItem">參數列表</param> private void SetRequest(HttpItem objhttpItem) { //設置安全協議 ServicePointManager.SecurityProtocol = objhttpItem.SecurityProtocolType; // 驗證證書 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> /// 設置證書 /// </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> private void SetCerList(HttpItem objhttpItem) { if (objhttpItem.ClentCertificates != null && objhttpItem.ClentCertificates.Count > 0) { foreach (X509Certificate item in objhttpItem.ClentCertificates) { request.ClientCertificates.Add(item); } } } /// <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> /// 設置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> /// 回調驗證證書問題 /// </summary> /// <param name="sender">流對象</param> /// <param name="certificate">證書</param> /// <param name="chain">X509Chain</param> /// <param name="errors">SslPolicyErrors</param> /// <returns>bool</returns> public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; } #endregion #region 公共方法 /// <summary> /// 根據指定的編碼對RUl進行解碼 /// </summary> /// <param name="text">要解碼的字符串</param> /// <param name="encoding">要進行解碼的編碼方式</param> /// <returns></returns> public string URLDecode(string text, Encoding encoding) { return HttpUtility.UrlDecode(text, encoding); } /// <summary> /// 根據指定的編碼對URL進行編碼 /// </summary> /// <param name="text">要編碼的URL</param> /// <param name="encoding">要進行編碼的編碼方式</param> /// <returns></returns> public string URLEncode(string text, Encoding encoding) { return HttpUtility.UrlEncode(text, encoding); } #endregion } /// <summary> /// Http請求參考類 /// </summary> public class HttpItem { string _URL = string.Empty; /// <summary> /// 請求URL必須填寫 /// </summary> public string URL { get { return _URL; } set { _URL = value; } } string _Method = "GET"; /// <summary> /// 請求方式默認爲GET方式,當爲POST方式時必須設置Postdata的值 /// </summary> public string Method { get { return _Method; } set { _Method = value; } } int _Timeout = 100000; /// <summary> /// 默認請求超時時間 /// </summary> public int Timeout { get { return _Timeout; } set { _Timeout = value; } } int _ReadWriteTimeout = 30000; /// <summary> /// 默認寫入Post數據超時間 /// </summary> public int ReadWriteTimeout { get { return _ReadWriteTimeout; } set { _ReadWriteTimeout = value; } } string _Accept = "text/html, application/xhtml+xml, */*"; /// <summary> /// 請求標頭值 默認爲text/html, application/xhtml+xml, */* /// </summary> public string Accept { get { return _Accept; } set { _Accept = value; } } string _ContentType = "text/html"; /// <summary> /// 請求返回類型默認 text/html /// </summary> public string ContentType { get { return _ContentType; } set { _ContentType = value; } } string _UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"; /// <summary> /// 客戶端訪問信息默認Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0) /// </summary> public string UserAgent { get { return _UserAgent; } set { _UserAgent = value; } } Encoding _Encoding = null; /// <summary> /// 返回數據編碼默認爲NUll,能夠自動識別,通常爲utf-8,gbk,gb2312 /// </summary> public Encoding Encoding { get { return _Encoding; } set { _Encoding = value; } } private PostDataType _PostDataType = PostDataType.String; /// <summary> /// Post的數據類型 /// </summary> public PostDataType PostDataType { get { return _PostDataType; } set { _PostDataType = value; } } string _Postdata = string.Empty; /// <summary> /// Post請求時要發送的字符串Post數據 /// </summary> public string Postdata { get { return _Postdata; } set { _Postdata = value; } } private byte[] _PostdataByte = null; /// <summary> /// Post請求時要發送的Byte類型的Post數據 /// </summary> public byte[] PostdataByte { get { return _PostdataByte; } set { _PostdataByte = value; } } CookieCollection cookiecollection = null; /// <summary> /// Cookie對象集合 /// </summary> public CookieCollection CookieCollection { get { return cookiecollection; } set { cookiecollection = value; } } string _Cookie = string.Empty; /// <summary> /// 請求時的Cookie /// </summary> public string Cookie { get { return _Cookie; } set { _Cookie = value; } } string _Referer = string.Empty; /// <summary> /// 來源地址,上次訪問地址 /// </summary> public string Referer { get { return _Referer; } set { _Referer = value; } } string _CerPath = string.Empty; /// <summary> /// 證書絕對路徑 /// </summary> public string CerPath { get { return _CerPath; } set { _CerPath = value; } } private Boolean isToLower = false; /// <summary> /// 是否設置爲全文小寫,默認爲不轉化 /// </summary> public Boolean IsToLower { get { return isToLower; } set { isToLower = value; } } private Boolean allowautoredirect = false; /// <summary> /// 支持跳轉頁面,查詢結果將是跳轉後的頁面,默認是不跳轉 /// </summary> public Boolean Allowautoredirect { get { return allowautoredirect; } set { allowautoredirect = value; } } private int connectionlimit = 1024; /// <summary> /// 最大鏈接數 /// </summary> public int Connectionlimit { get { return connectionlimit; } set { connectionlimit = value; } } private string proxyusername = string.Empty; /// <summary> /// 代理Proxy 服務器用戶名 /// </summary> public string ProxyUserName { get { return proxyusername; } set { proxyusername = value; } } private string proxypwd = string.Empty; /// <summary> /// 代理 服務器密碼 /// </summary> public string ProxyPwd { get { return proxypwd; } set { proxypwd = value; } } private string proxyip = string.Empty; /// <summary> /// 代理 服務IP /// </summary> public string ProxyIp { get { return proxyip; } set { proxyip = value; } } private ResultType resulttype = ResultType.String; /// <summary> /// 設置返回類型String和Byte /// </summary> public ResultType ResultType { get { return resulttype; } set { resulttype = value; } } private WebHeaderCollection header = new WebHeaderCollection(); /// <summary> /// header對象 /// </summary> public WebHeaderCollection Header { get { return header; } set { header = value; } } /// <summary> // 獲取或設置用於請求的 HTTP 版本。返回結果:用於請求的 HTTP 版本。默認爲 System.Net.HttpVersion.Version11。 /// </summary> public Version ProtocolVersion { get; set; } private Boolean _expect100continue = true; /// <summary> /// 獲取或設置一個 System.Boolean 值,該值肯定是否使用 100-Continue 行爲。若是 POST 請求須要 100-Continue 響應,則爲 true;不然爲 false。默認值爲 true。 /// </summary> public Boolean Expect100Continue { get { return _expect100continue; } set { _expect100continue = value; } } /// <summary> /// 設置509證書集合 /// </summary> public X509CertificateCollection ClentCertificates { get; set; } /// <summary> /// 指定Schannel安全包支持的安全協議 /// </summary> public SecurityProtocolType SecurityProtocolType { get; set; } /// <summary> /// 設置或獲取Post參數編碼,默認的爲Default編碼 /// </summary> public Encoding PostEncoding { get; set; } } /// <summary> /// Http返回參數類 /// </summary> public class HttpResult { /// <summary> /// Http請求返回的Cookie /// </summary> public string Cookie { get; set; } /// <summary> /// Cookie對象集合 /// </summary> public CookieCollection CookieCollection { get; set; } /// <summary> /// 返回的String類型數據 只有ResultType.String時才返回數據,其它狀況爲空 /// </summary> public string Html { get; set; } /// <summary> /// 返回的Byte數組 只有ResultType.Byte時才返回數據,其它狀況爲空 /// </summary> public byte[] ResultByte { get; set; } /// <summary> /// header對象 /// </summary> public WebHeaderCollection Header { get; set; } /// <summary> /// 返回狀態說明 /// </summary> public string StatusDescription { get; set; } /// <summary> /// 返回狀態碼,默認爲OK /// </summary> public HttpStatusCode StatusCode { get; set; } } /// <summary> /// 返回類型 /// </summary> public enum ResultType { /// <summary> /// 表示只返回字符串 只有Html有數據 /// </summary> String, /// <summary> /// 表示返回字符串和字節流 ResultByte和Html都有數據返回 /// </summary> Byte } /// <summary> /// Post的數據格式默認爲string /// </summary> public enum PostDataType { /// <summary> /// 字符串類型,這時編碼Encoding可不設置 /// </summary> String, /// <summary> /// Byte類型,須要設置PostdataByte參數的值編碼Encoding可設置爲空 /// </summary> Byte, /// <summary> /// 傳文件,Postdata必須設置爲文件的絕對路徑,必須設置Encoding的值 /// </summary> FilePath } }
3.HttpHelper類使用方法,
HttpHelper http = new HttpHelper(); HttpItem item = new HttpItem() { URL = "http://www.sufeinet.com",//URL 必需項 Encoding = null,//編碼格式(utf-8,gb2312,gbk) 可選項 默認類會自動識別 //Encoding = Encoding.Default, Method = "get",//URL 可選項 默認爲Get Timeout = 100000,//鏈接超時時間 可選項默認爲100000 ReadWriteTimeout = 30000,//寫入Post數據超時時間 可選項默認爲30000 IsToLower = false,//獲得的HTML代碼是否轉成小寫 可選項默認轉小寫 Cookie = "",//字符串Cookie 可選項 UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",//用戶的瀏覽器類型,版本,操做系統 可選項有默認值 Accept = "text/html, application/xhtml+xml, */*",// 可選項有默認值 ContentType = "text/html",//返回類型 可選項有默認值 Referer = "http://www.sufeinet.com",//來源URL 可選項 }; item.Header.Add("測試Key1", "測試Value1"); item.Header.Add("測試Key2", "測試Value2"); //獲得HTML代碼 HttpResult result = http.GetHtml(item); //取出返回的Cookie string cookie = result.Cookie; //返回的Html內容 string html = result.Html; if (result.StatusCode == System.Net.HttpStatusCode.OK) { //表示訪問成功,具體的你們就參考HttpStatusCode類 } //表示StatusCode的文字說明與描述 string statusCodeDescription = result.StatusDescription;
本例子是一個統一的標準寫法,你們能夠根據須要自行修改,在下面還會有對特定功能的說明請你們繼續看
4.最簡單的Post與Get的寫法
HttpHelper http = new HttpHelper(); HttpItem item = new HttpItem() { URL = "http://www.sufeinet.com",//URL這裏都是測試 必需項 Method = "get",//URL 可選項 默認爲Get }; //獲得HTML代碼 HttpResult result = http.GetHtml(item); item = new HttpItem() { URL = "http://tool.sufeinet.com",//URL這裏都是測試URl 必需項 Encoding = null,//編碼格式(utf-8,gb2312,gbk) 可選項 默認類會自動識別 //Encoding = Encoding.Default, Method = "post",//URL 可選項 默認爲Get Postdata = "user=123123&pwd=1231313" }; //獲得新的HTML代碼 result = http.GetHtml(item);
5.HttpHelper設置Header參考的方法
HttpHelper http = new HttpHelper(); HttpItem item = new HttpItem() { URL = "http://www.sufeinet.com",//URL 必需項 Encoding = null,//編碼格式(utf-8,gb2312,gbk) 可選項 默認類會自動識別 //Encoding = Encoding.Default, Method = "get",//URL 可選項 默認爲Get }; item.Header.Add("測試Key1", "測試Value1"); item.Header.Add("測試Key2", "測試Value2"); //獲得HTML代碼 HttpResult result = http.GetHtml(item); //取出返回的Cookie string cookie = result.Cookie; //返回的Html內容 string html = result.Html; if (result.StatusCode == System.Net.HttpStatusCode.OK) { //表示訪問成功,具體的你們就參考HttpStatusCode類 } //表示StatusCode的文字說明與描述 string statusCodeDescription = result.StatusDescription;
下面我列出一些不能直接Add的參數
6.HttpHelper獲取圖片的方式
HttpHelper http = new HttpHelper(); HttpItem item = new HttpItem() { URL = "http://www.sufeinet.com",//URL 必需項 Encoding = null,//編碼格式(utf-8,gb2312,gbk) 可選項 默認類會自動識別 //Encoding = Encoding.Default, ResultType = ResultType.Byte }; //獲得HTML代碼 HttpResult result = http.GetHtml(item); if (result.StatusCode == System.Net.HttpStatusCode.OK) { //表示訪問成功,具體的你們就參考HttpStatusCode類 } //表示StatusCode的文字說明與描述 string statusCodeDescription = result.StatusDescription; //把獲得的Byte轉成圖片 Image img = byteArrayToImage(result.ResultByte); } /// <summary> /// 字節數組生成圖片 /// </summary> /// <param name="Bytes">字節數組</param> /// <returns>圖片</returns> private Image byteArrayToImage(byte[] Bytes) { using (MemoryStream ms = new MemoryStream(Bytes)) { Image outputImg = Image.FromStream(ms); return outputImg; } }
7.二次或屢次使用Cookie的方式
HttpHelper http = new HttpHelper(); HttpItem item = new HttpItem() { URL = "http://www.sufeinet.com",//URL這裏都是測試 必需項 Encoding = null,//編碼格式(utf-8,gb2312,gbk) 可選項 默認類會自動識別 //Encoding = Encoding.Default, Method = "get",//URL 可選項 默認爲Get }; //獲得HTML代碼 HttpResult result = http.GetHtml(item); item = new HttpItem() { URL = "http://tool.sufeinet.com",//URL這裏都是測試URl 必需項 Encoding = null,//編碼格式(utf-8,gb2312,gbk) 可選項 默認類會自動識別 //Encoding = Encoding.Default, Method = "get",//URL 可選項 默認爲Get Cookie = result.Cookie, }; //獲得新的HTML代碼 result = http.GetHtml(item);
8.使用URL編碼的方式
HttpHelper http = new HttpHelper(); HttpItem item = new HttpItem() { URL = http.URLEncode("http://www.sufeinet.com", Encoding.UTF8),//URL編碼 }; HttpResult result = http.GetHtml(item); string html = result.Html; string url = http.URLDecode(html, Encoding.UTF8);//URL解碼 string cookie = result.Cookie;
好了就這些,具體的你們能夠在使用中提出來,我盡力爲你們解決
若是您感受還能夠的話就給個評論或者是推薦吧。