iOS9 apple changed the http protocol to the https protocol method
- 2020-05-13 03:26:04
- OfStack
Solutions:
Join key in info.plist
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
The following is an introduction to access to the http and https protocols in ios
Recently, I worked on a project. At the beginning, I used HTTP protocol to realize the interaction between the client and the server. Later, I needed to change it to HTTPS protocol. Some problems were found in the process of modification, and the solutions are as follows:
HTTP:
NSString *urlString =[NSString stringWithFormat:@"https://127.0.0.1/default.aspx?USER=%@",@"111"];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"GET"];
NSHTTPURLResponse* urlResponse = nil;
NSError *error = [[NSError alloc] init];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
NSMutableString *result = [[NSMutableString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"The result string is :%@",result);
HTTPS
Events trigger
{
NSString *urlString =[NSString stringWithFormat:@"https://127.0.0.1/default.aspx?USER=%@",@"111"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:5];
// Set the request mode as get
[request setHTTPMethod:@"GET"];
// Adding user sessions id
[request addValue:@"text/html" forHTTPHeaderField:@"Content-Type"];
// Connection send request
finished = false;
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
// Block the thread and wait for the end
while(!finished) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse*)response
{}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//[_waitingDialog dismissWithClickedButtonIndex:0 animated:NO];
[connection release];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
}
- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection{
return NO;
}
// The following two paragraphs are the focus, to the server side single HTTPS Validation, iOS The client ignores certificate validation.
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
NSLog(@"didReceiveAuthenticationChallenge %@ %zd", [[challenge protectionSpace] authenticationMethod], (ssize_t) [challenge previousFailureCount]);
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]){
[[challenge sender] useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
[[challenge sender] continueWithoutCredentialForAuthenticationChallenge: challenge];
}
}
NSLog(@"get the whole response");
//[receivedData setLength:0];
}
// Process the data
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
}