I tried to follow the instructions from this post iOS: how to perform a HTTP POST request? but unfortunately the server answers but doesn't give me any data.
The connection didReceiveResponse
is called as well as the connectionDidFinishLoading
without an error.
Am I doing something wrong? I tested the service with Hurl.it and it worked just right.
requestUrlString
= a normal url,
postString
= something like "appId=1&last_update=0"
responseData = [[NSMutableData alloc] initWithLength:0];
NSURL *baseURL = [NSURL URLWithString:[requestUrlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:baseURL];
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"%d",[postString length]] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
[connection start];
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[responseData setLength:0];
NSLog(@"Connection didReceiveResponse");
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[responseData appendData:data];
NSLog(@"Connection didReceiveData");
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"Connection Error: %@", [error description]);
[[[UIAlertView alloc] initWithTitle:@"Error"
message:[error description]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil] show];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"Connection didFinishLoading");
NSLog(@"data %@", [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
}
Please don't refer to ASIHTTP I got the same here.
//edit: I fixxed it sorry for bothering you with my problems. The problem was the encoding type.