[C#] 纯文本查看 复制代码
private HttpHelp _hp= new HttpHelp();
public HttpHelp HP
{
get { return this._hp; }
set { this._hp = value; }
}
private bool isReady() //这个是获取2个参数的,一个BAIDUID,一个Token,前者是cookie,后者是一个提交参数
{
try
{
this.HP = new HttpHelp();
//拿到BAIDUID
this.HP.GetHtml("http://www.baidu.com/", true);
//得到token
string html = this.HP.GetHtml("https://passport.baidu.com/v2/api/?getapi&tpl=mn&apiver=v3&tt=1385512949190&class=login&logintype=dialogLogin&callback=bd__cbs__j3jwk9", true);
string pattern = "(?<=\"token\" : \")\\S+?(?=\")";
this.Token = MatchHelp.GetString(html, pattern);
if (!string.IsNullOrEmpty(this.Token))
{
this.tokenDone = true;
return true;
}
else
{
return false;
}
}
catch
{
return false;
}
}
[C#] 纯文本查看 复制代码 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Web;
namespace SuperBlockServer
{
public class HttpHelp
{
private CookieCollection mycookie = new CookieCollection();
//private string mycookie;
public string GetHtml(string url,bool saveCookie)
{
HttpHelper http=new HttpHelper();
HttpItem item = new HttpItem()
{
URL = url,//必需项
Timeout = 15000,//30秒超时
Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
Encoding = null,//编码格式(utf-8,gb2312,gbk) 可选项 默认类会自动识别
Method = "get",
CookieCollection = this.mycookie,//使用Cookie
UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36",
//ProxyIp = "127.0.0.1:8888",
ResultCookieType = ResultCookieType.CookieCollection
};
HttpResult result = http.GetHtml(item);
string content = result.Html;
this.mycookie.Add(result.CookieCollection);//存储Cookie
//this.mycookie = result.Cookie;
return content;
}
public string PostHtml(string url, string postStr, bool saveCookie)
{
HttpHelper http = new HttpHelper();
HttpItem item = new HttpItem()
{
URL = url,//必需项
Timeout = 15000,//30秒超时
Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
Encoding = null,//编码格式(utf-8,gb2312,gbk) 可选项 默认类会自动识别
Method = "post",
UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36",
ContentType = "application/x-www-form-urlencoded",
Postdata=postStr,
//CookieCollection = this.mycookie,//使用Cookie
ProxyIp = "127.0.0.1:8888",
ResultCookieType = ResultCookieType.CookieCollection
};
HttpResult result = http.GetHtml(item);
string content = result.Html;
if (result.StatusCode == System.Net.HttpStatusCode.OK)
{
if (saveCookie)
{
this.mycookie.Add(result.CookieCollection);//存储Cookie
}
return content;
}
else
{
return null;
}
}
}
}
|