将代理与 HttpWebRequest if/else 结合使用

using proxy with HttpWebRequest if/else

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

你能找出问题所在吗?

        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;
         }


    }

此处显示的代码也不例外。所以你传的参数简单描述一下就可以解决

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;
            }
        }
    }
}