标题:WebRequest利用Post方法……?
只看楼主
tel1982
Rank: 3Rank: 3
来 自:水星
等 级:新手上路
威 望:8
帖 子:852
专家分:0
注 册:2006-10-21
 问题点数:0 回复次数:2 
WebRequest利用Post方法……?

通过Post发送的数据
string payload="chkbook=book&keyword=管理";
WebRequest req = WebRequest.Create("http://localhost/pceo/Search.aspx");
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
StringBuilder UrlEncoded = new StringBuilder();
Char[] reserved = {'?', '=', '&'};
byte[] SomeBytes = null;
if (payload != null)
{
int i=0, j;
while(i<payload.Length)
{
j=payload.IndexOfAny(reserved, i);
if (j==-1)
{
UrlEncoded.Append(HttpUtility.UrlEncode(payload.Substring(i, payload.Length-i),System.Text .Encoding .GetEncoding ("gb2312")));
break;
}
UrlEncoded.Append(HttpUtility.UrlEncode(payload.Substring(i, j-i),System.Text .Encoding .GetEncoding ("gb2312")));
UrlEncoded.Append(payload.Substring(j,1));
i = j+1;
}
SomeBytes = Encoding.Default.GetBytes(UrlEncoded.ToString());
req.ContentLength = SomeBytes.Length;
Stream newStream = req.GetRequestStream();
newStream.Write(SomeBytes, 0, SomeBytes.Length);
newStream.Close();
}
else
{
req.ContentLength = 0;
}
try
{
WebResponse result = req.GetResponse();
Stream ReceiveStream = result.GetResponseStream();
Byte[] read = new Byte[512];
int bytes = ReceiveStream.Read(read, 0, 512);
txtHTML.InnerHtml = "";
while (bytes > 0)
{
// 注意:
// 下面假定响应使用 UTF-8 作为编码方式。
// 如果内容以 ANSI 代码页形式(例如,932)发送,则使用类似下面的语句:
// Encoding encode = System.Text.Encoding.GetEncoding("shift-jis");
Encoding encode = System.Text.Encoding.GetEncoding("gb2312");
txtHTML.InnerHtml = txtHTML.InnerHtml + encode.GetString(read, 0, bytes);
bytes = ReceiveStream.Read(read, 0, 512);
}
}
catch(Exception)
{
txtHTML.InnerHtml = "检索页时出错";
}

这段代码中的txtHTML是怎样定义的呀?[em13]

[此贴子已经被作者于2007-10-18 13:34:21编辑过]

搜索更多相关主题的帖子: Post WebRequest 
2007-10-18 13:33
Clotho
Rank: 1
等 级:新手上路
帖 子:113
专家分:0
注 册:2007-9-23
得分:0 
楼主你问what啊?

思辩的人总是喜欢纠缠到大辩中,无奈..
2007-10-18 20:47
Clotho
Rank: 1
等 级:新手上路
帖 子:113
专家分:0
注 册:2007-9-23
得分:0 
这里的txtHTML是 某个自定义类罢了,它其中一个参数用来缓冲获取html

思辩的人总是喜欢纠缠到大辩中,无奈..
2007-10-18 20:49



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-178722-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.141608 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved