我用c#做一个这样功能的程序,
我输入一批有规则的URL,比如
http://abc.blog.163.com
http://aaa.blog.163.com
http://bbb.blog.163.com
http://ccc.blog.163.com可能有几百个这样的URL, 我的意图是查看这些指定的163博客有没有注册,
我使用httpwebrequest 去访问这些URL,如果没注册会返回404.
我是分批进行处理,比如第一次到10条URL,启用10个线程进行处理,我用ManualResetEvent来等待每批次处理的完成.
再取10条这样操作.
现在的问题是程序启动后,执行一会就自动关闭,不报异常。捕捉到的只有404的webexception,这个我都是处理了的。是不是httpwebrequest在多线程有什么要注意的吗?请各位帮分析下,谢谢!
我输入一批有规则的URL,比如
http://abc.blog.163.com
http://aaa.blog.163.com
http://bbb.blog.163.com
http://ccc.blog.163.com可能有几百个这样的URL, 我的意图是查看这些指定的163博客有没有注册,
我使用httpwebrequest 去访问这些URL,如果没注册会返回404.
我是分批进行处理,比如第一次到10条URL,启用10个线程进行处理,我用ManualResetEvent来等待每批次处理的完成.
再取10条这样操作.
现在的问题是程序启动后,执行一会就自动关闭,不报异常。捕捉到的只有404的webexception,这个我都是处理了的。是不是httpwebrequest在多线程有什么要注意的吗?请各位帮分析下,谢谢!
ManualResetEvent[] doneEvents = new ManualResetEvent[10];for (int x = 0; x < 10; x++)
{
doneEvents[x] = new ManualResetEvent(false); DoWork doWork = new DoWork(new CustomeEvetnArgs(textbox1, words[x], "blog.163.com", doneEvents[x])); ThreadPool.QueueUserWorkItem(new WaitCallback(doWork.send), x);
} WaitHandle.WaitAll(doneEvents);
下面是httpwebrequest处理代码 public void send(Object threadContext)
{
HttpWebRequest myHttpWebRequest = null;
HttpWebResponse myHttpWebResponse = null;
// Creates an HttpWebRequest for the specified URL.
StringBuilder uri = new StringBuilder("http://");
uri.Append(args.word.Replace(" ",""));
uri.Append(".");
uri.Append(args.Domains); try
{
myHttpWebRequest = (HttpWebRequest)WebRequest.Create(uri.ToString());
myHttpWebRequest.Method = "GET";
myHttpWebRequest.AllowAutoRedirect = true;
myHttpWebRequest.KeepAlive = false;
myHttpWebRequest.ServicePoint.Expect100Continue = false;
//LogBLL.info(uri.ToString()); // Sends the HttpWebRequest, and waits for a response.
myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
myHttpWebResponse.Close(); }
catch (WebException ex)
{
LogBLL.error(ex.ToString());
}
finally
{
//通知当前线程已经结束
this.args.DoneEvent.Set(); myHttpWebRequest = null;
myHttpWebResponse = null;
uri = null;
}}
请大家帮分析下!谢谢
{
LogBLL.error(ex.ToString());
}换成 Exception ,再试试,可能抛出不是WebException?
原来是有抛出 UriFormatException 这个异常,处理它就好了
现在开50线程都不退出了
结贴给分