How to use proxy server in .net application
Proxyserverisuse to distribute internetinsecure way. Proxyserveractsas an intermediaryfor requestsfromclientsseekingresourcesfromotherserversorInternet.Manyorganicuse proxyserverto stopdirectaccess to outside resource . If youhave application whichhave alogicto call external API.Butdue toproxywe can't call APIdirectly. So,We needtoenable proxysettingsinourapplicationtocall externalAPI. We have twowaysto configure proxyin.netapplication. 1. Setupproxyin web.config 2. Use "WebProxy"classforenable proxy 1. Setupproxy in web.config You can use followingcode intoweb.config touse proxy. Use proxyaddresstodefine proxyserverpath. here our proxyserveraddressis"192.168.1.121:6588". <!--startproxysetting--> <system.net> <defaultProxy enabled="true" useDefaultCredentials="true"> <proxy usesystemdefault="True" proxyaddress="http://192.168.1.121:6588" bypassonlocal="True"/> <bypasslist> <add address="[a-z]+.bypasslist.com"/> <!--Providesasetof regularexpressionsthatdescribe addressesthatdonotuse the proxy. Localhost,Local IP addressisbestexample of bypasslist--> </bypasslist> </defaultProxy> </system.net> <!--endproxysetting--> defaultproxysettings Element Description enabled It specify that web proxy is enable or disable. The Default value is true. useDefaultCredentials It use for specify credentials for web proxy. The default value is false.
Element Description bypasslist It use to specify addresses which not use proxy in regular expressions. module Use to specify new new proxy module to the application. proxy Use to define a proxy server Settings Like - usesystemdefault proxyaddress bypassonlocal
2. Use "WebProxy" classforenable proxy Justadd followingkeysintoappSettingsinweb.configfile.Andenable/disableproxytouse inside code. <appSettings> <add key="handleProxyInCode"value="true"/> <add key="proxyServerIPAddress"value="192.168.1.12"/> <add key="proxyServerPort"value="6588"/> <add key="proxyUseCustomNetworkCredential"value="true"/> <add key="proxyNetworkCredentialUserName"value="userName"/> <add key="proxyNetworkCredentialPassword"value="password"/> <add key="proxyNetworkCredentialDomain"value="domain"/> </appSettings> We are using WebProxy class in "ProxyFromCode"API Use of proxy in application usingSystem; usingSystem.Collections.Generic; usingSystem.Configuration; usingSystem.IO; usingSystem.Linq; usingSystem.Net; usingSystem.Web; usingSystem.Web.Http; namespace WebProxyTest.API { [RoutePrefix("API/WebProxyTest")] publicclassWebProxyController:ApiController { ///<summary> ///Testproxysettingsfrom web.config ///</summary> /// [Route("Test1")] [HttpGet] publicIHttpActionResultProxyFromConfig() { stringurl = @"http://jsonip.com";
HttpWebRequestrequest=WebRequest.Create(url) asHttpWebRequest; request.ContentType ="application/json"; var response =request.GetResponse(); StreamReaderresponsestream=new StreamReader(response.GetResponseStream()); returnJson( responsestream.ReadToEnd()); } ///<summary> ///Testproxysettingstohandle fromcode ///</summary> [Route("Test2")] [HttpGet] publicIHttpActionResultProxyFromCode() { stringurl = @"http://jsonip.com"; HttpWebRequestrequest=WebRequest.Create(url) asHttpWebRequest; request.ContentType ="application/json"; //checkthat"handleProxyInCode"istrue inweb.configfile bool enableProxyInCode = Convert.ToBoolean(ConfigurationManager.AppSettings["handleProxyInCode"]); //runthiscode when"handleProxyInCode"istrue if (enableProxyInCode) { // FetchproxyserversettingsfromAppSettingssectionof Web.Configfile stringproxyServerAddress=ConfigurationManager.AppSettings["proxyServerIPAddress"]; intproxyServerPort=Convert.ToInt16(ConfigurationManager.AppSettings["proxyServerPort"]); // Setproxycredentialsfromweb.configfile WebProxy proxy=new WebProxy(proxyServerAddress,proxyServerPort); proxy.Credentials=GetNetworkCredentialForProxy(); request.Proxy=proxy; } var response =request.GetResponse(); StreamReaderresponsestream=new StreamReader(response.GetResponseStream()); returnJson(responsestream.ReadToEnd()); } ///<summary> ///Returnnetworkcredentialforproxyfromweb.configfile
///</summary> ///<returns><typeparamref name="ICredentials"/></returns> private ICredentials GetNetworkCredentialForProxy() { bool proxyUseCustomNetworkCredential = Convert.ToBoolean(ConfigurationManager.AppSettings["proxyUseCustomNetworkCredential"]); if (proxyUseCustomNetworkCredential) { stringuserName =ConfigurationManager.AppSettings["proxyNetworkCredentialUserName"]; stringpassword= ConfigurationManager.AppSettings["proxyNetworkCredentialPassword"]; stringdomain= ConfigurationManager.AppSettings["proxyNetworkCredentialDomain"]; returnnew NetworkCredential(userName,password,domain); } returnSystem.Net.CredentialCache.DefaultCredentials; } } } Thanks http://www.codeandyou.com http://www.codeandyou.com/2015/09/How-to-use- proxy-server-in-.net-application_16.html Keywords - How to use proxy server in .net application, use proxy in .net application, configuration of proxy in asp.net

How to use proxy server in .net application

  • 1.
    How to useproxy server in .net application
  • 2.
    Proxyserverisuse to distributeinternetinsecure way. Proxyserveractsas an intermediaryfor requestsfromclientsseekingresourcesfromotherserversorInternet.Manyorganicuse proxyserverto stopdirectaccess to outside resource . If youhave application whichhave alogicto call external API.Butdue toproxywe can't call APIdirectly. So,We needtoenable proxysettingsinourapplicationtocall externalAPI. We have twowaysto configure proxyin.netapplication. 1. Setupproxyin web.config 2. Use "WebProxy"classforenable proxy 1. Setupproxy in web.config You can use followingcode intoweb.config touse proxy. Use proxyaddresstodefine proxyserverpath. here our proxyserveraddressis"192.168.1.121:6588". <!--startproxysetting--> <system.net> <defaultProxy enabled="true" useDefaultCredentials="true"> <proxy usesystemdefault="True" proxyaddress="http://192.168.1.121:6588" bypassonlocal="True"/> <bypasslist> <add address="[a-z]+.bypasslist.com"/> <!--Providesasetof regularexpressionsthatdescribe addressesthatdonotuse the proxy. Localhost,Local IP addressisbestexample of bypasslist--> </bypasslist> </defaultProxy> </system.net> <!--endproxysetting--> defaultproxysettings Element Description enabled It specify that web proxy is enable or disable. The Default value is true. useDefaultCredentials It use for specify credentials for web proxy. The default value is false.
  • 3.
    Element Description bypasslist Ituse to specify addresses which not use proxy in regular expressions. module Use to specify new new proxy module to the application. proxy Use to define a proxy server Settings Like - usesystemdefault proxyaddress bypassonlocal
  • 4.
    2. Use "WebProxy"classforenable proxy Justadd followingkeysintoappSettingsinweb.configfile.Andenable/disableproxytouse inside code. <appSettings> <add key="handleProxyInCode"value="true"/> <add key="proxyServerIPAddress"value="192.168.1.12"/> <add key="proxyServerPort"value="6588"/> <add key="proxyUseCustomNetworkCredential"value="true"/> <add key="proxyNetworkCredentialUserName"value="userName"/> <add key="proxyNetworkCredentialPassword"value="password"/> <add key="proxyNetworkCredentialDomain"value="domain"/> </appSettings> We are using WebProxy class in "ProxyFromCode"API Use of proxy in application usingSystem; usingSystem.Collections.Generic; usingSystem.Configuration; usingSystem.IO; usingSystem.Linq; usingSystem.Net; usingSystem.Web; usingSystem.Web.Http; namespace WebProxyTest.API { [RoutePrefix("API/WebProxyTest")] publicclassWebProxyController:ApiController { ///<summary> ///Testproxysettingsfrom web.config ///</summary> /// [Route("Test1")] [HttpGet] publicIHttpActionResultProxyFromConfig() { stringurl = @"http://jsonip.com";
  • 5.
    HttpWebRequestrequest=WebRequest.Create(url) asHttpWebRequest; request.ContentType ="application/json"; varresponse =request.GetResponse(); StreamReaderresponsestream=new StreamReader(response.GetResponseStream()); returnJson( responsestream.ReadToEnd()); } ///<summary> ///Testproxysettingstohandle fromcode ///</summary> [Route("Test2")] [HttpGet] publicIHttpActionResultProxyFromCode() { stringurl = @"http://jsonip.com"; HttpWebRequestrequest=WebRequest.Create(url) asHttpWebRequest; request.ContentType ="application/json"; //checkthat"handleProxyInCode"istrue inweb.configfile bool enableProxyInCode = Convert.ToBoolean(ConfigurationManager.AppSettings["handleProxyInCode"]); //runthiscode when"handleProxyInCode"istrue if (enableProxyInCode) { // FetchproxyserversettingsfromAppSettingssectionof Web.Configfile stringproxyServerAddress=ConfigurationManager.AppSettings["proxyServerIPAddress"]; intproxyServerPort=Convert.ToInt16(ConfigurationManager.AppSettings["proxyServerPort"]); // Setproxycredentialsfromweb.configfile WebProxy proxy=new WebProxy(proxyServerAddress,proxyServerPort); proxy.Credentials=GetNetworkCredentialForProxy(); request.Proxy=proxy; } var response =request.GetResponse(); StreamReaderresponsestream=new StreamReader(response.GetResponseStream()); returnJson(responsestream.ReadToEnd()); } ///<summary> ///Returnnetworkcredentialforproxyfromweb.configfile
  • 6.
    ///</summary> ///<returns><typeparamref name="ICredentials"/></returns> private ICredentialsGetNetworkCredentialForProxy() { bool proxyUseCustomNetworkCredential = Convert.ToBoolean(ConfigurationManager.AppSettings["proxyUseCustomNetworkCredential"]); if (proxyUseCustomNetworkCredential) { stringuserName =ConfigurationManager.AppSettings["proxyNetworkCredentialUserName"]; stringpassword= ConfigurationManager.AppSettings["proxyNetworkCredentialPassword"]; stringdomain= ConfigurationManager.AppSettings["proxyNetworkCredentialDomain"]; returnnew NetworkCredential(userName,password,domain); } returnSystem.Net.CredentialCache.DefaultCredentials; } } } Thanks http://www.codeandyou.com http://www.codeandyou.com/2015/09/How-to-use- proxy-server-in-.net-application_16.html Keywords - How to use proxy server in .net application, use proxy in .net application, configuration of proxy in asp.net