我正在从 iPhone 向 web 服务发送一个 POST 请求,这是一个基于 SOAP 的 REST 层。我收到错误 408 请求超时,在 iPhone 设备和模拟器上进行测试。在使用模拟器进行测试时,我注意到如果我先从浏览器访问网络服务器,然后运行模拟器,问题就会消失并且通信正常。行为与这个问题非常相似HTTP, 408 Request timeout 这是我用来创建 POST 请求的代码。有没有人遇到过这种行为,并找到了解决方案?
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;