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:
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
{
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
[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
{
}
You can also refer to the following article.
Methods for accessing HTTPS sites in uiwebview