首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将凭据传递给WebProxy?

将凭据传递给WebProxy?
EN

Stack Overflow用户
提问于 2011-11-05 21:43:42
回答 2查看 21.2K关注 0票数 19

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

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

代码语言:javascript
复制
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();
    }
}

}

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-11-05 21:55:41

我认为这应该行得通:

代码语言:javascript
复制
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 );
}
票数 29
EN

Stack Overflow用户

发布于 2018-04-17 15:07:49

我做了一点修改

代码语言:javascript
复制
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);
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8020537

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档