【问题标题】:error (407) "Proxy Authentication Required."错误 (407) “需要代理身份验证”。
【发布时间】:2013-01-18 09:16:01
【问题描述】:

我有一个要求,比如...我想从 winforms 访问一个 url(登录页面是 web)。我必须将凭据传递给该 url,并且响应应该是经过身份验证的网页(标记)的内容。

我编写了一个函数,它将请求 url 并返回响应。但我收到错误代码 (407)

“需要代理身份验证。”

这是我的代码。

private static void GetPageContent(){
    string url = "https://LoginPage.aspx/";
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    request.Method = "GET";
    // If required by the server, set the credentials.
    //request.Proxy.Credentials = CredentialCache.DefaultCredentials;
    request.Credentials = new NetworkCredential("user1", "testuser#");
    // Get the response.
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    // Display the status.
    Console.WriteLine(response.StatusDescription);
    // Get the stream containing content returned by the server.
    Stream dataStream = response.GetResponseStream();
    // Open the stream using a StreamReader for easy access.
    StreamReader reader = new StreamReader(dataStream);
    // Read the content.
    string responseFromServer = reader.ReadToEnd();
    // Display the content.
    Console.WriteLine(responseFromServer);
    // Cleanup the streams and the response.
    reader.Close();
    dataStream.Close();
    response.Close();
}

【问题讨论】:

    标签: c# authentication httpwebrequest


    【解决方案1】:
    WebProxy proxy = new WebProxy(proxyAddress);
    proxy.Credentials = new NetworkCredential("username", "password", "domain");
    proxy.UseDefaultCredentials = true;
    WebRequest.DefaultWebProxy = proxy;
    
    HttpWebRequest request = new HttpWebRequest();
    request.Proxy = proxy;
    

    或者你可以使用WebClient

    WebClient client = new WebClient();
    client.Proxy = proxy;
    string downloadString = client.DownloadString("http://www.google.com");
    

    【讨论】:

    • 嗨,安妮,proxyAddress 是什么??
    • 我将 uri 作为代理地址传递,但仍然出现错误。但这次是“ServicePointManager 不支持使用 https 方案的代理。”
    • 确保 url 以 http:// 为前缀。参考thisthis
    • @Tim 您收到 407 错误,因为您在代理后面。要获取您的代理地址,请转到您的 IE 设置 > 连接 > 局域网设置 > 代理服务器。
    • 仍然收到错误“ServicePointManager 不支持使用 https 方案的代理。” :(
    【解决方案2】:

    你可能想看看System.Net.HttpWebRequest.Proxy on MSDN
    这提供了如何设置代理身份验证的详细信息。

    这个 SO 答案还有一个工作代码示例:https://stackoverflow.com/a/9603791/204690

    例如:

    // Create a new request to the mentioned URL.               
    HttpWebRequest myWebRequest= (HttpWebRequest)WebRequest.Create("http://www.microsoft.com");
    
    // Obtain the 'Proxy' of the  Default browser.  
    IWebProxy proxy = myWebRequest.Proxy;
    
    if (proxy != null)
    {
        // Create a NetworkCredential object and associate it with the  
        // Proxy property of request object.
        proxy.Credentials=new NetworkCredential(username,password);
        // or 
        proxy.UseDefaultCredentials = true; 
    
        // try forcing the proxy to use http (just to the proxy not from proxy to server)
        UriBuilder proxyAddress = new UriBuilder(proxy.Address);
        proxyAddress.Scheme = "http";
    
        myWebRequest.Proxy=proxy;
    }
    HttpWebResponse myWebResponse=(HttpWebResponse)myWebRequest.GetResponse();
    

    【讨论】:

    • 您好,我尝试了第二个...仍然出现错误...“ServicePointManager 不支持使用 https 方案的代理。”
    • 您可能需要强制代理为 http 而不是 https。
    【解决方案3】:

    对我来说,它就像告诉它使用 DefaultCredentials 一样简单(尽管我还没有弄清楚为什么它不能默认使用这些):

    request.Proxy.Credentials = (System.Net.NetworkCredential)System.Net.CredentialCache.DefaultCredentials;
    

    【讨论】:

      【解决方案4】:

      您可以先检查是否可以连接到代理。这是一个例子:

       System.Net.Sockets.Socket sock = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
                      sock.Connect(url, proxyPort);
                      if (sock.Connected == true)  // Port is in use and connection is successful
                      {
                          sock.Close();
                          return true;
                      }
                      else
                      {
                          sock.Close();
                      }`enter code here`
                      return false;
      

      【讨论】:

      • 你可以先看看我们能不能连接代理
      猜你喜欢
      • 1970-01-01
      • 2012-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-10
      相关资源
      最近更新 更多