views:

94

answers:

3

Hello,

I want to ask about the iPhone application objective C problem.

I wrote a program to store the cookies and pass to another URL to retrieve the cookies. However, I found that one of the return status code is 0. The content of the html is empty.

Can any one help me? The following is my code.

// create a new mutable url
 NSMutableURLRequest *request_get2 = [[[NSMutableURLRequest alloc] init] autorelease];
    [request_get2 setURL:[NSURL URLWithString:@"http://www.example.com"]];  
 [request_get2 setHTTPMethod:@"GET"];
 [request_get2 setValue:@"text/html; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
 [request_get2 setValue:@"http://www.example.com" forHTTPHeaderField:@"Referer"];
 [request_get2 setHTTPShouldHandleCookies:YES];
        // cookiesString is a string, the format is "cookieName=cookieValue;"
 [request_get2 setValue: (NSString *) cookiesString forHTTPHeaderField:@"Cookie"];

 // doGet - response
 NSHTTPURLResponse *response_get2 = nil;
 NSError *error_get2 = nil;
 NSData *responseData_get2 = [NSURLConnection sendSynchronousRequest:request_get2 returningResponse:&response_get2 error:&error_get2];
 NSString *data_get2 = [[NSString alloc]initWithData:responseData_get2 encoding:NSUTF8StringEncoding];


 NSString *responseURL_get2 = [[response_get2 URL] absoluteString];           // null value
 NSString *responseTextEncodingName_get2 = [response_get2 textEncodingName];  // null value
 NSString *responseMIMEType_get2 = [response_get2 MIMEType];                  // null value
 NSUInteger *responseStatusCode_get2 = [response_get2 statusCode]; //[responseStatusCode intValue]; // the status code is 0

Thank you very much

+1  A: 

If you get a 0 for a response code, the response response_get2 probably was never initialized, which might point to a problem with the request unrelated to your web server.

You set error_get2, so check its value after the request is placed:

if (!error_get2) {
    NSString *responseURL_get2 = [[response_get2 URL] absoluteString];           // null value
    NSString *responseTextEncodingName_get2 = [response_get2 textEncodingName];  // null value
    NSString *responseMIMEType_get2 = [response_get2 MIMEType];                  // null value
    NSUInteger *responseStatusCode_get2 = [response_get2 statusCode]; //[responseStatusCode intValue]; // the status code is 0
}
else {
    NSLog(@"something went wrong: %@", [error_get2 userInfo]);
}
Alex Reynolds
thank you for your reply. You are right. would you mind to see my error on the following post.
Questions
Hi, alex, sorry to find you again, would you mind to see my new reply answer, I really need you help. Thank you.
Questions
A: 

Thank you for your replay. The following is the error in the console.

something went wrong: {
    NSErrorFailingURLKey = https://www.example.com;
    NSErrorFailingURLStringKey = "https://www.example.com";
    NSLocalizedDescription = "untrusted server certificate";
    NSURLErrorFailingURLPeerTrustErrorKey = <NSCFType: 0x3e930f0>;
    NSUnderlyingError = Error Domain=kCFErrorDomainCFNetwork Code=-1202 UserInfo=0x3e95fb0 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be “example.com” which could put your confidential information at risk.";
}

The error is about the cref of the 'HTTPS' certification.

As I have to connect to a server which will pass a certification to me, I must receive the certificate. Can you help me?

Thank you very much.

Questions
I found this Stack Overflow question: http://stackoverflow.com/questions/933331/how-to-use-nsurlconnection-to-connect-with-ssl-for-an-untrusted-cert
Alex Reynolds
thank you, alex. It is very useful. But I still have 1 questions, where should I put the code. I used to put the code within a .m class. But it did not work. I checked the apply developer website, I know those are pre-defined functions, but where should I call it? I also used to put the NSLog() to print the content, but I could not see the content of the NSLog. thank you very much.
Questions
A: 

Hi, Alex, sorry to find you again, but I really cannot solve the problems. I read different post form the Internet. One of the useful is this one (similar answer).

I add the following function in my .m class (I don't know it is correct or not). The following is the content.

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
    NSLog(@"\nused to call the canAuthenticateAgainstProtectionSpace\n");
    return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}

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

    NSLog(@"\nused to call the didReveiceAutheticationChallenge\n");
    //if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
        //if ([trustedHosts containsObject:challenge.protectionSpace.host])
            [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];

    [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];


}

And I add the new statement in the function.

// request in here ... same on the above

NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request_get2 delegate:self];

// response in here .. same on the above

however I read the same error from the console and the two NSLog from the message is displayed after the error message.

The following is the message from the console.

2010-07-06 12:48:15.381 ABC[1588:20b] 
something went wrong: {
    NSErrorFailingURLKey = https://www.example.com;
    NSErrorFailingURLStringKey = "https://www.example.com";
    NSLocalizedDescription = "untrusted server certificate";
    NSURLErrorFailingURLPeerTrustErrorKey = <NSCFType: 0x3e93b70>;
    NSUnderlyingError = Error Domain=kCFErrorDomainCFNetwork Code=-1202 UserInfo=0x3e96a50 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be “example.com” which could put your confidential information at risk.";
}

2010-07-06 12:48:15.385 ABC[1588:20b] 
Complete all the code
2010-07-06 12:48:16.669 ABC[1588:20b] 
used to call the canAuthenticateAgainstProtectionSpace
2010-07-06 12:48:16.670 ABC[1588:20b] 
used to call the didReveiceAutheticationChallenge

I already cancel the 2 if statements, but I still get wrong.

Can you help me . Thank you very much.

Questions
If you're not stuck to using NSURLRequest, ASIHTTPRequest would greatly simplify your code. You might want to use this framework, instead: e.g. http://allseeing-i.com/ASIHTTPRequest/How-to-use#disabling_secure_certificate_validation
Alex Reynolds
thank you alex, I used to visit this site. It is very useful. however, due to some reason, I cannot use this code, could you mind to give another suggestion to me. Thank you very much.
Questions