我是iphone开发世界的新手,我正在尝试使用HTTP方法POST调用Web服务。
要做到这一点,我正在使用NSMutableUrlRequest。我的问题是从未调用过DidReceiveData委托,NSUrlConnection不返回null。
这是我的代码:
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)data
{
NSLog(@"didReceiveData");
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"didFinishLoading");
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError");
}
- (void) HttpRequest
{
NSLog(@"Calling HttpRequest");
NSString *parameters = @"placeName=Restau";
NSData *postData = [parameters dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSURL *webServiceUrl = [NSURL URLWithString:@"http://api.malves.fr/API.asmx/EngineSearchPlaceService"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:webServiceUrl];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"keep-live" forHTTPHeaderField:@"Connection"];
[request setValue:@"300" forHTTPHeaderField:@"Keep-Alive"];
[request setHTTPBody:postData];
NSURLConnection *connectionResponse = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (!connectionResponse)
{
NSLog(@"Failed to submit request");
}
else
{
NSLog(@"Request submitted");
}
NSLog(@"Sleeping... ZZZZzzzzzz");
[NSThread sleepForTimeInterval:10];
NSLog(@"Sleep done");
}
有人可以告诉我,如果我忘了重要的事吗?非常感谢。