日期:2014-05-17 浏览次数:21015 次
我在模拟请求[POST]一个网站的时候,一直连接不上去,总是提示我”操作超时“
HttpWebRequest httpWebRequest = null;
HttpWebResponse httpWebResponse = null;
try
{
byte[] byteRequest = Encoding.Default.GetBytes(postData);//格式化POST数据
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);//发送请求
// httpWebRequest.CookieContainer = cookieContainer;//填充COOKIE
//httpWebRequest.ContentType = "application/x-www-form-urlencoded";
//httpWebRequest.ServicePoint.ConnectionLimit = maxTry;
;
//httpWebRequest.Headers.Add("Accept-Language", "ko,zh-cn;q=0.5");
//httpWebRequest.Headers.Add("Accept-Encoding", "gzip, deflate");
// httpWebRequest.Referer = "";
//httpWebRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
//httpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:20.0) Gecko/20100101 Firefox/20.0";
httpWebRequest.Method = isPost ? "POST" : "GET";//那种请求?
httpWebRequest.ContentLength = byteRequest.Length;//获取数据包大小
Stream stream = httpWebRequest.GetRequestStream();//获取流对象
stream.Write(byteRequest, 0, byteRequest.Length);
stream.Close();
httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
//定义数据响应信息
Stream responseStream = httpWebResponse.GetResponseStream();//获取服务端回发达留信息
StreamReader streamReader = new StreamReader(responseStream, encoding);