0

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.

Community
  • 1
  • 1
palme
  • 2,499
  • 2
  • 21
  • 38
  • My NSMutableData responseData for example is empty because the didReceiveData function is not fired. So the request is successful but I don't get any data. – palme Jul 17 '12 at 20:52
  • I don't have a complete solution for you, but I do have some ideas. Looking over the difference in your code and mine, the only major one I see is that I don't set Content-Length. I suppose if you're setting it incorrectly, that might cause this problem. It seems like a long shot, but I'd start by deleting that. Also, I suggest logging the `statusCode` in your `connectionDidFinishLoading`. It might be unexpected. – Steven Fisher Jul 17 '12 at 20:59
  • Thanks Steven for your ideas. Unfortunately deleting the line with the content-length doesn't fix it and the status code in `didReceiveResponse` is 200, so everything should be fine. Is there a way to get the status in `connectionDidFinishLoading`? Because I only have a `NSURLConnection` there. – palme Jul 17 '12 at 21:18
  • I should have asked about checking it in `didReceiveResponse` anyway. So you're getting code 200. :) – Steven Fisher Jul 17 '12 at 21:21
  • Your code looks fine to me, except I use `addValue:forHTTPHeaderField`. Perhaps the URL is not the correct one, hence you are getting no response? Is this being done in the main thread? – ohr Jul 17 '12 at 22:20
  • Have you tried your query with `curl`? – Steven Fisher Jul 18 '12 at 05:33
  • Tried to change `addValue:forHTTPHeaderField` but it didn't help. The URL because i already tried it on [link](http://hurl.it) and it gives me the right data. Alle this happens not in the Main-Thread because it is a asynchronous request. @Steven unfortunately I have never used curl before. Can you give me an example for that? – palme Jul 18 '12 at 07:14
  • curl is a command line tool for making web queries. We use it all the time when developing our web services; if we can't recreate a problem in curl, we figure it doesn't actually exist. You use it like this: `curl -d "form=value&form2=value2" -D header_output.txt http://myurl/goes/here` – Steven Fisher Jul 18 '12 at 16:51

1 Answers1

0

Does the web service generate a response body? OR does it just generate the response header? If there is no response body, there is no response to show on iOS.

Also, i believe you need an "Accept" header.

Steven
  • 974
  • 6
  • 18
  • The web service does generate a response body of the content type text/html. `[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Accept"];`and `[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];`did not fix it. Sorry. – palme Jul 17 '12 at 21:00
  • With regard to the comment from @Steven above (I dont have enough reputation to comment there). If I am checking the response data on a Mac machine, I use a REST client [available here](http://code.google.com/p/rest-client/). Although i prefer to use [Fiddler 2](http://www.fiddler2.com/fiddler2/) which runs on a windows machine as it uses the .NET framework – Steven Jul 18 '12 at 13:19
  • 1
    Ah! Good news. You should write up the answer for posterity. :) – Steven Fisher Jul 18 '12 at 16:52