2011-11-22 4 views
3
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create("https://www.example.com"); 
NetworkCredential nc = new NetworkCredential("myname", "mypass"); 
WebProxy myproxy = new WebProxy("192.168.1.1:8080", false); 
myHttpWebRequest.Proxy = myproxy; 
myHttpWebRequest.Proxy = WebRequest.DefaultWebProxy; 
myHttpWebRequest.Method = "GET"; 

HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); 
MessageBox.Show("Ok"); 

이 코드를 사용하여 웹 사이트 (C# .net 데스크탑 응용 프로그램)에 연결합니다. 하지만 다음과 같은 오류 메시지가 표시됩니다.프록시 인증 오류

The remote server returned an error: (407) Proxy Authentication Required.

어떻게 해결할 수 있습니까?

+0

Nc를 WebProxy 생성자로 전달해야합니까? – spb

답변

2

현재 프록시에서 자격 증명을 사용하고 있지 않습니다. 여기 example adapted from MSDN of how to use your NetworkCredential입니다 :

class Downloader 
{ 
    static void Main(string[] args) 
    { 
     NetworkCredential nc = new NetworkCredential(args[0], args[1]); 
     WebProxy proxy = new WebProxy(args[2], false); 
     proxy.Credentials = nc; 

     WebRequest request = new WebRequest(args[3]); 
     request.Proxy = proxy; 
     using (WebResponse response = request.GetResponse()) 
     { 
      Console.WriteLine(
       @"{0} - {1} bytes", 
       response.ContentType, 
       response.ContentLength); 
     } 
    } 
} 

내가 컴파일이 완전한 예제를 실행하면 : 내 실제 사용자가/내 작업 계정에 전달하고 프록시 사용 물론

C:\cs>csc proxy.cs 
Microsoft (R) Visual C# 2010 Compiler version 4.0.30319.1 
Copyright (C) Microsoft Corporation. All rights reserved. 


C:\cs>proxy user pass http://proxy:80 http://www.google.com 
text/html; charset=ISO-8859-1 - 31398 bytes 

C:\cs> 

.

+0

'NetworkCredential' 대신'CredentialCache' +'NetworkCredential'을 사용하는 이유는 무엇입니까? – abatishchev

+0

처음에는 잘못된 예를 사용했기 때문에. 고침, 고마워. – user7116

+0

감사합니다 Sixletter – Kevin