httpwebrequest发送POST请求,获取返回的头部值,急!

Anew_G 2012-03-06 08:55:58
用抓包软件(Fiddler2)可以得知发送的包是这样的(是一个登入页面,我输入了帐号密码)

Header里是这样的

POST /login HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Referer: 要登入的某网址/login
Accept-Language: zh-CN
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Host: 要登入的某网址
Content-Length: 103
Connection: Keep-Alive
Pragma: no-cache
Cookie: PHPSESSID=9a73683e21399713d6cddb270ec16c08; cnzz_a30020080=1; sin30020080=; rtime30020080=0; ltime30020080=1331033172518; cnzz_eid30020080=87021669-1331032392-

Textview里显示

charset=utf-8&jumpurl=%2F&username=我的帐号&password=我的密码&rememberme=1&input2=%E7%99%BB+%E5%BD%95

我用httpwebrequest模拟这个数据包

HttpWebRequest requestCookies = (HttpWebRequest)WebRequest.Create(要登入的某网址);
requestCookies.ContentType = "application/x-www-form-urlencoded";
requestCookies.Referer = "要登入的某网址/login";
requestCookies.Headers.Set("Pragma", "no-cache");
requestCookies.Accept = "text/html, application/xhtml+xml, */*";
requestCookies.Headers.Set("Accept-Language", "zh-CN");
requestCookies.Headers.Set("Accept-Encoding", "gzip, deflate");
string temp = "PHPSESSID=22a234c094a7f36ba11e6d6767fc614c; cnzz_a30020080=0; sin30020080=; rtime30020080=0; ltime30020080=1330943336040; cnzz_eid30020080=78128217-1330939152-";
requestCookies.Headers.Set("cookie", temp);
requestCookies.Method = "POST";

Encoding encoding23 = Encoding.GetEncoding("utf-8");
byte[] bytesToPost = encoding23.GetBytes("charset=utf-8&jumpurl=%2F&username=帐号&password=密码&rememberme=1&input2=%E7%99%BB+%E5%BD%95");
requestCookies.ContentLength = bytesToPost.Length;
System.IO.Stream requestStream = requestCookies.GetRequestStream();
requestStream.Write(bytesToPost, 0, bytesToPost.Length);
requestStream.Close();


HttpWebResponse hwr = (HttpWebResponse)requestCookies.GetResponse();

WebHeaderCollection head = hwr.Headers;
IEnumerator iem = head.GetEnumerator();

ArrayList value = new ArrayList();

for (int i = 0; iem.MoveNext(); i++)
{
string key=head.GetKey(i);
value.Add(head.GetValues(key));
}

string filePath2 = @"c:\infor2.txt";
FileStream fs2 = new FileStream(filePath2, FileMode.Open, FileAccess.ReadWrite);
StreamWriter sw2 = new StreamWriter(fs2);
fs2.SetLength(0);

for (int i = 0; i < value.Count; i++)
{
sw2.WriteLine(value[i].ToString());
}

sw2.Close();



打开文件后我想看下里面的内容是否是我想要的结果(如下,抓包软件中抓的)

HTTP/1.1 200 OK
Server: [SA: new-server-50 ][AD: http://www.hoopchina.com ]
Date: Sun, 04 Mar 2012 08:20:53 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
Vary: Accept-Encoding
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Set-Cookie: u=4673245%7CQW5ld19HLg%3D%3D%7Cedea%7C2635de86714644840c7e1d91f7f2b9be%7C714644840c7e1d91; path=/; domain=.hoopchina.com; httponly
Set-Cookie: ua=28931506; expires=Tue, 05-Mar-2013 08:20:53 GMT; path=/; domain=.hoopchina.com
Set-Cookie: g=deleted; expires=Sat, 05-Mar-2011 08:20:52 GMT; path=/; domain=.hoopchina.com; httponly
Content-Length: 2573

里面Set-cookie一些字符我后面的程序会用到

可是打开后确是这样

System.String[]
System.String[]
System.String[]
System.String[]
System.String[]
System.String[]
System.String[]
System.String[]
System.String[]
System.String[]
System.String[]

请问下哪里出了问题?
我发送POST及其内容的步骤有没哪里错了?
...全文
1383 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
xnlm2005 2012-03-07
  • 打赏
  • 举报
回复
问题是action不用放进去啊。
Anew_G 2012-03-07
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 viewstates 的回复:]

urlencode

HOOPCHINA....我×。。。。
[/Quote]

怎么了?
Anew_G 2012-03-07
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 xnlm2005 的回复:]

你的问题不是出在中文上吧,你的语句有问题,action不能放进去啊,他不属于Cookie的内容。
[/Quote]

action没放cookie里 action的那段是作为POST的正文

Encoding encoding233 = Encoding.GetEncoding("utf-8");
byte[]bytesToPost2=encoding233.GetBytes("action=virement&pwuser=asddsa&to_money=51&content_plus");
requestbank.ContentLength = bytesToPost2.Length;
System.IO.Stream requestStream2 = requestbank.GetRequestStream();
requestStream2.Write(bytesToPost2, 0, bytesToPost2.Length);
requestStream2.Close();
xnlm2005 2012-03-07
  • 打赏
  • 举报
回复
你的问题不是出在中文上吧,你的语句有问题,action不能放进去啊,他不属于Cookie的内容。
ViewStates 2012-03-07
  • 打赏
  • 举报
回复
urlencode

HOOPCHINA....我×。。。。
Anew_G 2012-03-07
  • 打赏
  • 举报
回复
比如一个字符串 ”XXX你好YYY“ 我想除了中文以外的数据全部不动 中文转成UTF-8

则实际post出去的内容为这样 ”XXX%QW%WE%ERYYY“ 请问应该如何实现?

我POST的数据原来是这样写的(都是POST英文)

Encoding encoding233 = Encoding.GetEncoding("utf-8");
byte[]bytesToPost2=encoding233.GetBytes("action=virement&pwuser=asddsa&to_money=51&content_plus");
requestbank.ContentLength = bytesToPost2.Length;
System.IO.Stream requestStream2 = requestbank.GetRequestStream();
requestStream2.Write(bytesToPost2, 0, bytesToPost2.Length);
requestStream2.Close();

现在就相当于要pwuser=asddsa里的asddsa可以是中文,发送的时候转为%XX%XX%XX格式的UTF-8编码
Anew_G 2012-03-07
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 xnlm2005 的回复:]

将value.Add(head.GetValues(key));换成value.Add(head.Get(key));应该就可以了,前者是返回了一个数组,肯定就是你这个结果了,后者是一个字符串。
[/Quote]

囧了 2L的问题是我自己搞错了 已解决

如果我要post的内容里有中文,要把所有的中文都转成%XX%XX%XX形式,应该怎么办?最后一个问题了 ^_^
Anew_G 2012-03-07
  • 打赏
  • 举报
回复

[Quote=引用 1 楼 xnlm2005 的回复:]

将value.Add(head.GetValues(key));换成value.Add(head.Get(key));应该就可以了,前者是返回了一个数组,肯定就是你这个结果了,后者是一个字符串。
[/Quote]

感谢 另外

                for (int i = 1; i <= pages; i++)
{

this.url2 = this.url + "-" + i + ".html";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this.url2);
request.Timeout = 150000;
request.Method = "GET";
requestCookies.Accept = "text/html, application/xhtml+xml, */*";
requestCookies.Headers.Set("Accept-Language", "zh-CN");
requestCookies.Headers.Set("Accept-Encoding", "gzip, deflate");
request.Headers.Set("Pragma", "no-cache");
string temp2 = "u="+mc3[0].Value+";"+"ua="+mc4[0].Value+";"+"passport="+mc5[0].Value+";";
requestCookies.Headers.Set("cookie", temp2);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream streamReceive = response.GetResponseStream();
Encoding encoding = Encoding.GetEncoding("GB2312");
StreamReader streamReader = new StreamReader(streamReceive, encoding);
strResult = strResult + streamReader.ReadToEnd();
}


为什么拿不到我需要的页面的源代码呢?
得到的页面是提示页面不存在(也就是没登入下的情况)

我用Fiddler2去发这个GET包(按照下面的形式是成功的)



GET /3322590.html HTTP/1.1:
Accept: text/html, application/xhtml+xml, */*
Accept-Language: zh-CN
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
Host: bbs.hoopchina.com
Cookie: u=4673245%7CQW5ld19HLg%3D%3D%7Cedea%7C2635de86714644840c7e1d91f7f2b9be%7C714644840c7e1d91;ua=28935704;passport=4673245%7CQW5ld19HLg%3D%3D%7C2635de86714644840c7e1d91f7f2b9be%7Cbreakitwall%40gmail.com;
xnlm2005 2012-03-07
  • 打赏
  • 举报
回复
直接Encoding.Default.GetString就行了
sam7pan 2012-03-07
  • 打赏
  • 举报
回复
UrlEncode(str, Encoding.UTF8);

public string UrlEncode(string str, Encoding e)
{
if (str == null)
{
return null;
}
return Encoding.ASCII.GetString(UrlEncodeToBytes(str, e));
}
Anew_G 2012-03-07
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 xnlm2005 的回复:]

问题是action不用放进去啊。
[/Quote]

是不是有的服务器端自己就有编码函数 我发中文数据 他自己会转成UTF-8?
Anew_G 2012-03-07
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 newuser2008 的回复:]

设置编码
[/Quote]

能否具体一点呢?
NewUser2008 2012-03-07
  • 打赏
  • 举报
回复
设置编码
Anew_G 2012-03-07
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 xnlm2005 的回复:]

问题是action不用放进去啊。
[/Quote]

哦 好的 你看下我4L的问题吧 POST数据正文里有中文想转 %XX%XX%XX 格式的utf-8字符应该怎样做?
xnlm2005 2012-03-06
  • 打赏
  • 举报
回复
将value.Add(head.GetValues(key));换成value.Add(head.Get(key));应该就可以了,前者是返回了一个数组,肯定就是你这个结果了,后者是一个字符串。

111,088

社区成员

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

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

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