Here is a brief summary of experience
Previously, when I used smartclient + remoting to develop a business management system, I encountered an HTTP proxy configuration problem. At that time, because remoting could not automatically configure the proxy, the solution was to provide the proxy network configuration option on the logon interface, allowing users to manually enter the proxy server address, port, user name, and password.
For Web Service and WCF in. Net 4.0, You can automatically configure most proxy servers by setting webrequest. defaultwebproxy. Credentials = credentialcache. defaultnetworkcredentials. This method is actually to use the proxy server configuration in IE.
For more information about defanetworknetworkcredentials, see my other blog: defaultnetworkcredentials vs defacrecredentials.
Pass the aboveCodeAfter configuration, you cannot guarantee correct access. Therefore, you need to perform the following operations:
- After the code is configured as the default proxy
- Check whether the network is connected.
- If there is no connectivity, especially the code for accessing the verification error 407,
- Then you need to prompt the user to enter the proxy server user name and password
My implementation code is as follows:
Custom Code for set proxy // Reference: http://www.codeguru.com/csharp/csharp/cs_network/http/article.php/c16479 _ Canconnectedike =False ; Httpwebrequest httpreq = (httpwebrequest) webrequest. Create (" Http://www.itke.com.cn/ping.txt "); Httpreq. allowautoredirect = False ; Httpwebresponse httpres; webrequest. defaultwebproxy. Credentials = credentialcache. defaultnetworkcredentials; Try {Httpres = (httpwebresponse) httpreq. getresponse (); If (Httpres. statuscode = httpstatuscode. OK) {httpres. Close (); _ canconnectedike = True ;} Httpres. Close ();} Catch (Webexception ex ){ If (EX. Message. Contains ("407 ") {Proxyauthdialog dialog = New Proxyauthdialog (); dialog. showdialog (); webrequest. defaultwebproxy. Credentials = New Networkcredential (dialog. username, dialog. Password ); Try {Httpreq = (httpwebrequest) webrequest. Create (" Http://www.itke.com.cn/ping.txt "); Httpres = (httpwebresponse) httpreq. getresponse (); If (Httpres. statuscode = httpstatuscode. OK) {httpres. Close (); _ canconnectedike = True ;}} Catch (Exception ex1) {programbase. Logger. writeexception (ex1 );}}}
In addition to using code to configure the default proxy, you can also configure WCF in the configuration file as follows:
<Basichttpbinding>
<Binding Name="Myclientbinding" Proxyaddress="Http: // Gateway: 8080" Usedefawebwebproxy="False">
</Binding>
</Basichttpbinding>
Or
<Custombinding>
<Binding Name="Mycustomclientbinding">
<Binarymessageencoding />
<Httptransport Proxyaddress="Http: // Gateway: 8080" Usedefawebwebproxy="False" />
</Binding>
</Custombinding>
Or
<System.net>
<Defaultproxy Usedefaultcredentials="True">
<Proxy Bypassonlocal="False" Proxyaddress="Http: // Gateway: 8080" />
</Defaultproxy>
</System.net>