为什么我的 NSURLMutableRequest POST 请求的行为类似于 GET 请求?
我的 Objective C 代码有问题。我构建了一个受 API 密钥保护的 WCF API,它接受 POST 请求并使用 C# 将它们写入 Java servlet。无论如何,这在使用 Fiddler 进行测试时效果很好,但在目标 C 中效果不太好。当我尝试从目标 C 运行 POST 时,它的“行为”就像 NSURLMutableRequest 正在寻找 GET,因为响应仅返回一些默认值我为 GET 方法编写的代码。有谁知道这是为什么,而且我能做些什么来解决它?这是我使用(非常成功)通过 Objective C 发出其他 POST 请求的代码。
问题是我在 NSMutableRequest 的 URL 中指定了 API 密钥吗?这是我唯一能想到的。
这是代码:
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'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 successfully) 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];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我最终使用
ASIHTTPRequest
运行对 WCF REST 服务的 POST 请求,现在一切似乎都运行顺利。这意味着 API 密钥可能存在某种 URL 编码机制,该机制在幕后进行,但NSMutableURLRequest
的文档记录很少,谁知道呢。好消息是,我已经解决了这个问题。这是我使用的代码: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 forNSMutableURLRequest
, who knows. The good thing is, I've got the issue fixed. Here is the code I used:您是否尝试设置 Content-Length 标头?如果正文没有在标头中定义其长度,则 WCF/IIS 可能会忽略正文。
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.