使用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())
{
//在这里对接收到的页面内容进行处理
}