为什么我的 NSURLMutableRequest POST 请求的行为类似于 GET 请求?

发布于 2024-11-29 12:23:41 字数 1314 浏览 2 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橪书 2024-12-06 12:23:41

我最终使用 ASIHTTPRequest 运行对 WCF REST 服务的 POST 请求,现在一切似乎都运行顺利。这意味着 API 密钥可能存在某种 URL 编码机制,该机制在幕后进行,但 NSMutableURLRequest 的文档记录很少,谁知道呢。好消息是,我已经解决了这个问题。这是我使用的代码:

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];

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];
我的奇迹 2024-12-06 12:23:41

您是否尝试设置 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文