2013-01-18 155 views
3

我有一个要求...我想从一个winforms访问一个URL(登录页面是网页)。我必须将凭据传递给该URL,并且响应应该是经过身份验证的网页(标记)的标记。错误(407)“需要代理验证”。

我写了一个函数,它会请求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(); 
} 

回答

4
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"); 
+0

嗨安妮,什么是proxyAddress? – Tim

+0

我通过uri作为代理地址,但仍然出现错误。但这次是因为“ServicePointManager不支持使用https方案的代理。” – Tim

+0

确保网址前缀为“http://”。参考[this](http://blogs.msdn.com/b/jpsanders/archive/2007/04/25/the-servicepointmanager-does-not-support-proxies-of-https-scheme-net-1- 1-sp1.aspx)和[this](http://stackoverflow.com/questions/954635/the-servicepointmanager-does-not-support-proxies-of-scheme) –

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(); 
+0

嗨,我尝试了第二个......仍然出现错误......“ServicePointManager不支持代理与https计划“。 – Tim

+0

您可能需要强制将代理设置为http不是https。 – Grhm

+0

还是一样的错误...:( – Tim

1

对我来说是为告诉它使用的DefaultCredentials(虽然我仍然天堂”一样简单t想通了它为什么不能默认使用这些):

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

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

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; 
+0

你可以检查我们可以先连接到代理服务器 –

相关问题