1

I am currently posting json data using an asynchronous NSURLConnection to a ASP.Net WebAPI resource. The connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite: is being called, and the correct amount of data is being written. But the connection:didReceiveResponse:, connection:didReceiveData: or connectionDidFinishLoading: are not being called, but eventually it gives up, and the connection is lost.

I am setting up the connection as follows:

NSURL *url = [[NSURL URLWithString:[Common baseUrl]] URLByAppendingPathComponent:@"api/iCat/wishlists/upload"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.f];
NSString *jsonString = [self generateStrippedJsonForWishlist:wishlist];
NSData *requestData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];

[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [jsonString lengthOfBytesUsingEncoding:NSUTF8StringEncoding]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:requestData];

And initiating the connection with:

connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

Eventually the request will time out with a "The network connection was lost" error message. To me it seems like the app is never completely finished writing the body of the message.

From the api side, the method is never actually being called (break point is not being hit). But if I send the request with no body, then it does.

I have tested the same call using curl, and it is working as expected:

curl -v -i -X POST -H "Content-Type: application/json" -d '{"CreatedBy":"iPad Simulator","ContactName":"Ddd","ContactEmail":"ddd","Products":[{"ProductId":117,"ProductName":"Internal doors - deco","Specification":""}]}' http://SCMOBILE02:1861/api/iCat/wishlists/upload
8
  • can you post your methods that are not working?
    – Bot
    Aug 23, 2012 at 0:26
  • Is requestData nil? What is in jsonString? Aug 23, 2012 at 0:31
  • In this example the json string is: {"CreatedBy":"iPad Simulator","ContactName": "Sss","ContactEmail": "sss","Products": [{"ProductId": 117,"ProductName": "Internal doors - deco","Specification": ""}]}
    – Scott
    Aug 23, 2012 at 0:52
  • no, requestData is not nil. As I mentioned, the connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite: is returning that the entire body has been written to the server. None of the other delegate methods are being called, and the server method is not being triggered
    – Scott
    Aug 23, 2012 at 0:54
  • Any chance you could use fiddler (fiddler2.com/fiddler2) to capture the request header and message being sent to your server? Well worth comparing to the CURL request that works. It would probably help others diagnose the issue if you also posted that to your question.
    – Mark Jones
    Aug 23, 2012 at 7:16

2 Answers 2

0

Try library https://github.com/AFNetworking/AFNetworking while posting json data.

0

Can you try the following code below ?

-(void) post
{
    NSString * a = @"{company_name : \"com\"}";
    NSString * URL = @"yourUrl";

    NSString *post = [NSString stringWithFormat: @"%@",a];
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

    NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
    [request setURL:[NSURL URLWithString:URL]];

    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [request setTimeoutInterval:20.0];
    [request setHTTPBody:postData];


    [NSURLConnection connectionWithRequest:request delegate:self];

}

In your web api you should have the following class and post method like below

        public class company 
        {
            public string company_name { get; set; }


        }


       // POST api/values
        public void Post([FromBody]company value)
        {
            var t = value;
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.