C# 中HttpWebRequest 获取网页信息阻塞问题,只能提交一次
按钮事件:
private void button_start_Click(object sender, EventArgs e)
{
if (textBox_url.Text.Replace("http://", "") != "")
{
domain = textBox_url.Text.Replace("http://", "").Replace("www.", "");
String htmlcode =dopost("论文" + " site:" + domain);
MessageBox.Show(htmlcode);
}
else
{
MessageBox.Show("The Target is Null,易做图 you!");
}
}
提交方法:
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());
return sr.ReadToEnd();
}
}
代码都在这里了,编译之后,第一次执行都没有问题,但是只能执行一次;要重新提交请求的话必须要把 程序关了再重新打开,极度郁闷啊
分不少了吧,要是嫌少,我可以再加,分不是问题,问题是怎么感觉这C#这么奇怪啊!
--------------------编程问答-------------------- 没有释放
req.Abort();
wr.close();
我以前遇到过 --------------------编程问答-------------------- 另外补充一下 如果你要提取大量网页在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下没问题 --------------------编程问答-------------------- 晕菜了 server2003
补充:.NET技术 , C#