//通过Post发送的数据 Byte[] read = new Byte[512]; txtHTML.InnerHtml = “”; // 注意: 出处:狂风之家 BLOG
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();
int bytes = ReceiveStream.Read(read, 0, 512);
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方式向服务器提交数据_asp.net技巧
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 利用webrequest来实现模拟浏览器通过post方式向服务器提交数据_asp.net技巧
相关推荐
-      对.net framework 反射的反思_asp.net技巧
-      .net3.5和vs2008中的asp.net ajax_asp.net技巧
-      使用asp.net ajax框架扩展html map控件_asp.net技巧
-      asp.net应用程序资源访问安全模型_asp.net技巧
-      photoshop初学者轻松绘制螺旋漩涡特效_photoshop教程
-      photoshop通道结合图层模式抠狗尾巴草_photoshop教程
-      web.config详解+asp.net优化_asp.net技巧
-      asp.net中多彩下拉框的实现_asp.net技巧