Settings of. Net Windows client in proxy environment

Source: Internet
Author: User

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:

    1. After the code is configured as the default proxy
    2. Check whether the network is connected.
    3. If there is no connectivity, especially the code for accessing the verification error 407,
    4. 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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.