日期:2014-05-18  浏览次数:20789 次

使用POST 方式提交数据时出现异常
各位帮帮忙,代码如下:

Encoding myEncoding = Encoding.GetEncoding("gb2312");
string param = HttpUtility.UrlEncode("wd", myEncoding) + "=" + HttpUtility.UrlEncode("sohu", myEncoding) + "&" + HttpUtility.UrlEncode("cl", myEncoding) + "=" + HttpUtility.UrlEncode("3", myEncoding);

byte[] bs = Encoding.UTF8.GetBytes(param);
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://www.baidu.com/s");
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded;charset=gb2312";
req.ContentLength = bs.Length;
Stream reqStream = req.GetRequestStream();//执行到这里出现异常
//异常信息:基础连接已经关闭: 连接被意外关闭。
reqStream.Write(bs, 0, bs.Length);
reqStream.Close();
using (WebResponse wr = req.GetResponse())
{
  //在这里对接收到的页面内容进行处理
}

------解决方案--------------------
没人。我帮你顶顶
------解决方案--------------------
百度发现你是冒牌的浏览器,就断掉了你的连接。
------解决方案--------------------
楼主 参考下这个 http://www.dotnetsky.net/netsave/ShowTopic-32641.html
------解决方案--------------------
代码好像没有错啊
有没有使用代理上网,如果有要设置代理访问才可以
------解决方案--------------------
看看啥异常啊?
------解决方案--------------------
http://www.baidu.com/s 都没有这个页面,LZ玩我们啊
------解决方案--------------------
关注
------解决方案--------------------
Stream reqStream = req.GetRequestStream();//执行到这里出现异常
我怀疑关闭了 request! 这个stream本身有什么好函数?
------解决方案--------------------
帮顶,会不会是网址错了的原因?