HTTP error 408 accessing web service

4.9k Views Asked by At

I am sending a POST request from iPhone to a webservice, which is a REST layer over SOAP. I am getting Error 408 Request Timeout, testing on both the iPhone device and simulator. When testing with the simulator, I noticed that if I access the webserver from a browser first, then run the simulator, the problem goes away and communication works. Behaviour is very similar to this question HTTP, 408 Request timeout Here is the code I am using to create the POST request. Has anyone come across this behaviour, and found a solution?

NSString * urlString = [NSString stringWithFormat:@"%@%@", kBaseURL,method];
NSURL * myURL = [NSURL URLWithString:urlString];
NSLog(@"URL %@", myURL);

NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:myURL];
[theRequest setHTTPMethod:@"POST"];
[theRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[theRequest setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
[theRequest setTimeoutInterval:60]; // leave at default 60 secs 
NSLog(@"Post Body: %@",body);   
NSURLConnection *theConnection = [[NSURLConnection alloc] 
                                  initWithRequest:theRequest 
                                  delegate:self];
if( theConnection ) {
    if (webData){
        [webData release];
    }
    webData = [[NSMutableData data] retain];
    NSLog(@"connection set up");
}
else {
    NSLog(@"theConnection is NULL");
}
self.mainConnection=theConnection;
[theConnection release];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES; 
2

There are 2 best solutions below

0
On BEST ANSWER

sorry for my english,

i faced with this stution, too. My application is an exe that calls a webservice. the webservice is coded with apache cxf. Then, i got an error "The remote server returned an unexpected response (408) Request timeout" in client side. but if i access the wsdl from a browser first, my exe runned successfully. i haven't found any solution. i add the fallowing codes before calls the webservice.

WebClient x = new WebClient();

string source = x.DownloadString("http://......../InfoOperations?wsdl");

  if (source != "")
  {
      // Call your web method here...
  }

It does the same thing with browser. this solved the problem. but normally, we don’t need to add this code.

Then, I changed the webservise coding. I used Axis2. but my problem is not solved. I thought this problem related to firewall.

Connection to webservice times out first time

http://www.mustafakurkcu.com/java/230-cxfclientreturn408.html

0
On

I eventually found what was causing this. I tried changing the URL, and found I could access any other website but not the webserver hosting the webservice I needed to communicate with. I tested on 3 different networks plus iPhone on 3G, so it wasn't a problem on my local network. I also tried using ASIHTTPRequest instead of NSURLConnection, this had no effect. The connection worked if I opened a webbrowser on the same device and pointed it at the remote server before attempting to access the web service.
Turns out the remote server was not handling POST requests correctly, but it was handling GET OK. The browser uses GET, so this is why the browser was able to set up a connection when my web service (using POST) couldn't. A quick and nasty workaround is to send a GET request and wait until it completes before attempting a POST request.