1

I am building a JSON post in objective-c and sending it to an ASP.NET MVC controller.

I am building the NSMutableURLRequest as follows:

    request = [[NSMutableURLRequest alloc] initWithURL:url];

    NSString* jsonRequest = [NSString stringWithFormat: @"{\"collection\":\"images\",\"id\":\"%@\",\"objectjson\":%@}",response.id,response.json];

    NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];

    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody: requestData];

I then send the request as follows:

        NSOperationQueue *backgroundQueue = [[NSOperationQueue alloc] init];

        [NSURLConnection sendAsynchronousRequest:request queue:backgroundQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
        { ... completion code goes here

This works well most of the time. However, for very large JSON strings I occasionally get a web service error where the web service reports that it is encountering an End of File marker within the JSON. It appears that the JSON is being truncated.

I am sending the JSON to an ASP.NET MVC controller.

Does anyone have any words of wisdom on what might be happening? Are there any ASP.NET web configuration settings that perhaps I need to adjust to prevent this issue occurring.

One thing I don't understand is why it is such an intermittent problem.

Journeyman
  • 10,011
  • 16
  • 81
  • 129
  • Having the same problem. Have found nothing online. Not sure if it's iOS or IIS... I personally would tend to blame IIS but who knows... – jjxtra Apr 25 '13 at 14:28
  • Maybe this link helps: http://stackoverflow.com/questions/12662282/content-length-of-http-request-body-size/12832707#12832707 – jjxtra Apr 25 '13 at 14:29
  • Did some logging on my server and found that the content length header is indeed the correct value as set by my iOS app but that actual stream contains less bytes than the content length. Some bytes must be getting lost in transit, probably due to 3G connection is my guess. Not sure of a work around other than telling the client to try again. – jjxtra May 17 '13 at 15:46

2 Answers2

2

This seems to be a result of bytes being lost over 3G or EDGE connection. The best idea I can come up with is to detect on the server that the content length header is larger than the request POST body and to return a status code that tells the client to try again. The client could pass a retry count on the url and the server could read it and if it's a certain value, the server would return an error code indicating that a retry should not be attempted. Ugly I know but I can't think of a better way. This is what I am going to do for my photo uploading app.

Good luck!

jjxtra
  • 20,415
  • 16
  • 100
  • 140
0

the problem is in the conversion to NSData

try this

NSData *requestData = [jsonRequest dataUsingEncoding:NSUTF8StringEncoding];