开发者

Why does my NSURLMutableRequest POST request act like a GET request?

开发者 https://www.devze.com 2023-03-27 16:16 出处:网络
I\'ve got a problem with my objective c code.I have a API-key protected WCF API that I built that takes POST requests and writes them to a Java servlet with C#.Anyway, this works great when testing wi

I've got a problem with my objective c code. I have a API-key protected WCF API that I built that takes POST requests and writes them to a Java servlet with C#. Anyway, this works great when testing with Fiddler, not so good from objective C. When I try to run the POST from my objective C, it "acts" like the NSURLMutableRequest is looking for a GET, in that the response only returns some default code I have written in for the GET method. Does anybody know why this is, and, moreover, what I can do to fix it? Here is the code that I use (quite success开发者_JAVA百科fully) to make other POST requests in with objective C.

is the problem the fact that I specify the API key in the URL for the NSMutableRequest? That's the only thing I can figure.

Here is the code:

NSString* theMessage = [NSString stringWithFormat:@"<MyRequestObject xmlns='http://schemas.datacontract.org/2004/07/MyService'></MyRequestObject>"];

        NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:POST_API_URL] 
            cachePolicy:NSURLRequestUseProtocolCachePolicy
            timeoutInterval:240.0];

        [theRequest setHTTPMethod:@"POST"];
        [theRequest setValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
        [theRequest setHTTPBody:[theMessage dataUsingEncoding:NSUTF8StringEncoding]];

        NSString *msgLength = [NSString stringWithFormat:@"%d", [theMessage length]];
        [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];

        NSURLResponse* response;
        NSError *error;

        NSData* result = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];


I ended up using ASIHTTPRequest to run the POST request to the WCF REST service, and now everything seems to be running smoothly. This means that there's probably some sort of URL Encoding mechanism for the API key that's going on behind the scenes that was poorly documented for NSMutableURLRequest, who knows. The good thing is, I've got the issue fixed. Here is the code I used:

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:POST_API_URL]];
        [request appendPostData:[[NSString stringWithFormat:@"<MyRequest xmlns='http://schemas.datacontract.org/2004/07/MyService'>all of my request params in here</MyRequest>"] dataUsingEncoding:NSUTF8StringEncoding]];

        [request setRequestMethod:@"POST"];
        [request addRequestHeader:@"Content-Type" value:@"text/xml"];

        [request startSynchronous];


Did you try setting the Content-Length header? WCF/IIS might be ignoring the body if it doesn't have its length defined in as a header.

0

精彩评论

暂无评论...
验证码 换一张
取 消