//通过Post发送的数据
string payload="chkbook=book&keyword=管理";
WebRequest req = WebRequest.Create(" http://localhost/pceo/Search.aspx ");
请求方法 = "POST";
req.ContentType = "application/x-www-form-urlencoded";
StringBuilder UrlEncoded = new StringBuilder();
Char[] 保留 = {'?', '=', '&'};
byte[] SomeBytes = null;
if(有效负载!= null)
{
int i = 0,j;
while(i<有效负载.长度)
{
j=payload.IndexOfAny(保留, i);
如果(j==-1)
{
UrlEncoded.Append(HttpUtility.UrlEncode(payload.Substring(i,payload.Length-i),System.Text.Encoding.GetEncoding(“gb2312”)));
休息;
}
UrlEncoded.Append(HttpUtility.UrlEncode(payload.Substring(i, ji),System.Text .Encoding .GetEncoding("gb2312")));
UrlEncoded.Append(payload.Substring(j,1));
我 = j+1;
}
SomeBytes = Encoding.Default.GetBytes(UrlEncoded.ToString());
req.ContentLength = SomeBytes.Length;
Stream newStream = req.GetRequestStream();
newStream.Write(SomeBytes, 0, SomeBytes.Length);
newStream.Close();
}
别的
{
请求内容长度 = 0;
}
尝试
{
WebResponse 结果 = req.GetResponse();
流 ReceiveStream = result.GetResponseStream();
字节[]读取=新字节[512];
int 字节 = ReceiveStream.Read(读, 0, 512);
txtHTML.InnerHtml = "";
而(字节> 0)
{
// 注意:
// 下面假设响应使用UTF-8作为编码方式。
// 如果内容以ANSI代码页形式(例如,932)发送,则使用类似下面的语句:
// 编码encode = System.Text.Encoding.GetEncoding("shift-jis");
编码 编码 = System.Text.Encoding.GetEncoding("gb2312");
txtHTML.InnerHtml = txtHTML.InnerHtml + 编码.GetString(read, 0, bytes);
字节 = ReceiveStream.Read(读, 0, 512);
}
}
捕获(异常)
{
txtHTML.InnerHtml = "检索页时出错";
}
出处:狂风之家BLOG