GetResponse()在我的proxychecker c#中抛出错误

时间:2015-10-05 23:18:31

标签: c#

我正在尝试执行多线程代理检查,但GetResponse会抛出错误。我试图使用catch webException,仍然无法正常工作。我被困了请求帮助。你能告诉我我应该更改或添加到这段代码中吗?

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web;

namespace ProxyChecker
{

 internal class Program
{

    private static void Main(string[] args)
    {
        string path = "C:\\users\\zsolt\\desktop\\proxies.txt";
        //string path = args[0];
        List<string> proxies = File.ReadLines(path).ToList();

        Parallel.ForEach(proxies, proxy =>
        {
             WebProxy myproxy = new WebProxy(proxy);
            WebRequest myWebRequest = WebRequest.Create("http://www.google.com");
            myWebRequest.Timeout = 3000;
            myWebRequest.Proxy = myproxy;
       //   WebResponse myWebResponse = myWebRequest.GetResponse();
       //   myWebResponse.Close();

            try
            {
                using (WebResponse response = myWebRequest.GetResponse())
                {
                    HttpWebResponse httpResponse = (HttpWebResponse) response;
                    Console.WriteLine(proxy, httpResponse.StatusCode);
                }
            }
            catch
                (WebException e)
            {
                using (WebResponse response = e.Response)
                {
                    HttpWebResponse httpResponse = (HttpWebResponse) response;
                    Console.WriteLine("Error code: {0}", httpResponse.StatusCode);
                    using (Stream data = response.GetResponseStream())
                    {
                        string text = new StreamReader(data).ReadToEnd();
                        Console.WriteLine(proxy, text);
                    }
                }
            }

            });

            Console.Read();
        }
    }
 }

0 个答案:

没有答案