Http and https access code in ios

Source: Internet
Author: User
Tags session id

Recently, I started to use the HTTP protocol to implement interaction between the client and the server. Later I needed to change it to the HTTPS protocol. Some problems were found during the modification. The solution is as follows:

The code is as follows: Copy code

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

Event triggering

The code is as follows: Copy code

{

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 method to get

The code is as follows: Copy code

[Request setHTTPMethod: @ "GET"];

// Add the user session id

[Request addValue: @ "text/html" forHTTPHeaderField: @ "Content-Type"];

// Connection sends the request

Finished = false;

NSURLConnection * conn = [[NSURLConnection alloc] initWithRequest: request delegate: self];

// Clog the thread and wait for the end

While (! Finished ){

[[Nsunloop 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 sections are important. Server-side individual HTTPS verification is required, and the iOS client ignores certificate verification.

-(BOOL) connection :( NSURLConnection *) connection canAuthenticateAgainstProtectionSpace :( NSURLProtectionSpace *) protectionSpace {

Return [protectionSpace. authenticationMethod isinclutostring: NSURLAuthenticationMethodServerTrust];

}

-(Void) connection :( NSURLConnection *) connection didReceiveAuthenticationChallenge :( NSURLAuthenticationChallenge *) challenge {

NSLog (@ "didReceiveAuthenticationChallenge % @ % zd", [[challenge protectionSpace] authenticationMethod], (ssize_t) [challenge previusfailurecount]);

   

If ([challenge. protectionSpace. authenticationMethod isinclutostring: NSURLAuthenticationMethodServerTrust]) {

[[Challenge sender] useCredential: [NSURLCredential credentialForTrust: challenge. protectionSpace. serverTrust] forAuthenticationChallenge: challenge];

[[Challenge sender] continueWithoutCredentialForAuthenticationChallenge: challenge];

    }

}

NSLog (@ "get the whole response ");

// [ReceivedData setLength: 0];

}

// Process data

-(Void) connection :( NSURLConnection *) connection didReceiveData :( NSData *) data

{

}

Related Article

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.