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