0

I'm trying to do a simple get in iOS (Objective C) using a simulator and not a real device.

    NSMutableURLRequest *newRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:theGetURL]
                                                              cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
                                                          timeoutInterval:10];
    [newRequest setHTTPMethod: @"GET"];

    NSError *requestError2;
    NSURLResponse *urlResponse2;


    NSData *response2 = [NSURLConnection sendSynchronousRequest:newRequest returningResponse:&urlResponse2 error:&requestError2];
    NSString* secondResponse = [[NSString alloc] initWithData:response2 encoding:NSUTF8StringEncoding];

    NSLog(@"error = %@",requestError.localizedDescription);
    NSLog(@"response=%@",secondResponse);
    NSLog(@"url response = %@",urlResponse);

This code works perfectly when I'm passing a simple url. When I try the code with a longer (around 4000 characters) url, the code doesn't work (no error is printed).

I am aware that a post is better for this kind of thing, but my question is, is this expected from a get request?

Also, my url works perfectly in my mac and iOS browsers.

danb
  • 25
  • 6
  • 1
    Maybe check if the URL of `newRequest` is valid (maybe some escaping which seems wrong). – Larme Feb 23 '15 at 10:00
  • I've printed `theGetURL` in the log and I've tested it on the simulators browser and it works as expected. – danb Feb 23 '15 at 10:03
  • From what's you're saying, you've printed the `NSString`, but not `[NSURL URLWithString:theGetURL]`? – Larme Feb 23 '15 at 10:04
  • `NSLog(@"The URL:%@", [NSURL URLWithString:theGetURL]);` returns null. Am I doing it right? – danb Feb 23 '15 at 10:08
  • Check `stringByAddingPercentEscapesUsingEncoding`: http://stackoverflow.com/questions/3418754/how-to-prepare-an-nsurl-from-an-nsstring-continaing-international-characters – Larme Feb 23 '15 at 10:10
  • Using `stringByAddingPercentEscapesUsingEncoding` prints a url, but it's a little different from my original one and does not work in the browser. What does that mean? – danb Feb 23 '15 at 10:14

2 Answers2

1

As you suspect, I think you need to consider moving to use POST rather then GET. The server side limit is 8K, however it seems this can be much less for the client side.

The following discussion sums everything up well. It also seems to imply the limit for Safari is 2K, which probably means it is the same or less for iOS, which would explain your problem with 4000 characters.

maximum length of HTTP GET request?

Community
  • 1
  • 1
Rory McKinnel
  • 7,936
  • 2
  • 17
  • 28
1

I think your URL query parameter might have any character that is not encoded. Try to ensure it. For encoding you may try this code

- (NSString *)encodeQueryParameter:(NSString *)str
{
    CFStringRef ref = CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                              (__bridge CFStringRef)str,
                                                              NULL,
                                                              CFSTR(":/=,!$&'()*+;[]@#?"),
                                                              kCFStringEncodingUTF8);
    NSString *result = (__bridge_transfer NSString *)ref;
    return result;
}
eNipu
  • 25
  • 1
  • 8
  • My query has a lot of `%` signs. I'm using double `%` as escape characters everywhere for those instances. Should I remove the escape characters before trying this method? – danb Feb 23 '15 at 11:23
  • Hi @danb, As I am not sure how your URL is structured, my opinion is please read the following link http://blog.lunatech.com/2009/02/03/what-every-web-developer-must-know-about-url-encoding . You may find it useful about URL and encoding works. Then you will surely able to decide what is going wrong and where. – eNipu Feb 23 '15 at 11:41
  • Here is a sample url: `http://10.2.176.100:9000/recorddata?mobileData={%22mobileData%22:[{%22cpu%22:-51,%22date%22:%22142441374.5834%22,%22memory%22:978,%22battery%22:-96,%22deviceID%22:2}]}` – danb Feb 23 '15 at 11:47
  • %22 is a Quotation mark. You do not need to use double %. Encoding methods should take care of %22 in your Url. I will try to test this url. – eNipu Feb 23 '15 at 12:14
  • Thanks, waiting for your reply. – danb Feb 24 '15 at 05:36