How IOS HTTPS access bypasses invalid certificate permission access

Source: Internet
Author: User

One, because a Web page access in the project involves multiple certificates, each certificate only needs to be bypassed, but we cannot tell when the certificate was changed. Therefore, the method of the Internet is always unable to solve the problem perfectly. The following methods can be commonly encountered in a variety of situations, but also very grateful to netizens closure to provide me with this method.


@property nsurlrequest *failedrequest;

@property nsurlconnection *connection;

@property Nsmutableset *whitelist;


-(BOOL) WebView: (uiwebview *) WebView shouldstartloadwithrequest: (nsurlrequest *) Request Navigationtype: (uiwebviewnavigationtype) navigationtype

{

nsurl *requesturl =[request URL];

NSLog(@ "This is Web URL:%@", Requesturl);

if ([[[Requesturl scheme ] isequaltostring: @ "http" ])

{

nsstring *requeststring = [Requesturl absolutestring];

if ([requeststring hassuffix:@ "callback.html" ]) {

[self. Navigationcontroller popviewcontrolleranimated:YES];

}

}

nsstring* scheme = [[Request URL] scheme];

if ([Scheme isequaltostring:@ "https"]) {

// If it is https: then use nsurlconnection to re-send the request. So in the process of the request to request the URL to do the trust processing.

BOOL result = [_whitelist containsobject: request];

if (!result) {

_failedrequest = Request;

_connection = [[nsurlconnection alloc] initwithrequest: Request Delegate :Self];

}

return result;

}

return YES;

}


To server-side single- HTTPS authentication, theiOS client ignores certificate validation.

-(void) connection: (nsurlconnection *) connection Willsendrequestforauthenticationchallenge: ( nsurlauthenticationchallenge *) Challenge {

if ([Challenge. Protectionspace. Authenticationmethod isequaltostring:nsurlauthenticationmethodservertrust]) {

[Challenge. Sender usecredential: [nsurlcredential credentialfortrust: Challenge. Protectionspace . Servertrust ] forauthenticationchallenge: challenge];

}

[Challenge. Sender Continuewithoutcredentialforauthenticationchallenge: challenge];

}


-(void) connection: (nsurlconnection *) connection didreceiveresponse: (nsurlresponse *) Presponse

{

[_whitelist addobject:_failedrequest];

[Connection cancel];

[_webview loadrequest:_failedrequest];

}


-(BOOL) connection: (nsurlconnection *) connection canauthenticateagainstprotectionspace: ( Nsurlprotectionspace *) Protectionspace {

return [Protectionspace. Authenticationmethod isequaltostring:nsurlauthenticationmethodservertrust];

}


-(void) connection: (nsurlconnection *) connection Didreceiveauthenticationchallenge: ( Nsurlauthenticationchallenge *) Challenge {

if ([Challenge. Protectionspace. Authenticationmethod isequaltostring:nsurlauthenticationmethodservertrust])

[Challenge. Sender usecredential: [nsurlcredential credentialfortrust: Challenge. Protectionspace . Servertrust ] forauthenticationchallenge: challenge];

[Challenge. Sender Continuewithoutcredentialforauthenticationchallenge: challenge];

}


Second, some HTTPS requests require parameters with parameter user-agent, which can be obtained by means of a method.

1. Setting properties

{

UIWebView *_web;

}

2. Call method in Viewdidload: [ self createhttprequest];

-(void) Createhttprequest {

_web = [[uiwebview alloc] init];

_web. delegate = self;

[_web loadrequest: [nsurlrequest requestwithurl:

[nsurl urlwithstring:@ "http://www.eoe.cn"]];

NSLog(@ "%@", [ self useragentstring]);

}


-(nsstring *) useragentstring

{

while (self.) useragent = = Nil)

{

NSLog(@ "%@", @ "in while");

[[nsrunloop currentrunloop] runmode:nsdefaultrunloopmode c6> beforedate: [nsdate distantfuture]];

}

return self. useragent ;

}

3. Add the Shouldstartloadwithrequest to the request:

if (WebView = = _web) {

_useragent = [Request Valueforhttpheaderfield:@ "User-agent"];

return NO;

}




How IOS HTTPS access bypasses invalid certificate permission access

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.