//通过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 = “检索页时出错”;
}
利用WebRequest来实现模拟浏览器通过Post方式向服务器提交数据
原创
©著作权归作者所有:来自51CTO博客作者linzheng的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:C#MD5算法
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
用HttpClient来模拟浏览器GET POST
用HttpClient来模拟浏览器GET POST转载自: http://www.xd
浏览器 应用服务器 Apache JSP 编程 -
利用Telnet模拟浏览器
希望今年夏天的遗憾能成为你秋天的惊喜。。。。---- 网易云热评一、win7开启Telnet服务打开控制面板,选择程序,打开
web安全 telnet 浏览器 HTTP -
android与服务器端数据交互方式 android向服务器提交数据
Android网络编程之传递数据给服务器(二)请尊重他人的劳动成果,转载请注明出处:Android网络编程之传递数据给服务器(二) 我曾在《Android网络编程之传递数据给服务器(一) 》 一文中介绍了如何通过GET方式传递数据给服务器,通过GET方式传递数据主要适用于数据大小不超过2KB,且对安全性要求不高的情况下。下面
android与服务器端数据交互方式 通过Post方式传递数据给服务器 Android通过Post方式传递数据给 传递数据给服务器 Android传递数据给服务器