0

I am using a webservice which takes json object as parameter. Here's my code:

    -(void)createHttpHeaderRequest {

        NSString *x = @"{\"GetVehicleInventory\": {\"ApplicationArea\": {\"Sender\": {\"ComponentID\":}}}" (something like that)

        NSString *sample = [NSString stringWithFormat:@"https://trialservice.checkitout?XML_INPUT=%@",x];
 NSString * final = (NSString *) CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)sampleReq, NULL, CFSTR(":/?#[]@!$&'()*+,;=\""), kCFStringEncodingUTF8);
 NSMutableRequest *request = [NSMutableREquest requestWithURL:[NSURL URLWithString:final]];
    NSURLConnection * theConnection = [[NSURLConnection alloc]initWithRequest:theRequest delegate:self];
        if (theConnection) {
            NSLog(@"Service hit");
        }
    }

    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
        NSError * error;
        NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
        NSArray * categories = [dict objectForKey:@"category"];
        [self.delegate giveControlBackToController:categories];

    }

When I am trying to NSLog sample it gives me the complete URL which on pasting into a browser gives me the result back, but when I am calling NSLog on the request, it shows null and nothing happens after this. The control never goes to its NSURLConnection delegate method.

Ashutosh
  • 5,614
  • 13
  • 52
  • 84
  • Please make the effort to use correct English and paste some code that at least compiles. –  Jul 25 '12 at 18:52
  • You're creating an NSMutableRequest called `request` but then initializing your NSURLConnection with something called `theRequest`. Does that reflect your actual code or is that just an error introduced when you copied it here? – Ethan Holshouser Jul 25 '12 at 19:54
  • That's just an error which got introduced here. Sorry about that. – Ashutosh Jul 25 '12 at 19:57
  • Have you implemented `connection: didReceiveResponse:` in your delegate? – Ethan Holshouser Jul 25 '12 at 20:06

2 Answers2

1

The trick is that most browsers automatically escape URLs whilst NSURL doesn't. You'll need to do it manually; have a look at the CFURLCreateStringByAddingPercentEscapes function.

  • OK. So i tried this and it's not giving me null now but still doesn't hit the delegate method plus the the url is now filled with a lot of %7D etc. Any other suggestions?? – Ashutosh Jul 25 '12 at 19:05
  • Have you set the delegate? Have you even created an NSURLConnection? –  Jul 25 '12 at 19:08
  • Yes. NSURLConnection * theConnection = [[NSURLConnection alloc]initWithRequest:theRequest delegate:self]; – Ashutosh Jul 25 '12 at 19:09
  • please see the edit above. Can't show the correct url or the json parameter but this is it. – Ashutosh Jul 25 '12 at 19:22
  • @Ashutosh so have you finally added the percent escapes using the function I suggested? I don't see it anywhere. Also, in `connection:didReceiveData:` doesn't do what you think it does -- the data parameter is partial data and should be collected using an NSMutableData instance. –  Jul 25 '12 at 19:36
  • Sorry about that. It was in my code somehow it got skipped while pasting code here. And the connectionDidReceiveData is not even being called. I just want to hit the service and see the response. I have put a break point in the start of this delegate method just to see if the program reaches here. – Ashutosh Jul 25 '12 at 19:41
  • Escape the JSON only, not the whole URL string. –  Jul 25 '12 at 20:00
0

This should probably be a comment instead of an answer, but unfortunately you can't format code in a comment. Add the following method to your delegate and see whether it gets called. If it does, let us know what the response is.

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
    NSLog(@"Connection response code: %d", httpResponse.statusCode);
}
Ethan Holshouser
  • 1,402
  • 14
  • 13
  • Its not getting called. But method didFailWithError does and it says NSLocalizedDescription=The certificate for this server is invalid. You might be connecting to a server that is pretending to be “209.112.58.126” which could put your confidential information at risk., NSUnderlyingError=0x6877b40 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be “209.112.58.126” which could put your confidential information at risk.", NSURLErrorFailingURLPeerTrustErrorKey=} – Ashutosh Jul 26 '12 at 15:09
  • It sounds like there's something wrong with the server configuration, then. You say it works in a browser, though? – Ethan Holshouser Jul 26 '12 at 17:11