问题描述
我正在研究一种方法来集中URL连接以从服务器发送和接收JSON数据.它适用于POST,但不适用于GET.我使用的是Google App Engine服务器,并且在我的计算机上它将处理POST请求并返回正确的结果(并正确记录),但是当我尝试使用GET方法请求时收到以下错误:
I'm working on a method to centralize my URL connections for sending and receiving JSON data from a server. It works with POST, but not GET. I'm using a Google App Engine server and on my computer it'll handle the POST requests and return proper results (and log appropriately), but I get the following error when I try the request with a GET method:
Error Domain=kCFErrorDomainCFNetwork Code=303 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error 303.)" UserInfo=0xd57e400 {NSErrorFailingURLKey=http://localhost:8080/api/login, NSErrorFailingURLStringKey=http://localhost:8080/api/login}
此外,GAE开发服务器显示管道中断"错误,表明客户端在服务器发送完所有数据之前关闭了连接.
In addition, the GAE dev server shows a "broken pipe" error, indicating that the client closed the connection before the server was finished sending all data.
这是方法:
/* Connects to a given URL and sends JSON data via HTTP request and returns the result of the request as a dict */
- (id) sendRequestToModule:(NSString*) module ofType:(NSString*) type function:(NSString*) func params:(NSDictionary*) params {
NSString *str_params = [NSDictionary dictionaryWithObjectsAndKeys:func, @"function", params, @"params", nil];
NSString *str_url = [NSString stringWithFormat:@"%@%@", lds_url, module];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:str_url]];
NSData *data = [[NSString stringWithFormat:@"action=%@", [str_params JSONString]] dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPMethod:type];
[request setHTTPBody:data];
[request setValue:[NSString stringWithFormat:@"%d", [data length]] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
NSError *error = nil;
NSURLResponse *response = nil;
NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(@"Error: %@", error);
NSLog(@"Result: %@", [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding]);
return [result objectFromJSONData];
}
一个示例调用为:
NSDictionary *response = [fetcher sendRequestToModule:@"login" ofType:@"GET" function:@"validate_email" params:dict];
同样,这适用于POST,但不适用于GET.我该如何解决?
Again, this works with a POST but not a GET. How can I fix this?
推荐答案
我认为根本原因是您的URL无效.
I think the root cause is you have an invalid URL.
JSON编码将包含诸如"{",}","["和]"之类的内容.所有这些都需要先进行URL编码,然后才能添加到URL.
JSON encoding will include things like '{', '}', '[' and ']'. All of these need to be URL encoded before being added to a URL.
NSString *query = [NSString stringWithFormat:@"?action=%@", [str_params JSONString]];
query = [query stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", str_url, query]];
直接回答您的问题:
To directly answer your question:
根据 CFNetwork错误代码参考错误是 kCFErrorHTTPParseFailure .这意味着客户端无法正确解析HTTP响应.
According to CFNetwork Error Codes Reference the error is kCFErrorHTTPParseFailure. This means the client failed to correctly parse the HTTP response.
这篇关于NSURLConnection在GET上早关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!