2

I have a Windows based REST Server built using Microsoft-HTTPAPI (i.e. its not running under IIS)

If I send a JSON request to the service from a browser based REST tool (Firefox RESTClient) the server receives the request and processes it correctly as per the traces from the Microsoft Service Trace Viewer and the response from the service. I receive valid JSON back from the service.

I have an iOS application that uses a NSURLConnect to send the VERY SAME JSON request to the service, but it always times out. I've traced using WireShark, and the HTTP request is correctly formed and sent correctly to the server in both cases. I've traced using the MS Trace Viewer and it goes into "receive Bytes" but never returns. I get an exception when I eventually close the WCF Server.It never returns when I close the iOS app.

I have tried using NSURLConnection sendSynchronousRequest and asynchronously using the callbacks and asynchronously using completion blocks. If I make the timeout 0, the call (to sendSynchronousRequest) never returns. In all other cases, I get a timeout, and the WCF never completes the receive bytes part of the WCF invocation (a POST request)

I've checked

Here's my iOS Code:

dispatch_queue_t myQ = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(myQ, ^{ // First Queue the parsing
    @try {
        //Generate endpoint url
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",appState.posPostingURL,serviceName]];
        //Setup request
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                               cachePolicy:NSURLRequestReloadIgnoringCacheData
                                                           timeoutInterval:10.0];

        NSString *authStr = [NSString stringWithFormat:@"%@:%@", @"xxxx", @"xxxx"];
        NSString *authData = [NSString base64StringFromString:authStr];
        NSString *authValue = [NSString stringWithFormat:@"Basic %@", authData];
        [request setValue:authValue forHTTPHeaderField:@"Authorization"];

        //Setup request headers
        [request setHTTPMethod:@"POST"];
        [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
        [request setValue:@"no-cache" forHTTPHeaderField:@"Cache-Control"];
        [request setValue:[NSString stringWithFormat:@"%d", [dataBuffer length]] forHTTPHeaderField:@"Content-Length"];

        // Put the request data in the request body
        [request setHTTPBody: dataBuffer];
        NSHTTPURLResponse *urlResponse = nil;
        NSError *error = nil;
        // and send to the server synchronously
        serverData = [[NSURLConnection sendSynchronousRequest:request
                                            returningResponse:&urlResponse
                                                        error:&error] mutableCopy];
        // Now check the status and response
        if (error) {

My WCF Code is as follows

namespace MyWebService{
[ServiceContract(Name = "MyServiceIntegrator", Namespace = "")]
public interface IMyIntegrator
{
    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "/Configure",
        BodyStyle=WebMessageBodyStyle.Bare,
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json)]
    Result Configure(MyServerConfig config);

In the app.config file, the service endpoint is configured with

binding="webHttpBinding"

The Trace view is as follows: WCF Trace

Could there be any reason on either side that the server does not complete the read even though the JSON is valid.

I have also checked NSURLConnection JSON Submit to Server and as far as I can see my code to send the request is correct.

Any ideas - Been pulling my hair out for 3 days now

Community
  • 1
  • 1
ferdil
  • 1,259
  • 11
  • 24
  • I recommend to send the data through a proxy like Charles or Fiddler and check the differences of the working and the not-working request. – Thorsten Mar 17 '13 at 22:30
  • Have you tried testing the request against an endpoint that doesn't require the authentication? I'm wondering if that might be causing issues. – KevinH Mar 18 '13 at 15:56
  • I had a problem when one of the string I was sending thorough post contains '&'.. then it breaks... I had to do the [following](http://stackoverflow.com/questions/3423545/objective-c-iphone-percent-encode-a-string/3426140#3426140) – chuthan20 Mar 19 '13 at 02:14
  • Is ther a keep-alive option on NSURLConnection? – nielsbot Mar 19 '13 at 05:08
  • Thanks for the suggestions. Still not working. – ferdil Mar 21 '13 at 13:39

2 Answers2

0

What happens if you change this:

  [request setValue:[NSString stringWithFormat:@"%d", [dataBuffer length]] forHTTPHeaderField:@"Content-Length"];

To this:

  [request setValue:[NSString stringWithFormat:@"%i", [dataBuffer length]] forHTTPHeaderField:@"Content-Length"];

Probably won't make a difference, but the rest of your code looks fine so maybe worth a shot. (My thinking is the decimal is causing the server issues).

Jack Freeman
  • 1,414
  • 11
  • 18
0

ferdil,I think you can try changing timeout period to 30.0 instead of 10.

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0];