|
发表于 2013-7-18 14:50:16
|
显示全部楼层
sufei你好,根据你的这种做法,在登陆php的网站,如你说的赶集网等是没有问题的,但是在登陆www.cnblogs.com(博客园),asp.net的网站的时候,为什么会不能成功登陆呢?代码如下:
[code=csharp] //实例化一个http链接辅助类
HttpHelper helper = new HttpHelper();
HttpItem items = new HttpItem()
{
URL = "http://passport.cnblogs.com/login.aspx",//URL 必需项
//Encoding = null,//编码格式(utf-8,gb2312,gbk) 可选项 默认类会自动识别
Encoding = Encoding.UTF8,
Method = "post",//URL 可选项 默认为Get
Postdata = "__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=%2FwEPDwULLTE1MzYzODg2NzZkGAEFHl9fQ29udHJvbHNSZXF1aXJlUG9zdEJhY2tLZXlfXxYBBQtjaGtSZW1lbWJlcm1QYDyKKI9af4b67Mzq2xFaL9Bt&__EVENTVALIDATION=%2FwEdAAUyDI6H%2Fs9f%2BZALqNAA4PyUhI6Xi65hwcQ8%2FQoQCF8JIahXufbhIqPmwKf992GTkd0wq1PKp6%2B%2F1yNGng6H71Uxop4oRunf14dz2Zt2%2BQKDEIYpifFQj3yQiLk3eeHVQqcjiaAP&tbUserName=hanxue517&tbPassword=dbswsn517&btnLogin=%E7%99%BB++%E5%BD%95&txtReturnUrl=http%3A%2F%2Fhome.cnblogs.com%2F",
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,application/xml;q=0.9,*/*;q=0.8",// 可选项有默认值
ContentType = "text/html",//返回类型 可选项有默认值
Referer = "http://www.baidu.com",//来源URL 可选项};
};
// WebHeaderCollection webcollection=
//items.Header.Set("Host","passport.cnblogs.com");
//items.Header.Add("你好啊","hi");
//items.Header.Add("date", "2013-07-18");
HttpResult result = helper.GetHtml(items);
//取出返回的Cookie
string cookie = result.Cookie;
//返回的Html内容
string html = result.Html;[/code]
还想请教的是,在使用items.Header.Add("Host","passport.cnblogs.com");的时候总是会提示“无法直接修改标题host”,参数名:name
希望得到你的指教!
|
|