프록시를 통해 연결하는 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();
앱이 시스템 기본 프록시를 사용하도록하려면 다음을 Application.exe.config에 추가합니다 (여기서 application.exe는 애플리케이션 이름 임).
<system.net>
<defaultProxy enabled="true" useDefaultCredentials="true">
<proxy usesystemdefault="true" bypassonlocal="true" />
</defaultProxy>
</system.net>
자세한 내용은 System.Net 의 MSDN 문서 에서 찾을 수 있습니다.
이 코드를 사용해보십시오. 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);
}
}
이 한 줄짜리가 저에게 효과적입니다.
WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultNetworkCredentials;
CredentialCache.DefaultNetWorkCredentials
Internet Explorer에 설정된 프록시 설정입니다.
WebRequest.DefaultWebProxy.Credentials
응용 프로그램의 모든 인터넷 연결에 사용됩니다.
Foole의 코드는 나에게 완벽하게 작동했지만 .NET 4.0에서는 Proxy가 NULL인지 확인하는 것을 잊지 마십시오. 이는 프록시가 구성되지 않았 음을 의미합니다 (회사 환경 외부).
So here's the code that solved my problem with our corporate proxy
WebClient web = new WebClient();
if (web.Proxy != null)
web.Proxy.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
This code has worked for me:
WebClient wc = new WebClient();
wc.Proxy.Credentials = CredentialCache.DefaultCredentials;
Automatic proxy detection is a process by which a Web proxy server is identified by the system and used to send requests on behalf of the client. This feature is also known as Web Proxy Auto-Discovery (WPAD). When automatic proxy detection is enabled, the system attempts to locate a proxy configuration script that is responsible for returning the set of proxies that can be used for the request.
http://msdn.microsoft.com/en-us/library/fze2ytx2.aspx
var getHtmlWeb = new HtmlWeb() { AutoDetectEncoding = false, OverrideEncoding = Encoding.GetEncoding("iso-8859-2") };
WebProxy myproxy = new WebProxy("127.0.0.1:8888", false);
NetworkCredential cred = (NetworkCredential)CredentialCache.DefaultCredentials;
var document = getHtmlWeb.Load("URL", "GET", myproxy, cred);
I am going to use an example to add to the answers above.
I ran into proxy issues while trying to install packages via Web Platform Installer
That too uses a config file which is WebPlatformInstaller.exe.config
I tried the edits suggest in this IIS forum which is
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.net>
<defaultProxy enabled="True" useDefaultCredentials="True"/>
</system.net>
</configuration>
and
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.net>
<defaultProxy>
<proxy
proxyaddress="http://yourproxy.company.com:80"
usesystemdefault="True"
autoDetect="False" />
</defaultProxy>
</system.net>
</configuration>
None of these worked.
What worked for me was this -
<system.net>
<defaultProxy enabled="true" useDefaultCredentials="false">
<module type="WebPI.Net.AuthenticatedProxy, WebPI.Net, Version=1.0.0.0, Culture=neutral, PublicKeyToken=79a8d77199cbf3bc" />
</defaultProxy>
</system.net>
The module needed to be registered with Web Platform Installer in order to use it.
참고URL : https://stackoverflow.com/questions/1938990/c-sharp-connecting-through-proxy
'development' 카테고리의 다른 글
indexedDB를 삭제하는 방법? (0) | 2020.09.10 |
---|---|
JUnit assertEquals (더블 예상, 더블 실제, 더블 엡실론) (0) | 2020.09.09 |
Ruby on Rails-애플리케이션 레이아웃없이 액션을 렌더링하려면 어떻게해야합니까? (0) | 2020.09.09 |
.bashrc / .profile이 새 tmux 세션 (또는 창)에로드되지 않는 이유는 무엇입니까? (0) | 2020.09.09 |
C 컴파일러가 정렬 패딩을 제거하기 위해 구조체 멤버를 재 배열 할 수없는 이유는 무엇입니까? (0) | 2020.09.09 |