-
大小: 10KB文件類型: .cs金幣: 1下載: 0 次發布日期: 2021-05-16
- 語言: C#
- 標簽: C#??httphelper??
資源簡介
C#是沒有自帶HttpHelper的,這個是別人編寫的一個叫HttpHelper的類來的。
HttpHelper.cs(httpwebrequest訪問網站助手)
1.提供比較全面的網站表單提交,上傳下載等功能。
2.能夠保存cookie,實現網站操作需要登錄的情況。
3.方便實現模擬登錄,保存cookie。
4.代碼規范易懂,使用方便。
代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.Text;
using?System.Net;
using?System.IO;
using?System.Threading;
namespace?WebQQ2.DLL
{
????///?
????///?2011-1-5
????///?by?hackren
????///?Email:hackren@vip.qq.com
????///?
????public?class?HttpHelper
????{
????????#region?變量定義
????????private??CookieContainer?cc?=?new?CookieContainer();
????????private??string?contentType?=?“application/x-www-form-urlencoded“;
????????private??string?accept?=?“*/*“;
????????private??string?userAgent?=?“Mozilla/4.0?(compatible;?MSIE?7.0;?Windows?NT?5.1;?.NET?CLR?2.0.50727;?.NET?CLR?3.0.04506.648;?.NET?CLR?3.5.21022)“;
????????private??Encoding?encoding?=?Encoding.GetEncoding(“utf-8“);
????????private??int?maxTry?=?300;
????????private??int?currentTry?=?0;
????????public??IWebProxy?Proxy;
????????#endregion
????????#region?變量賦值
????????///??
????????///?Cookie
????????///? ?
????????public??CookieContainer?CookieContainer
????????{
????????????get
????????????{
????????????????return?cc;
????????????}
????????}
????????///??
????????///?語言
????????///? ?
????????///? ?
????????public??Encoding?Encoding
????????{
????????????get
????????????{
????????????????return?encoding;
????????????}
????????????set
????????????{
????????????????encoding?=?value;
????????????}
????????}
????????public??int?NetworkDelay
????????{
????????????get
????????????{
????????????????Random?r?=?new?Random();
????????????????return?(r.Next(10?600?));
????????????}
????????}
????????public??int?MaxTry
????????{
????????????get
????????????{
????????????????return?maxTry;
????????????}
????????????set
????????????{
????????????????maxTry?=?value;
????????????}
????????}
????????#endregion
????????#region?獲取HTML
????????///?
????????///?獲取HTML
????????///?
????????///?地址
????????///?post?提交的字符串
????????///?是否是post
????????///?CookieContainer
????????///?html?
????????public??string?GetHtml(string?url?string?postData?bool?isPost?CookieContainer?cookieContainerstring?refurl)
????????{
????????????ServicePointManager.Expect100Continue?=?false;
????????????if?(string.IsNullOrEmpty(postData))
????????????{
????????????????return?GetHtml(url?cookieContainer);
????????????}
????????????Thread.Sleep(NetworkDelay);//等待
????????????currentTry++;
????????????HttpWebRequest?httpWebRequest?=?null;
???????????
????????????HttpWebResponse?httpWebResponse?=?null;
????????????try
????????????{
????????????????byte[]?byteRequest?=?Encoding.Default.GetBytes(postData);
????????????????httpWebRequest?=?(HttpWebRequest)HttpWebRequest.Create(url);
????????????????if?(Proxy?!=?null)
????????????????????httpWebRequest.Proxy?=?Proxy;
????????????????httpWebR
評論
共有 條評論