使用代理与HttpWebRequest if / else

时间:2018-10-06 08:19:03

标签: c# c#-4.0 httpwebrequest

我正在尝试基于if语句使HttpWebRequest与代理一起工作:

你能发现什么毛病吗?

        private HttpWebRequest CreateRequest(Uri url)
    {
        HttpWebRequest request;
         if (Options.DefaultUseproxy != null) 
         {
        request = (HttpWebRequest)WebRequest.Create(url);
        request.Timeout = this.responseTimeout;
        request.UserAgent = this.userAgent;

        request.Proxy = new WebProxy(this.Proxy, false);
        return request;
        }
         else
         {
            request = (HttpWebRequest)WebRequest.Create(url);
            request.Timeout = this.responseTimeout;
            request.UserAgent = this.userAgent;


            return request;
         }


    }

1 个答案:

答案 0 :(得分:1)

The bollow code has no exception .So the parameter you pass 
If you describ brifly can solve


using System;
using System.Net;

namespace webreq
{
    class Program
    {
        IWebProxy Proxy;
       static string DefaultUseproxy = "";
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
           var Uri = new Uri("http://yahoo.com");
          var eres =  CreateRequest(Uri);
        }
        private static HttpWebRequest CreateRequest(Uri url)
        {
            HttpWebRequest request;
            if (DefaultUseproxy != null)
            {
                request = (HttpWebRequest)WebRequest.Create(url);
                request.Timeout = 1000;
                request.UserAgent = "demo";

                request.Proxy = new WebProxy();
                return request;
            }
            else
            {
                request = (HttpWebRequest)WebRequest.Create(url);
                request.Timeout = 1000;
                request.UserAgent = "demo";


                return request;
            }


        }
    }
}