C# 中HttpWebRequest 获取网页信息阻塞问题,只能提交一次 没有释放req.Abort();wr.close();我以前遇到过 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 另外补充一下 如果你要提取大量网页在catch里也要释放一下 修改之后的代码”: public String dopost(String keyword) { Encoding myEncoding = Encoding.GetEncoding("gb2312"); string param = HttpUtility.UrlEncode("hl", myEncoding) + "=" + HttpUtility.UrlEncode("zh-CN", myEncoding) + "&" + HttpUtility.UrlEncode("num", myEncoding) + "=" + HttpUtility.UrlEncode("100", myEncoding) + "&" + HttpUtility.UrlEncode("q", myEncoding) + "=" + HttpUtility.UrlEncode(keyword, myEncoding); byte[] postBytes = Encoding.ASCII.GetBytes(param); HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://www.google.com.hk/search?"+param); req.Method = "GET"; req.ContentType = "application/x-www-form-urlencoded;charset=gb2312"; req.ContentLength = postBytes.Length; WebResponse wr = null; try { wr = req.GetResponse(); } catch { label_status.Text = "网络连接错误,没插网线吧!"; } using (wr) { StreamReader sr = new StreamReader(wr.GetResponseStream(),Encoding.GetEncoding("gb2312")); String str=sr.ReadToEnd(); req.Abort(); wr.Close(); return str; } }依然不行 ,提示是wr为空 req.Method = "POST";req.ContentType = "application/x-www-form-urlencoded";req.ContentLength = bs.Length;using (Stream reqStream = req.GetRequestStream()){ }using (WebResponse wr = req.GetResponse()){} 我是 直接 get方式的 ,非要采用post 方式?请明示 HttpWebRequest MyRequest = null; HttpWebResponse MyResponse = null; try { if (url.IndexOf("www.tudou.com/programs/view/") > 0) { return checkSame(url); } else { MyRequest = (HttpWebRequest)HttpWebRequest.Create(url); MyRequest.Timeout = 15000; MyRequest.AllowAutoRedirect = false; MyResponse = (HttpWebResponse)MyRequest.GetResponse(); if (MyResponse.StatusCode == HttpStatusCode.OK) { Stream resStream = MyResponse.GetResponseStream(); StreamReader webRstrem = new StreamReader(resStream, Encoding.Default); StringBuilder sb = new StringBuilder(); string Result = ""; while ((Result = webRstrem.ReadLine()) != null) { sb.Append(Result); if (Result.IndexOf("</title>") > 0) { break; } } string haha = sb.ToString(); webRstrem.Close(); resStream.Close(); MyResponse.Close(); MyRequest.Abort(); string regex = @"(?<=<title.*>)([\s\S]*)(?=</title>)"; System.Text.RegularExpressions.Regex ex = new System.Text.RegularExpressions.Regex(regex, System.Text.RegularExpressions.RegexOptions.IgnoreCase); string rile = ex.Match(haha).Value.Trim(); if (rile == "网页无法找到") { return false; } else { return true; } } else { MyResponse.Close(); MyRequest.Abort(); return false; } } } catch (Exception e) { if (MyResponse != null) { MyResponse.Close(); } if (MyRequest != null) { MyRequest.Abort(); } if (e.Message.ToString() == "操作超时") { time++; if (time >= 3) { return false; } System.Threading.Thread.Sleep(500); return checkTudou(url); } } 摘自我以前写的一个类 vs2005 sql2003下没问题 请问,这个var怎么转型? 急急急 谢谢 c#里的数组问题 菜鸟提问 关于WPF中的dataGrid c#flash焦点图广告的后台代码如何让字体在图像上透明显示? 在C#里怎样才能将WINDOWS窗体分屏啊?在线等~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~急 WPF,动画的Seek方法到底是啥意思? 一个相当简单的问题,关于textBox.Text C# winfrom comboBox控件 comboBox.Text获取值和实际值不同 如何接收要打印的数据 c#与java进行socket连接的问题,请大家指点一下,小弟实在是不会鸟
{
Encoding myEncoding = Encoding.GetEncoding("gb2312");
string param = HttpUtility.UrlEncode("hl", myEncoding) + "=" + HttpUtility.UrlEncode("zh-CN", myEncoding) + "&"
+ HttpUtility.UrlEncode("num", myEncoding) + "=" + HttpUtility.UrlEncode("100", myEncoding) + "&"
+ HttpUtility.UrlEncode("q", myEncoding) + "=" + HttpUtility.UrlEncode(keyword, myEncoding);
byte[] postBytes = Encoding.ASCII.GetBytes(param); HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://www.google.com.hk/search?"+param);
req.Method = "GET";
req.ContentType = "application/x-www-form-urlencoded;charset=gb2312";
req.ContentLength = postBytes.Length;
WebResponse wr = null;
try
{
wr = req.GetResponse();
}
catch
{
label_status.Text = "网络连接错误,没插网线吧!";
} using (wr)
{
StreamReader sr = new StreamReader(wr.GetResponseStream(),Encoding.GetEncoding("gb2312"));
String str=sr.ReadToEnd();
req.Abort();
wr.Close();
return str;
}
}
依然不行 ,提示是wr为空
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = bs.Length;
using (Stream reqStream = req.GetRequestStream())
{
}
using (WebResponse wr = req.GetResponse())
{}
请明示
HttpWebRequest MyRequest = null;
HttpWebResponse MyResponse = null;
try
{
if (url.IndexOf("www.tudou.com/programs/view/") > 0)
{
return checkSame(url);
}
else
{ MyRequest = (HttpWebRequest)HttpWebRequest.Create(url);
MyRequest.Timeout = 15000;
MyRequest.AllowAutoRedirect = false;
MyResponse = (HttpWebResponse)MyRequest.GetResponse(); if (MyResponse.StatusCode == HttpStatusCode.OK)
{
Stream resStream = MyResponse.GetResponseStream();
StreamReader webRstrem = new StreamReader(resStream, Encoding.Default);
StringBuilder sb = new StringBuilder();
string Result = "";
while ((Result = webRstrem.ReadLine()) != null)
{
sb.Append(Result);
if (Result.IndexOf("</title>") > 0)
{
break;
}
}
string haha = sb.ToString(); webRstrem.Close();
resStream.Close();
MyResponse.Close();
MyRequest.Abort(); string regex = @"(?<=<title.*>)([\s\S]*)(?=</title>)";
System.Text.RegularExpressions.Regex ex = new System.Text.RegularExpressions.Regex(regex, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
string rile = ex.Match(haha).Value.Trim();
if (rile == "网页无法找到")
{
return false;
}
else
{
return true;
}
}
else
{
MyResponse.Close();
MyRequest.Abort();
return false;
}
}
}
catch (Exception e)
{
if (MyResponse != null)
{
MyResponse.Close();
}
if (MyRequest != null)
{
MyRequest.Abort();
}
if (e.Message.ToString() == "操作超时")
{
time++;
if (time >= 3)
{
return false;
}
System.Threading.Thread.Sleep(500);
return checkTudou(url);
}
}