The previous article in C # processing the certificate, this time using OC to try it, in fact, the basic principle is tired, is generally covered by their own several functions, and then handle the certificate related things.
Create a class yourself, such as:
@interface myurlconnection:nsobject<nsurlconnectiondelegate, nsurlconnectiondatadelegate>-(void) Sendpostrequest: (nsurl*) URL postdata: (nsdata*) postdata; @end
I prefer to put some non-public members in the extension, save the others to see.
@interface myurlconnection () { nsurlconnection* _conn; nsmutabledata* _receiveddata; BOOL _success;} @end
Then implement the Sendpostrequest function:
-(void) Sendpostrequest: (nsurl *) URL postdata: (NSData *) postdata{ nsmutableurlrequest *request = [[ Nsmutableurlrequest alloc] init]; [Request Seturl:url]; [Request sethttpmethod:@ "POST"]; NSString *postlength = [NSString stringwithformat:@ "%lu", (unsigned long) [postdata length]]; [Request Setvalue:postlength forhttpheaderfield:@ "Content-length"]; [Request setvalue:@ "Text/plain, Charset=utf-8" forhttpheaderfield:@ "Content-type"]; [Request Sethttpbody:postdata]; NSLog (@ "All headers:%@", request.allhttpheaderfields); _conn = [nsurlconnection connectionwithrequest:request delegate:self]; [_conn start]; _receiveddata = nil; _success = NO;}
Implement several delegate functions for Nsurlconnection: (the nsurlconnection operation above is asynchronous)
-(void) connection: (nsurlconnection*) connection didreceiveresponse: (Nsurlresponse *) response{if ([Response isKindOf Class:[nshttpurlresponse class]] {Nsinteger StatusCode = [(Nshttpurlresponse *) response StatusCode]; if (statusCode! =) {NSLog (@ "%s failed status code%ld", __function__, (long) statusCode); _success = NO; } else {_success = YES; }} _receiveddata = nil;} -(void) connection: (Nsurlconnection *) connection didreceivedata: (NSData *) data{if (!_receiveddata) {_received Data = [[Nsmutabledata alloc] initwithdata:data]; } else {[_receiveddata appenddata:data]; }}-(void) connectiondidfinishloading: (nsurlconnection *) connection{nsstring* data = [[NSString alloc] Initwithdata:_r Eceiveddata encoding:nsutf8stringencoding];//NSLog (@ "nxurlconnection, finished loading:%@", data); nserror* err = nil; if (!_success) {err = [[Nserror all]OC] initwithdomain:@ "HTTPS access Failed" code:1 Userinfo:nil]; } else {//Success}}-(void) connection: (Nsurlconnection *) connection didfailwitherror: (Nserror *) error{ _success = NO; NSLog (@ "Nxurlconnection error:%@", [Error localizeddescription]);} -(void) connection: (Nsurlconnection *) connection Willsendrequestforauthenticationchallenge: ( Nsurlauthenticationchallenge *) challenge{NSLog (@ "Willsendrequestforauthentiationchallenge:%@", Challenge.protectionSpace.authenticationMethod); if ([Challenge.protectionSpace.authenticationMethod Isequaltostring:nsurlauthenticationmethodservertrust]) {NSLog (@ "cert Host:%@", challenge.protectionSpace.host); nsurlcredential* cred = [nsurlcredential credentialfortrust:[[challenge protectionspace] serverTrust]]; [[Challenge Sender] usecredential:cred Forauthenticationchallenge:challenge]; }}
The first few functions can be literally understood to be some processing of access, such as failure to succeed or something. The key is the last function.
This function can handle the certificate related things, such as obtaining certificate information, the above example is no matter what certificate, all pass. Of course, we can also according to the specific certificate information to make some judgments, such as not the company's own certificate, the cancellation of operations or something.
Call:
myurlconnection* conn = [[Myurlconnection alloc]init]; [Conn sendpostrequest:@ "https://xxxxx" postdata:@ "test"];
This way, even if the test site's certificate is a test certificate that you create yourself, you can successfully access the data.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HTTPS Certificate Processing OBJECTIVE-C implementation