AFNetworking 2.0 header Content-Type 未在 POST 呼叫中发送
AFNetworking 2.0 header Content-Type not being sent on POST call
我正在尝试将一些 NSURLConnection 代码转换为 AFNetworking 2.0,但我遇到了 POST 调用的问题。 GET 调用有效,但 POST.
无效
这是我的代码:
+(void)login:(User*)myUser
{
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setAuthorizationHeaderFieldWithUsername:myUser.username password:myUser.password];
[manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[manager POST:URL_LOGIN
parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"success! Response = %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"failure. Error = %@", error);
}];
}
这是我从服务器返回的错误:
Error Domain=com.<server>.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html" UserInfo=0x7fe043c43670 {com.<server>.serialization.response.error.response=<NSHTTPURLResponse: 0x7fe043d52850> { URL: http://<server>/login.htm;jsessionid=98399EA8F090BF310D1CF5FD3C992A21?login_error=1 } { status code: 200, headers {
"Content-Language" = en;
"Content-Length" = 2801;
"Content-Type" = "text/html;charset=ISO-8859-1";
Date = "Tue, 28 Apr 2015 15:30:21 GMT";
Server = "Apache-Coyote/1.1";
我也曾尝试将 Content-Type 显式地作为参数放置,但它也不起作用。
我在网上发现了很多建议,要么修改服务器(我不能),要么只设置 manager.requestSerializer = [AFJSONRequestSerializer serializer];
,这显然也不起作用。
如有任何建议,我们将不胜感激。另外,如前所述,与此 Content-Type 的通信适用于 NSURLConnection 与 NSMutableURLRequest.
谢谢!
无论您喜不喜欢,您的服务器都会向您发送 text/html。
因此您必须在请求序列化程序中将其设为可接受的内容类型。
您可以像这样添加它:
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
或者使用更通用的 HTTP 序列化程序,而不是 JSON 序列化程序,如下所示:
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
我正在尝试将一些 NSURLConnection 代码转换为 AFNetworking 2.0,但我遇到了 POST 调用的问题。 GET 调用有效,但 POST.
无效这是我的代码:
+(void)login:(User*)myUser
{
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setAuthorizationHeaderFieldWithUsername:myUser.username password:myUser.password];
[manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[manager POST:URL_LOGIN
parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"success! Response = %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"failure. Error = %@", error);
}];
}
这是我从服务器返回的错误:
Error Domain=com.<server>.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html" UserInfo=0x7fe043c43670 {com.<server>.serialization.response.error.response=<NSHTTPURLResponse: 0x7fe043d52850> { URL: http://<server>/login.htm;jsessionid=98399EA8F090BF310D1CF5FD3C992A21?login_error=1 } { status code: 200, headers {
"Content-Language" = en;
"Content-Length" = 2801;
"Content-Type" = "text/html;charset=ISO-8859-1";
Date = "Tue, 28 Apr 2015 15:30:21 GMT";
Server = "Apache-Coyote/1.1";
我也曾尝试将 Content-Type 显式地作为参数放置,但它也不起作用。
我在网上发现了很多建议,要么修改服务器(我不能),要么只设置 manager.requestSerializer = [AFJSONRequestSerializer serializer];
,这显然也不起作用。
如有任何建议,我们将不胜感激。另外,如前所述,与此 Content-Type 的通信适用于 NSURLConnection 与 NSMutableURLRequest.
谢谢!
无论您喜不喜欢,您的服务器都会向您发送 text/html。
因此您必须在请求序列化程序中将其设为可接受的内容类型。
您可以像这样添加它:
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
或者使用更通用的 HTTP 序列化程序,而不是 JSON 序列化程序,如下所示:
manager.responseSerializer = [AFHTTPResponseSerializer serializer];