利用WebRequest来实现模拟浏览器通过Post方式向服务器提交数据
2010-11-08 23:30
976 查看
//通过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 = “检索页时出错”;
}
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方式向服务器提交数据
- 利用WebRequest来实现模拟浏览器通过Post方式向服务器提交数据
- 利用WebRequest来实现模拟浏览器通过Post方式向服务器提交数据
- 利用WebRequest来实现模拟浏览器通过Post方式向服务器提交数据
- 利用HttpWebRequest以POST方式提交Json数据-后台实现不同平台间的数据传输
- Winform利用POST传值方式模拟表单提交数据(Winform与web交互)
- 利用HttpWebRequest通过POST Json数据在.net后台实现不同平台间的数据传输
- 小程序wx.request通过post方式提交数据给服务器-小程序支付开发教程
- (转载)利用HttpWebRequest通过POST Json数据在.net后台实现不同平台间的数据传输
- 通过表单form模拟post方式提交多参数实现文件下载
- C# Winform利用POST传值方式模拟表单提交数据(Winform与网页交互)
- 黄聪:C#模拟网站页面POST数据提交表单(二)--HttpWebRequest以及HttpWebResponse (转)
- (转)C# Winform利用POST传值方式模拟表单提交数据(Winform与网页交互)
- 【转】asp.net(c#)使用HttpWebRequest附加携带请求参数以post方式模拟上传大文件(以图片为例)到Web服务器端
- domino代理通过Request_Content域接收extjs以post方式提交的数据
- C# Winform利用POST传值方式模拟表单提交数据(Winform与网页交互)
- 黄聪:C#模拟网站页面POST数据提交表单(二)--HttpWebRequest以及HttpWebResponse (转)
- java模拟表单上传文件,java通过模拟post方式提交表单实现图片上传功能实例
- C# Winform利用POST传值方式模拟表单提交数据(Winform与网页交互)
- c# POST和GET方式通过服务器地址提交数据