gpt4 book ai didi

ios - AFNetworking 请求失败 : unacceptable content-type: text/html

转载 作者:可可西里 更新时间:2023-11-01 05:03:47 28 4
gpt4 key购买 nike

我正在尝试向服务器发送 POST 请求,但仍然失败。请帮我找出不正确的地方。

共享实例初始化:

- (id)init{
self = [self initWithBaseURL:[NSURL URLWithString:kBaseURL]];
if(self) {
self.
self.responseSerializer = [AFJSONResponseSerializer serializer];
[self.requestSerializer setAuthorizationHeaderFieldWithUsername:@"user" password:@"password"];
parsingQueue = dispatch_queue_create("com.company.queue.parser",NULL);
}
return self;

post方法体:

NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:[FSApplicationManager sharedInstance].userToken, @"user_token", nil];
[params setObject:[NSNumber numberWithDouble:userId] forKey:@"user_id"];
[params setObject:[NSNumber numberWithDouble:filmId] forKey:@"film_id"];
[params setObject:[NSNumber numberWithBool:NO] forKey:@"sendCopy"];

if([subject length]>0){
[params setObject:subject forKey:@"subject"];
}
[params setObject:message forKey:@"message"];

NSString *URLString = [NSString stringWithFormat:@"%@%@", kBaseURL, kAPISendMessageToUser];

NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:@"POST"
URLString:URLString
parameters:params
error:nil];

[request addValue:[self signatureWithURL:[request.URL absoluteString] requestBody:[[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding]] forHTTPHeaderField:@"X-SERVAPI-Signature"];
[request addValue:pub forHTTPHeaderField:@"X-SERVAPI-PublicKey"];

AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id response){
NSLog(@"%@", response);

} failure:^(AFHTTPRequestOperation *operation, NSError *error){
NSLog(@"%@, \n response %@", error, operation.responseObject);
}];

[self.operationQueue addOperation:operation];

return operation;

我得到的错误:

Error Domain=AFNetworkingErrorDomain Code=-1011 "Request failed: internal server error (500)" UserInfo=0xcf0c2f0 {NSErrorFailingURLKey=https://ppd.someserver.com/api/user/send-message, NSLocalizedDescription=Request failed: internal server error (500), NSUnderlyingError=0xcf06ce0 "Request failed: unacceptable content-type: text/html", AFNetworkingOperationFailingURLResponseErrorKey= { URL: https://ppd.someserver.com/api/user/send-message } { status code: 500, headers { Connection = close; "Content-Encoding" = gzip; "Content-Length" = 1412; "Content-Type" = "text/html; charset=UTF-8"; Date = "Sun, 14 Dec 2014 13:46:57 GMT"; P3P = "CP=\"NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM\""; Server = Apache; Vary = "Accept-Encoding"; "X-Powered-By" = "PHP/5.3.3-7+squeeze19"; } }},

我已经尝试联系负责服务器的人,这就是我得到的:

There is no need to add the content type to your request but if you wish to do so, since your are using a POST method, you should use: Content-Type: application/x-www-form-urlencoded;

最佳答案

问题是服务器向您发送了 HTML 编码的答案。您可以执行以下 2 个选项之一:

  1. 与服务器端人员交谈,将其编码为 json。

  2. 尝试通过执行以下操作自行修复它:设置 self.responseSerializer 后放置此行:

    self.operationManager.responseSerializer.acceptableContentTypes = [self.operationManager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];

此行将“text/html”添加到可接受的类型。提供服务器的响应仍然是可序列化的——你应该没问题。

关于ios - AFNetworking 请求失败 : unacceptable content-type: text/html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27470032/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com