NSURLConnection closes early on GET

I am working on a method to centralize my URL connections to send and receive JSON data from a server. It works with POST, but not GET. I use the Google App Engine server, and on my computer it processes POST requests and returns the correct results (and the log, respectively), but I get the following error when trying to request using the 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}

In addition, a “broken pipeline” error is displayed on the GAE dev server, indicating that the client closed the connection before the server completed sending all the data.

Here's the method:

/* 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];
}

Sample call:

NSDictionary *response = [fetcher sendRequestToModule:@"login" ofType:@"GET" function:@"validate_email" params:dict];

Again, this works with POST, but not with GET. How can i fix this?

+5
source share
3 answers

, , URL.

JSON , '{', '}', '[' and ']'. URL URL.

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

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

:

CFNetwork, kCFErrorHTTPParseFailure. , HTTP.

+2

[request setHTTPMethod: @ "POST" ]

+8

, GET . JSON GET?

api , url.

If you want to send data and "receive" a response, use the message and check the body in response.

Sample message:

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;
+1
source

All Articles