Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Connecting Through Proxy

Tags:

c#

proxy

I work in a office which requires all connections to be made through a specific http proxy. I need to write a simple application to query some values from a webserver - it's easy if there were no proxy. How can I make the C# application proxy-aware? How can I make any sort of connection through a proxy?

like image 304
KalEl Avatar asked Dec 21 '09 09:12

KalEl


People also ask

Bahasa C digunakan untuk apa?

Meskipun C dibuat untuk memprogram sistem dan jaringan komputer namun bahasa ini juga sering digunakan dalam mengembangkan software aplikasi. C juga banyak dipakai oleh berbagai jenis platform sistem operasi dan arsitektur komputer, bahkan terdapat beberepa compiler yang sangat populer telah tersedia.

C dalam Latin berapa?

C adalah huruf ketiga dalam alfabet Latin. Dalam bahasa Indonesia, huruf ini disebut ce (dibaca [tʃe]).

Bahasa C dibuat pertama kali oleh siapa dan tahun berapa?

Bahasa pemrograman C ini dikembangkan antara tahun 1969 – 1972 oleh Dennis Ritchie. Yang kemudian dipakai untuk menulis ulang sistem operasi UNIX. Selain untuk mengembangkan UNIX, bahasa C juga dirilis sebagai bahasa pemrograman umum.


2 Answers

This is easily achieved either programmatically, in your code, or declaratively in either the web.config or the app.config.

You can programmatically create a proxy like so:

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(); 

You're basically assigning the WebProxy object to the request object's proxy property. This request will then use the proxy you define.

To achieve the same thing declaratively, you can do the following:

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

within your web.config or app.config. This sets a default proxy that all http requests will use. Depending upon exactly what you need to achieve, you may or may not require some of the additional attributes of the defaultProxy / proxy element, so please refer to the documentation for those.

like image 187
CraigTP Avatar answered Oct 10 '22 09:10

CraigTP


If you are using WebClient, it has a Proxy property you can use.

As other have mentioned, there are several ways to automate proxy setting detection/usage

Web.Config:

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

Use of the WebProxy class as described in this article.


You can also cofigure the proxy settings directly (config or code) and your app will then use those.

Web.Config:

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

Code:

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(); 
like image 27
Oded Avatar answered Oct 10 '22 11:10

Oded