0

I have a web service. I use it to accept a base 64 string representation of a small (thumbnail size) image. This web service works awesome when using it with Fiddler and manually posting the request. When I run the same request with NSMutableURLRequest (or ASIHTTPRequest), it always returns a 413 status code (413 is Request Entity is Too Large).

Why would NSMutableURLRequest cause it to come up with a 413, whereas Fiddler returns 200 every time?

Here is my NSMutableURLRequest code. I could really use a push, if anybody has any ideas.

        //the image request
        NSMutableURLRequest *imageRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:POST_IMAGE_API_URL] 
                                                                  cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                              timeoutInterval:240.0];

        //the post parameters
        [imageRequest setHTTPMethod:@"POST"];
        [imageRequest setHTTPBody:[imageMessage dataUsingEncoding:NSUTF8StringEncoding]];
        [imageRequest setValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];


        //a few other things
        NSURLResponse* imageresponse;
        NSError *imageerror;


        NSData* imageresult = [NSURLConnection sendSynchronousRequest:imageRequest returningResponse:&imageresponse error:&imageerror];
        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)imageresponse;

        NSLog(@"imageresponse: %d", httpResponse.statusCode);

2 Answers 2

1

When I see this bit of your code:

//the image request
NSMutableURLRequest *imageRequest = 
    [NSMutableURLRequest requestWithURL:[NSURL URLWithString:POST_IMAGE_API_URL]  
                            cachePolicy:NSURLRequestUseProtocolCachePolicy   
                        timeoutInterval:240.0];

I'm guessing you have some whacky characters in your "POST_IMAGE_API_URL" #define, most likely in the parameters that you're passing along.

You need to URL encode the URL string you pass to your URL request.

Try doing:

// assuming POST_IMAGE_API_URL starts with a "@" character
NSString * yourURLAsString = [NSString stringWithString: POST_IMAGE_API_URL]; 
NSURL * yourEncodedURL = [yourURL stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];

and pass "yourEncodedURL" in as a parameter to the URLRequest.

Sign up to request clarification or add additional context in comments.

7 Comments

thanks for the idea. There's nothing funny in my service URL string at all. I put together a simple, distilled service specifically for testing. I have the service's endpoint defined in a header file like this: #define POST_IMAGE_API_URL @"myserver.com/SRImageServ/SRImgService"
well okay then! I see the trouble right off the bat. There's no scheme on that URL. Try "http://myserver.com..."
Yeah, it's there. Sorry, the paste function got cut off. It really looks like this: #define POST_IMAGE_API_URL @"myserver.com/SRImageServ/SRImgService"
Yep. I believe that your scheme is there now. Do you have control over the server side? Is it getting hit with the POST bytes at all? Is your server returning the 413 error code or is it coming from the iPhoneOS?
No, the post data is never making it to the server. My trace object (this is a WCF service) tells me that I found the service, but that it never sends the data to the service.
|
0

I found a solution for this. The issue was not on the Apple end, but on the IIS end. There is an additional parameter for IIS hosted applications (one of which being my WCF service) beyond what is specified in the WCF's web.config file that specifies the "uploadReadAheadSize" for each service. I increased this and the 413 went away. Interestingly enough, I didn't get this error when sending the HTTP request from Fiddler on a desktop client on the same network as the server where the service resides. Basically, I had the solution to this guy's problem but not his context. My solution was his context.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.