gpt4 book ai didi

ios - 无法在 AFNetworking AFHTTPRequestOperationManager 中设置内容类型

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:39:54 26 4
gpt4 key购买 nike

我有一个网络服务需要 Content-Type = application/x-www-form-urlencoded。我已经设置了 header ,但它并没有像我在请求日志中看到的那样改变。下面是代码和请求日志

    NSURL *baseURL = [NSURL URLWithString:@"http://example.net/api/"];

// Initialize Request Operation Manager
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:baseURL];

// Configure Request Operation Manager
[manager setResponseSerializer:[AFJSONResponseSerializer serializer]];
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"application/x-www-form-urlencoded"];

// Send Request
[manager GET:@"Account/Register" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
// Process Response Object
NSLog(@"Register Response: %@",responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// Handle Error
NSLog(@"Register Response Error: %@",[error description]);
}];

但是当我查看请求时,Content-Type 没有改变。这是请求的详细信息

    ErrorDomain=AFNetworkingErrorDomainCode=-1011"Request failed: bad request (400)"UserInfo=0x109559a40{
AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x109709260>{
URL: http: //example.net/api/Account/Register
}{
statuscode: 400,
headers{
"Cache-Control"="no-cache";
"Content-Length"=142;
"Content-Type"="application/json; charset=utf-8";
Date="Mon, 10 Mar 2014 14:30:52 GMT";
Expires="-1";
Pragma="no-cache";Server="Microsoft-IIS/8.0";
"Set-Cookie"="xxxxx;Path=/;Domain=example.net, WAWebSiteSID=xxxxxxx; Path=/; HttpOnly";"X-AspNet-Version"="4.0.30319";"X-Powered-By"="ASP.NET";
}
},

NSLocalizedDescription=Requestfailed: badrequest(400),
NSErrorFailingURLKey=http: //example.net/api/Account/Register
}

如何将 Content-Type 设置为 application/x-www-form-urlencoded?

最佳答案

您不需要将 ResponseSerializer 设置为 manager。请使用以下代码为您工作。

 NSURL *baseURL = [NSURL URLWithString:@"http://example.net/api/"];

// Initialize Request Operation Manager
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:baseURL];

// Configure Request Operation Manager
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"application/x-www-form-urlencoded"];

// Send Request
[manager GET:@"Account/Register" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
// Process Response Object
NSLog(@"Register Response: %@",responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// Handle Error
NSLog(@"Register Response Error: %@",[error description]);
}];

关于ios - 无法在 AFNetworking AFHTTPRequestOperationManager 中设置内容类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22303577/

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