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

안녕하세요 Anne, 그러면 proxyAddress가 무엇입니까 ?? – Tim

+0

프록시 주소로 uri를 전달했지만 여전히 오류가 발생합니다. 이번에는 "ServicePointManager가 https 구성표를 가진 프록시를 지원하지 않습니다." – Tim

+0

URL 앞에 '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를 사용하도록 말하는만큼 간단했다 왜 기본적으로 사용할 수 없는지 알아 냈습니다.) :

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

당신은 프록시에 먼저 연결할 수 있는지 확인할 수 있습니다. –

관련 문제