IOS development ASIHTTPRequest uses proxy connection

Source: Internet
Author: User

ASIHTTPRequest detects the proxy settings of the system and automatically uses the proxy for request. Starting from version 1.0.6, it also supports the PAC file and the proxy that requires authorization.

By default, ASIHTTPRequest will attempt to automatically detect proxy settings. Of course, you can view your manual settings:

 
 
  1. // Manually set the Proxy Server
  2. NSURL * url = [NSURL URLWithString: @ "http://www.dreamingwish.com"];
  3. ASIHTTPRequest * request = [ASIHTTPRequest requestWithURL: url];
  4. [Request setProxyHost: @ "192.168.0.1"];
  5. [Request setProxyPort: 3128];
  6.  
  7. // Another method is to use the agent to configure the script file
  8. // (It is best to use a local pac file)
  9. [Request setPACurl: [NSURL URLWithString: @ "path/to/test. pac"];
Proxy requiring authorization

On Mac OS, ASIHTTPRequest can automatically detect the creden of the proxy that requires authorization, provided that the creden。 are set in system settings ). On iOS, ASIHTTPRequest cannot automatically detect authorization creden。. Therefore, you must manually use delegate to obtain the appropriate creden。 from your controller or user, or ask ASIAuthenticationDialog to obtain credenalog from the user. Once a valid proxy credential is obtained, the credential is stored in keychian only if useKeychainPersistence is enabled.

Manually specify creden for proxy
 
 
  1. NSURL * url = [NSURL URLWithString: @ "http://www.dreamingwish.com"];
  2. ASIHTTPRequest * request = [ASIHTTPRequest requestWithURL: url];
  3. [Request setProxyHost: @ "192.168.0.1"];
  4. [Request setProxyPort: 3128];
  5.  
  6. // Set username and password for the proxy that requires authorization
  7. [Request setProxyUsername: @ "bencopsey"];
  8. [Request setProxyPassword: @ "password"];
  9.  
  10. // For NTLM proxy, you also need to set domain (NTLM proxy function is not tested)
  11. [Request setProxyDomain: @ "la. la. land"];
Use delegate to provide proxy credenproxy

The working principle of this feature is the same as that of "using delegate to provide HTTP Authorization". There is only one difference: Your delegate must respond to the proxyAuthenticationNeededForRequest: function.

Use the built-in authorization dialog box only for iOS)

This feature is attributed to the new ASIAuthenticationDialog class of version 1.0.8. Used to obtain creden。 from the user to authorize webserver or proxy.

If your delegate does not respond to the proxyAuthenticationNeededForRequest: function, ASIHTTPRequest will display a prompt to the customer asking the user to enter the authorization credential by default. With ASIHTTPRequest, developers no longer need to write additional code to display the authorization dialog box, because ASIHTTPRequest will display it by default.

The proxy authorization dialog box is not displayed when synchronous request is used.

If you do not limit the use of the proxy authorization dialog box, you can either implement proxyAuthenticationNeededForRequest:, or set shouldPresentProxyAuthenticationDialog to false. In this case, your program cannot connect to the proxy ). To change the style of the dialog box, you must inherit the ASIHTTPRequest class and override showProxyAuthenticationDialog to display your own dialog box or ASIAuthenticationDialog subclass.

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.