我的目标 c 代码有问题。我有一个我构建的受 API 密钥保护的 WCF API,它接受 POST 请求并将它们写入使用 C# 的 Java servlet。无论如何,这在使用 Fiddler 进行测试时效果很好,而不是从目标 C 中很好。当我尝试从我的目标 C 运行 POST 时,它“行为”就像 NSURLMutableRequest 正在寻找一个 GET,因为响应只返回一些默认值我为 GET 方法编写的代码。有谁知道这是为什么,而且,我能做些什么来解决它?这是我使用(非常成功)在目标 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];