以下是我向服务器发送命令的方法:
- (void)sendCommand:(NSString *)command
{
NSURL *phpURL=[NSURL URLWithString:[DTAPI_Host stringByAppendingString:DTAPI_Path]];
NSMutableURLRequest *phpRequest=[[NSMutableURLRequest alloc] initWithURL:phpURL];
[phpRequest setHTTPMethod:@"POST"];
[phpRequest setValue:@"Accept" forHTTPHeaderField:@"application/json"];
[phpRequest setValue:[NSString stringWithFormat:@"%d", command.length] forHTTPHeaderField:@"Content-length"];
phpRequest.HTTPBody = [command dataUsingEncoding:NSUTF8StringEncoding];
[NSURLConnection connectionWithRequest:phpRequest delegate:self];
}
在:- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
我得到:Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost."
最令人困惑的部分是当我在Mac上发送相同的请求,比如Safari时,它没有问题。
我错过了什么?
答案 0 :(得分:3)
请使用
[phpRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"];
而不是相反。