使用 Objective C 的 AFNetworking JSON 帖子返回不受支持的媒体类型

Posted

技术标签:

【中文标题】使用 Objective C 的 AFNetworking JSON 帖子返回不受支持的媒体类型【英文标题】:AFNetworking JSON post using Objective C returns unsupported media type 【发布时间】:2015-06-25 13:30:10 【问题描述】:

我正在尝试访问 Microsoft CRM Web 服务,并且我正在使用 AFNetworking 来执行此操作。该服务被配置为处理 JSON 请求和响应。

但是当我做一个 JSON 帖子时,我收到了这个错误:

"com.alamofire.serialization.response.error.data=, NSLocalizedDescription=请求失败:不支持的媒体类型 (415) "

我在代码上犯了什么错误。

这是我的代码:

   AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];

    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept" ];
    [manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type" ];

    NSDictionary *parameters = @@"username": self.Username.text,
                                 @"password": self.Password.text;

    [manager POST:@"http://www.xxxxxx.com/xxxx.svc/xxxxx" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) 
        NSLog(@"JSON: %@", responseObject);
     failure:^(AFHTTPRequestOperation *operation, NSError *error) 
        NSLog(@"Error: %@", error);
    ];

【问题讨论】:

你为什么害怕发布网址? 90% 的时间有人将 URL 粘贴到他们的浏览器中并立即知道您做错了什么。 【参考方案1】:

实际上,服务器期待一个 SOAP XML 请求。这是解决我的问题的代码:

NSString *soapMessage = [NSString stringWithFormat:@"<Authenticate xmlns=\"http://tempuri.org/\"><userName>"];
soapMessage = [soapMessage stringByAppendingString:self.loginUsername.text];
soapMessage = [soapMessage stringByAppendingString:@"</userName>"];
soapMessage = [soapMessage stringByAppendingString:@"<password>"];
soapMessage = [soapMessage stringByAppendingString:self.loginPassword.text];
soapMessage = [soapMessage stringByAppendingString:@"</password></Authenticate>"];

NSURL *url = [NSURL URLWithString:@"http://www.xxxxx.com/xxxxxx.svc/Authenticate"];

NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%lu", (unsigned long)[soapMessage length]];

[theRequest addValue: @"application/xml; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"http://tempuri.org/xxxxxx/Authenticate" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
// NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

if(theConnection) 
    NSError *WSerror;
    NSURLResponse *WSresponse;
    NSString *theXml;
    NSData *myData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&WSresponse error:&WSerror];
    theXml = [[NSString alloc]initWithBytes:[myData bytes] length:[myData length] encoding:NSUTF8StringEncoding];

    NSLog(@"Data : %@, %@",theXml, myData);    

else 

    NSLog(@"theConnection is NULL");

这篇文章帮助了我Passing parameter to WCF function using Objective c

【讨论】:

【参考方案2】:

谷歌搜索“状态 415”直接从马口中找到:

“服务器拒绝为请求提供服务,因为请求实体的格式不受所请求方法的请求资源支持。”

也就是说,服务器不支持application/json。

【讨论】:

【参考方案3】:

不确定您对 CRM 的要求,但您可以试试这个:

[request setValue:@"image/png" forHTTPHeaderField:@"Content-Type"];

【讨论】:

以上是关于使用 Objective C 的 AFNetworking JSON 帖子返回不受支持的媒体类型的主要内容,如果未能解决你的问题,请参考以下文章

objective-c和Cpp怎么混合编程?

Effective Objective -C 第一章 熟悉iOS

objective-c入门

Objective-C 和 MetroWerks C/C++ IDE

在 Mac OS X 上结合 Objective-C 和 C/C++

如何在混合代码 Objective-C/C++ 文件(.mm 文件)中使用 PHPhotoLibrary?