【问题标题】:Passing Credentials To WebProxy?将凭证传递给 WebProxy?
【发布时间】:2022-04-05 16:42:46
【问题描述】:

我有一个用于我正在使用的服务的自定义 HTTP 类。最终它将以方法的形式包含服务特定的请求。我需要做的是设置用户提供的代理的凭据,例如,如果用户有一个代理列表。

下面是我的代码。我已经评论了我需要设置凭据的部分。我查看了 MSDN 上的 iCredentials 类,但看不到如何从字符串中设置它们。

class RequestClass
{
    private CookieContainer cookieJar;
    private WebProxy proxy = null;

    public RequestClass()
    {
        this.cookieJar = new CookieContainer();
    }

    public RequestClass(String proxyURL, int port)
    {
        this.proxy = new WebProxy(proxyURL, port);
    }

    public RequestClass(String proxyURL, int port, String username, String password)
    {
        this.proxy = new WebProxy(proxyURL, port);
        // Need to set them here
    }

    // HTTP Get Request
    public HttpWebResponse getRequest(String url, NameValueCollection headers)
    {
        HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(url);
        getRequest.Method = "GET";
        getRequest.CookieContainer = cookieJar;

        foreach (String key in headers.Keys)
        {
            getRequest.Headers.Add(key, headers[key]);
        }

        return (HttpWebResponse)getRequest.GetResponse();
    }

    // HTTP Post Request
    public HttpWebResponse postRequest(String url, String postData, NameValueCollection headers)
    {
        byte[] postBytes = Encoding.ASCII.GetBytes(postData);

        HttpWebRequest postRequest = (HttpWebRequest)WebRequest.Create(url);
        postRequest.Method = "POST";
        postRequest.CookieContainer = cookieJar;
        postRequest.ContentLength = postBytes.Length;
        postRequest.ProtocolVersion = HttpVersion.Version10;

        foreach(String key in headers.Keys)
        {
            postRequest.Headers.Add(key, headers[key]);
        }

        Stream postRequestStream = postRequest.GetRequestStream();

        postRequestStream.Write(postBytes, 0, postBytes.Length);
        postRequestStream.Close();

        return (HttpWebResponse)postRequest.GetResponse();
    }
}

}

【问题讨论】:

    标签: c#


    【解决方案1】:

    我认为这应该可行:

    public RequestClass(String proxyURL, int port, String username, String password)
    {
        //Validate proxy address
        var proxyURI = new Uri(string.Format("{0}:{1}", proxyURL, port));
    
        //Set credentials
        ICredentials credentials = new NetworkCredential(username, password);
    
        //Set proxy
        this.proxy =  = new WebProxy(proxyURI, true, null, credentials );
    }
    

    【讨论】:

    • 在刷新答案之前,我只是在阅读有关 NetworkCredential 的信息。我认为你是对的,这就是这样做的方式。我来测试一下。
    【解决方案2】:

    我做了一点修改

    public System.Net.IWebProxy RequestClass(String proxyURL, int port, String username, String password)
        {
            //Validate proxy address
            var proxyURI = new Uri(string.Format("{0}:{1}", proxyURL, port));
    
            //Set credentials
            ICredentials credentials = new NetworkCredential(username, password);
    
            //Set proxy
           return new WebProxy(proxyURI, true, null, credentials);
        }
    

    【讨论】:

      【解决方案3】:

      Sklivvz 的回答是正确的。但是,我不赞成在 Uri 中使用 string.Format

      using System;
      using System.Net;
      
      public class RequestClass
      {
          private WebProxy proxy = null;
      
          public RequestClass(string url, int port, string username, string password)
          {
              // Prepare URL
              var urlBuilder = new UriBuilder(url);
              urlBuilder.Port = port;
      
              // Setup credentials
              var credentials = new NetworkCredential(username, password);
      
              // Setup proxy
              this.proxy = new WebProxy(urlBuilder.Uri, true, null, credentials);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-05-03
        • 2022-01-06
        • 2021-08-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-17
        • 1970-01-01
        相关资源
        最近更新 更多