我有一个网络服务,需要Content-Type = application / x-www-form-urlencoded。我已经设置了标题,但它没有像我在请求日志中看到的那样发生变化。以下是代码和请求日志
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
}
如何将内容类型设置为application / x-www-form-urlencoded?
答案 0 :(得分:0)
您无需将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]);
}];