[c#] 프록시를 통해 연결하는 C #

특정 http 프록시를 통해 모든 연결이 이루어져야하는 사무실에서 일합니다. 웹 서버에서 일부 값을 쿼리하는 간단한 응용 프로그램을 작성해야합니다. 프록시가 없으면 쉽습니다. C # 애플리케이션이 프록시를 인식하도록하려면 어떻게해야합니까? 프록시를 통해 연결하려면 어떻게해야합니까?



답변

이는 코드에서 프로그래밍 방식으로 또는 web.config 또는 app.config에서 선언적으로 쉽게 수행 할 수 있습니다.

다음과 같이 프로그래밍 방식으로 프록시를 만들 수 있습니다.

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("[ultimate destination of your request]");
WebProxy myproxy = new WebProxy("[your proxy address]", [your proxy port number]);
myproxy.BypassProxyOnLocal = false;
request.Proxy = myproxy;
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse) request.GetResponse();

기본적으로 WebProxy개체를 request개체의 proxy속성에 할당합니다 . 이 request후 사용할 proxy사용자가 정의합니다.

선언적으로 동일한 작업을 수행하려면 다음을 수행 할 수 있습니다.

<system.net>
  <defaultProxy>
    <proxy
      proxyaddress="http://[your proxy address and port number]"
      bypassonlocal="false"
    />
  </defaultProxy>
</system.net>

web.config 또는 app.config 내에서. 모든 http 요청이 사용할 기본 프록시를 설정합니다. 정확히 무엇을 달성해야하는지에 따라 defaultProxy / proxy 요소 의 추가 속성 중 일부가 필요할 수도 있고 필요하지 않을 수도 있으므로 해당 문서를 참조하십시오.


답변

을 사용하는 경우 사용할 수 WebClient있는 프록시 속성이 있습니다.

다른 사람들이 언급했듯이 프록시 설정 감지 / 사용을 자동화하는 몇 가지 방법이 있습니다.

Web.Config :

<system.net>
   <defaultProxy enabled="true" useDefaultCredentials="true">
     <proxy usesystemdefault="true" bypassonlocal="true" />
   </defaultProxy>
</system.net>

이 문서 에서 설명하는 WebProxy 클래스 사용 .


프록시 설정을 직접 (구성 또는 코드) 구성 할 수도 있으며 앱에서이를 사용합니다.

Web.Config :

<system.net>
  <defaultProxy>
    <proxy
      proxyaddress="http://[proxy address]:[proxy port]"
      bypassonlocal="false"
    />
  </defaultProxy>
</system.net>

암호:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("url");
WebProxy myproxy = new WebProxy("[proxy address]:[proxy port]", false);
request.Proxy = myproxy;
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse) request.GetResponse();


답변

이 코드를 사용해보십시오. http 요청을하기 전에 호출하십시오. 코드는 Internet Explorer 설정의 프록시를 사용합니다.하지만 한 가지는 proxy.Credentials = ....프록시 서버가 NTLM 인증 Internet Acceleration Server이기 때문에 사용 합니다. 재즈를 줘.

static void setProxy()
{
    WebProxy proxy = (WebProxy)WebProxy.GetDefaultProxy();
    if(proxy.Address != null)
    {
        proxy.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
        WebRequest.DefaultWebProxy = new System.Net.WebProxy(proxy.Address, proxy.BypassProxyOnLocal, proxy.BypassList, proxy.Credentials);
    }
}


답변

앱이 시스템 기본 프록시를 사용하도록하려면 다음을 Application.exe.config에 추가합니다 (여기서 application.exe는 애플리케이션의 이름 임).

<system.net>
   <defaultProxy enabled="true" useDefaultCredentials="true">
   <proxy usesystemdefault="true" bypassonlocal="true" />
   </defaultProxy>
</system.net>

자세한 내용은 System.NetMSDN 문서 에서 찾을 수 있습니다.


답변

이 한 줄짜리가 저에게 효과적입니다.

WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultNetworkCredentials;

CredentialCache.DefaultNetWorkCredentials Internet Explorer에 설정된 프록시 설정입니다.

WebRequest.DefaultWebProxy.Credentials 응용 프로그램의 모든 인터넷 연결에 사용됩니다.


답변

Foole의 코드는 나에게 완벽하게 작동했지만 .NET 4.0에서는 Proxy가 NULL인지 확인하는 것을 잊지 마십시오. 이는 프록시가 구성되지 않았 음을 의미합니다 (회사 환경 외부).

그래서 여기 우리 회사 프록시의 문제를 해결 한 코드가 있습니다.

WebClient web = new WebClient();
if (web.Proxy != null)
    web.Proxy.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;


답변

이 코드는 나를 위해 일했습니다.

WebClient wc = new WebClient();
wc.Proxy.Credentials = CredentialCache.DefaultCredentials;