2012-10-22 3 views
9

나는 인터넷에서 HTML 데이터를 obtaing 다음 코드를 사용 :WebProxy가 오류 : 프록시 인증 필요

WebProxy p = new WebProxy("localproxyIP:8080", true); 
p.Credentials = new NetworkCredential("domain\\user", "password"); 
WebRequest.DefaultWebProxy = p; 
WebClient client = new WebClient(); 
string downloadString = client.DownloadString("http://www.google.com"); 

그러나 다음과 같은 오류가 나타난다 : "프록시 인증 필요"를. 기본 프록시 설정이없는 특별 계정으로 Windows 서비스에서 실행되는 코드 때문에 기본 프록시를 사용할 수 없습니다. 그래서 코드에서 모든 프록시 설정을 specidy하고 싶습니다. 이 오류를 해결하는 방법을 알려주세요.

+1

시도 \ \ part ('WebClient'에'IDisposable'을 구현했기 때문에'using()'문을 감싸는 것을 기억하십시오) – jwaliszko

답변

18

당신은 ..

WebProxy p = new WebProxy("localproxyIP:8080", true); 
p.Credentials = new NetworkCredential("domain\\user", "password"); 
WebRequest.DefaultWebProxy = p; 
WebClient client = new WebClient(); 
**client.Proxy = p;** 
string downloadString = client.DownloadString("http://www.google.com"); 
+0

'WebRe quest.DefaultWebProxy = p'이면 충분합니다. – jwaliszko

+0

WebRequest.Create()를 사용하고 WebRequest.GetResponse()를 호출 한 후에 맞으면 좋습니다. 그러나이 경우 그는 WebClient를 사용하여 요청을 작성하므로 Proxy가 동일하지 않습니다. – 2GDev

+2

실제로'DownloadString' 메쏘드는 내부적으로'WebRequest.Create()'에 의해 생성 된'WebRequest' 객체를 사용합니다. 프록시가 특히'WebClient' 객체로 설정되지 않았다면,'WebRequest' 객체는'WebRequest.DefaultWebProxy = p' 문에 의해 설정되는'WebRequest.InternalDefaultWebProxy' 속성으로부터 얻어진 프록시를 사용합니다; – jwaliszko

1

이 코드를 시도

var transferProxy = new WebProxy("localproxyIP:8080", true); 
transferProxy.Credentials = new NetworkCredential("user", "password", "domain"); 
var transferRequest = WebRequest.Create("http://www.google.com"); 
transferRequest.Proxy = transferProxy; 
HttpWebResponse transferResponse = 
    (HttpWebResponse)transferRequest.GetResponse(); 
System.IO.Stream outputStream = transferResponse.GetResponseStream(); 
39

이 나를 위해 일한 WebClient.Proxy 속성을 설정했습니다 : 도메인을 제거

IWebProxy defaultWebProxy = WebRequest.DefaultWebProxy; 
defaultWebProxy.Credentials = CredentialCache.DefaultCredentials; 
client = new WebClient 
    { 
     Proxy = defaultWebProxy 
    }; 
string downloadString = client.DownloadString(...); 
+3

멋진! 이것은 모든 WebClient 샘플의 일부 여야합니다. –