利用WebRequest来实现模拟浏览器通过Post方式向服务器提交数据
2006-04-03 17:47
1316 查看
//通过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数据-后台实现不同平台间的数据传输
- 利用HttpWebRequest通过POST Json数据在.net后台实现不同平台间的数据传输
- (转载)利用HttpWebRequest通过POST Json数据在.net后台实现不同平台间的数据传输
- Winform利用POST传值方式模拟表单提交数据(Winform与web交互)
- 小程序wx.request通过post方式提交数据给服务器-小程序支付开发教程
- 转载 C# Winform利用POST传值方式模拟表单提交数据(Winform与网页交互)
- 程序使用 HTTP 协议和服务器交互主要是进行数据的提交,通常数据的提交是通过 GET 和 POST 两种方式来完成
- C# Winform利用POST传值方式模拟表单提交数据(Winform与网页交互)
- HttpWebRequest 向网站提交数据 (post与get方式)
- 纯vc sdk实现http post 方式上传数据到web服务器
- CC3200 HTTP Cli通过POST的方式把json数据提交给服务器出现的问题
- 利用HttpWebRequest来实现提交和获取网页数据
- domino代理通过Request_Content域接收extjs以post方式提交的数据
- android 通过get,post方式提交数据给服务器
- 原始ajax通过xmlHttpRequest对象的send()方法提交数据--Get方式和Post方式
- domino代理通过Request_Content域接收extjs以post方式提交的数据