2013-01-18 159 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

你可以檢查我們可以先連接到代理服務器 –

相關問題