IOS9 Apple changed the original HTTP protocol to the HTTPS protocol _ios

Source: Internet
Author: User
Tags session id

Workaround:

Add key to Info.plist

<key>NSAppTransportSecurity</key>
<dict>
<key>nsallowsarbitraryloads</key >
<true/>
</dict>

Let me introduce you to the HTTP and HTTPS protocols in iOS.

Recently done a project, began to use the HTTP protocol to achieve client and server-side interaction, and later need to change to HTTPS protocol. Some problems were identified during the modification process, and the solution was as follows:

HTTP:

NSString *urlstring =[nsstring stringwithformat:@ "https://127.0.0.1/default.aspx?user=%@", @ "D"];
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];

HTTPS

Event triggers

{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 to get [request sethttpmethod:@ ' get '];
Add user session ID [request addvalue:@ "text/html" forhttpheaderfield:@ "Content-type"];
Connection Send request finished = false;
Nsurlconnection *conn = [[Nsurlconnection alloc] initwithrequest:request delegate:self]; Blocking threads, waiting for 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 key to server-side single HTTPS authentication, IOS 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" whole RESPONSE ");
[Receiveddata setlength:0];  }//Processing data-(void) connection: (Nsurlconnection *) connection didreceivedata: (NSData *)

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.