关于HttpWebRequest的问题(急)

bigbaldy 2012-03-09 01:50:25
我要做的是向一个网站提交表单,但是该网站有cookie验证,提交数据必须得有相应的cookie才行,第一次进入网站时,该网站会返回一个sessionID,所以我就打算用下面的方法,访问两次,第一次获取cookie,第二次带着cookie提交数据,但运行到最后一行“request1.GetResponse();”时程序会卡死不动,直到提示超时错误,而如果我中间把request.Abort()了,那么虽然能提交,但之前与网站的连接已经不存在,提交的cookie也就不是本次所需要的了,大家帮帮忙,这个问题我是实在不知道如何解决了,不过用python,可以说同样的方法就能成功,难道C#就不行。。。。。

下面是C#和python的代码

CookieContainer cookieContainer = new CookieContainer();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://192.168.0.232:8360/rule_conf/index_core.htm?product=url&business_type=1");
request.CookieContainer = cookieContainer;
request.Accept = "text/html, application/xhtml+xml, */*";
request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)";
request.Method = "Get";
request.Headers.Add("Accept-Encoding: gzip, deflate");
request.Headers.Add("Accept-Language: zh-CN");
request.KeepAlive = true;
request.GetResponse();

HttpWebRequest request1 = (HttpWebRequest)WebRequest.Create("http://192.168.0.232:8360/rule_conf/index_core.htm?product=url&business_type=1");
request1.CookieContainer = cookieContainer;
request1.Accept = "text/html, application/xhtml+xml, */*";
request1.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)";
request1.Method = "Post";
request1.Headers.Add("Accept-Encoding: gzip, deflate");
request1.Headers.Add("Accept-Language: zh-CN");
request1.KeepAlive = true;
string data = "op=add&rule_num=&apply=N&rule_id=wdid%3A150&business_type=1&rules=%28return_levelsublevel%3A70.0%29%26%26%28file_path%3Alike%2Clinetest4%29&type=in®ionlist=100000&hit_limit=&rule_desc=test&del_reasion=";
request1.ContentLength = data.Length;
StreamWriter sw = new StreamWriter(request1.GetRequestStream(), Encoding.ASCII);
sw.Write(data);
sw.Flush();
request1.GetResponse();
}



#coding: gbk
import urllib
import urllib2
import cookielib
import httplib2


Entryurl = 'http://192.168.0.232:8360/rule_conf/index_core.htm?product=url&business_type=1'
http = httplib2.Http()


headers = {'User-agent':'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1)',
'Accept':'text/html, application/xhtml+xml, */*',
'Referer':'http://192.168.0.232:8360/rule_conf/',
'Accept-Encoding':'gzip, deflate',
'Host':'192.168.0.232:8360',
'Connection':'Keep-Alive',
'Accept-Language':'zh-CN'}
response, content = http.request(Entryurl, 'GET', headers=headers)
cookie = re.split(';',response['set-cookie'])[0]


url = 'http://192.168.0.232:8360/rule_conf/index_core.htm?product=url&business_type=1'
data = {'op':'add',
'rule_num':'',
'apply':'N',
'rule_id':'wdid:2',
'business_type':'1',
'rules':'(return_levelsublevel:40.0)&&(file_path:like,自动化测试)',
'type':'in',
'regionlist':'100000',
'hit_limit':'',
'rule_desc':'test',
'del_reasion':'',
}
postData = urllib.urlencode(data)


headers ={'User-Agent':'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1)',
'Content-Type':'application/x-www-form-urlencoded',
'Cache-Control':'no-cache',
'Accept': 'text/html, application/xhtml+xml, */*',
'Connection': 'Keep-Alive',
'Referer':'http://192.168.0.232:8360/rule_conf/index_core.htm?product=url&business_type=1',
'Accept-Language': 'zh-CN',
'Cookie': cookie,

#User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
'Accept-Encoding: gzip': 'deflate',
'Host': '192.168.0.232:8360',
'Connection': 'Keep-Alive',
'Cache-Control':'no-cache'}
response, content = http.request(Entryurl, 'POST', headers=headers,body = postData)
print response
print content
...全文
151 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧