我正在從iPhone發送POST請求到web服務,這是一個基於SOAP的REST層。我得到錯誤408請求超時,在iPhone設備和模擬器上進行測試。在使用模擬器進行測試時,我注意到如果我先從瀏覽器訪問網絡服務器,然後運行模擬器,問題消失並且通信正常。行爲與此問題非常相似HTTP, 408 Request timeout 以下是我用來創建POST請求的代碼。有沒有人遇到過這種行爲,並找到了解決方案?HTTP錯誤408訪問網絡服務
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;