Question

I am building an iPhone app the retrieves a file from an IIS7 server. I'm using a plain old NSURLRequest to retrieve this file. The request works fine if I use another web server, but for some reason this one keeps giving me a 400 bad request. So I took a look at the request the NSURLRequest sends, and it looks like this:

::1 [Wed Sep 01 20:18:53 -0400 2010]: GET /some.file
::1 [Wed Sep 01 20:18:53 -0400 2010]:  HTTP/1.1
::1 [Wed Sep 01 20:18:53 -0400 2010]: Host: localhost
::1 [Wed Sep 01 20:18:53 -0400 2010]: User-Agent: MyApp/1.0 CFNetwork/485.2 Darwin/10.4.0
::1 [Wed Sep 01 20:18:53 -0400 2010]: Accept: */*
::1 [Wed Sep 01 20:18:53 -0400 2010]: Accept-Language: en-us
::1 [Wed Sep 01 20:18:53 -0400 2010]: Accept-Encoding: gzip, deflate
::1 [Wed Sep 01 20:18:53 -0400 2010]: Connection: keep-alive
::1 [Wed Sep 01 20:18:53 -0400 2010]: 

I used telnet and connected directly to the webserver. Sent the following:

GET /some.file HTTP/1.1\r\n

This worked fine, the server responded with the file.

I have a feeling that the fact that NSURLRequest puts HTTP/1.1 on another line causes IIS7 to return a Bad Request.

Anyone know how I can remedy this situation? I don't mind making changes on the server or client, but unfortunately i'm stuck with IIS.

Thanks

Was it helpful?

Solution

Figured it out...my URL had a \n at the end. No wonder NSURLRequest was placing HTTP/1.1 on another line. I'm surprised NSURL doesn't clean up the string. wow.

OTHER TIPS

I had a similar setup (not IIS, though): GET request, fails with HTTP 400 on iOS/Swift code (NSURLSession), but works on Postman and curl.

After hours and hours of trying to figure out what could possibly be different between the requests sent from each client, I discovered my iOS code was sending an empty swift dictionary (of type [String : Any]) encoded as JSON in the request body.

I designed my client code to take a non-optional dictionary as the request parameters, and encode it/append it to the request body regardless of its contents. Some requests (like the one I was attempting) do not require any parameters in the body, and somehow the empty dict was causing problems. Removing it fixed the connection.

(I will now refactor my function to take an optional dictionary as the argument instead, and pass nil for requests that take no parameters in the body.)

Not a solution to the OP's question perhaps, but related. Here just in case it helps someone avoid lost hours of misery...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top