2

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

Questions
  • 20,055
  • 29
  • 72
  • 101

1 Answers1

3

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
  • 95,983
  • 54
  • 240
  • 345