How to access the Internet through a proxy server in C #

I am developing a Windows Form application using C #. The application performs its functions smoothly when connected to the Internet, but the real problem starts when we try the application in our college.

Our college connects to the network using proxy gateways. The proxy server is 192.168.120.5 and the proxy port is 8080 . Each student is assigned a unique username and password.

How can I get through this obstacle? I am trying to create a proxy connection with the target IP address:

 WebRequest request = WebRequest.Create("http://www.example.com"); request.Proxy = new WebProxy("192.168.120.5", 8080); 

Will this help me? If so, where can I enter my username and password credentials?

+2
source share
2 answers

yes, you can use this ... although it may be easier to use WebRequest.DefaultWebProxy

 WebProxy proxyObject = new WebProxy("http://proxyserver:80/",true); proxyObject.Credientials = new NetworkCredential(UserName, "bla"); WebRequest req = WebRequest.Create("http://www.contoso.com"); req.Proxy = proxyObject; 

although as an extra note you can use local default credentials by setting

 proxyObject.UseDefaultCredentials = true; 

If you try to use this, you must ensure that proxyObject.Credintials = null.
The msdn site on all of this is here MSDN WebProxy

+2
source

It will be a little late, but it can help other people in the same situation, first create a new class for your proxy configuration, as shown below

 using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Web; namespace Your_name_space { public class Proxy : IWebProxy { public ICredentials Credentials { get { return new NetworkCredential("username", "password", "domaine"); } set { } } public Uri GetProxy(Uri destination) { return new Uri("your_proxy_address:port"); } public bool IsBypassed(Uri host) { return false; } } } 

Then add this as a module to your application configuration in the config / system.net section.

  <system.net> <defaultProxy enabled="true" useDefaultCredentials="false" > <module type="Your_name_space.Proxy , your_assembly_name" /> </defaultProxy> </system.net> 

if you want to use the default session credentials, in the proxy class, replace this:

 new NetworkCredential("username", "password", "domaine"); 

by

 CredentialCache.DefaultCredentials; 

Hope this helps.

0
source

Source: https://habr.com/ru/post/916666/


All Articles