NSURLConnection早关闭GET

问题描述:

我正在研究一种方法来集中我的URL连接以从服务器发送和接收JSON数据。它适用于POST,但不适用于GET。我正在使用Google App Engine服务器,并在我的计算机上处​​理POST请求并返回正确的结果(并且适当地记录日志),但是当我使用GET方法尝试请求时遇到以下错误:NSURLConnection早关闭GET

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 dev服务器显示“断开管道”错误,指示客户端在服务器完成发送所有数据之前关闭连接。

这里的方法:

/* 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。我怎样才能解决这个问题?

我认为根本原因是你有一个无效的URL。

JSON编码将包含诸如'{','}','['和']'之类的内容。所有这些在添加到URL之前都需要进行URL编码。

NSString *query = [NSString stringWithFormat:@"?action=%@", [str_params JSONString]]; 
query = [query stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 

NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", str_url, query]]; 

直接回答你的问题:

根据CFNetwork Error Codes Reference误差kCFErrorHTTPParseFailure。这意味着客户端无法正确解析HTTP响应。

+0

我会尝试了这一点,谢谢! – drodman

原因是GET不包含正文。你为什么要在GET中提交JSON呢?

如果目标API返回的数据只有你传递它的url params。

如果您想发送数据并“获得”回复,请使用帖子并在回复时检查正文。

样品帖子:

NSError *error; 
NSString *urlString = [[NSString alloc] initWithFormat:@"http://%@:%@/XXXX/MVC Controller Method/%@",self.ServerName, self.Port, sessionId ]; 
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding ]]; 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
[request setHTTPMethod:@"POST"]; 

// hydrate the remote object 
NSString *returnString = [rdc JSONRepresentation]; 
NSData *s10 = [returnString dataUsingEncoding:NSUTF8StringEncoding]; 
[request setHTTPBody:s10]; 
NSURLResponse *theResponse = [[NSURLResponse alloc] init]; 
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&error]; 
NSString *message = [[NSString alloc] initWithFormat: @"nothing"]; 

if (error) { 
    message = [[NSString alloc] initWithFormat:@"Error: %@", error]; 


} else { 
    message = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 

} 

NSLog(@"%@", message); 

return message; 

在我来说,我没有叫[请求setHTTPMethod:@ “POST”]

+0

我没有指定一个GET或POST,导致我这个错误。指定GET为我解决了它。 – njtman