如何使用AFNetworking 2设置HTTP请求? [英] How to set HTTP request using AFNetworking 2?
问题描述
我需要发送普通的HTTP请求(GET),答案将以text / html格式显示。
如何使用AFNetworkin 2发送此响应?
I need to send ordinary HTTP request (GET) and answer will in text/html. How can I send this response using AFNetworkin 2 ?
现在我正在尝试使用
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://example.com"]];
[self HTTPRequestOperationWithRequest:request
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
很沮丧-它什么也没做。调试时,也未触发成功或失败子句。
And was frustrates - it do nothing. When debugging, nor success nor fail clause have been triggered.
我也尝试使用GET:parameters:success:failure:方法,但作为响应,我看到此错误:
Also I tried to use GET:parameters:success:failure: method, but in response I see this error:
错误:错误域= AFNetworkingErrorDomain代码= -1016请求
失败:不可接受的内容类型:text / html
Error: Error Domain=AFNetworkingErrorDomain Code=-1016 "Request failed: unacceptable content-type: text/html"
请任何人都可以向我解释错误的地方以及正确的发送请求的方式(如果我将以text / html的形式获得回复) )?
Please, anybody can explain me what are wrong and what is the correct way to send request (if I will get response as text/html)?
关于亚历克斯。
推荐答案
您在评论,针对使用 AFHTTPRequestOperationManager
的建议:
You said in your comment, in response to the suggestion to use the AFHTTPRequestOperationManager
:
GET我收到上面写的错误:错误:错误Domain = AFNetworkingErrorDomain代码= -1016请求失败:内容类型不可接受:text / html
When I used GET I got this error as I wrote above: Error: Error Domain=AFNetworkingErrorDomain Code=-1016 "Request failed: unacceptable content-type: text/html"
您可以使用 AFHTTPResponseSerializer
进行补救:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager GET:@"https://example.com" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
// do whatever you'd like here; for example, if you want to convert
// it to a string and log it, you might do something like:
NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"%@", string);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
您还可以使用 AFHTTPRequestOperation
:
NSOperationQueue *networkQueue = [[NSOperationQueue alloc] init];
networkQueue.maxConcurrentOperationCount = 5;
NSURL *url = [NSURL URLWithString:@"https://example.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
// do whatever you'd like here; for example, if you want to convert
// it to a string and log it, you might do something like:
NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"%@", string);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%s: AFHTTPRequestOperation error: %@", __FUNCTION__, error);
}];
[networkQueue addOperation:operation];
但是理想情况下,建议编写返回JSON(或XML)的服务器代码,因为这很多应用程序更易于使用和解析。
Ideally, though, it's advisable to write server code that returns JSON (or XML), as that's much easier for an app to consume and parse.
这篇关于如何使用AFNetworking 2设置HTTP请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!