AFNetworking可以像curl -v那样请求吗?

时间:2016-11-07 12:37:05

标签: ios iphone curl afnetworking

我想制作一个http测试工具来帮助我们使用我们的应用找到用户问题,我喜欢使用curl -v进行测试。 每行都有用于调试。如IP,端口,证书和标题等。 iOS平台上有没有工具可以做到这一点?

curl -v https://stackoverflow.com/
*   Trying 151.101.129.69...
* Connected to stackoverflow.com (151.101.129.69) port 443 (#0)
* TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
* Server certificate: *.stackexchange.com
* Server certificate: DigiCert SHA2 High Assurance Server CA
* Server certificate: DigiCert High Assurance EV Root CA
> GET / HTTP/1.1
> Host: stackoverflow.com
> User-Agent: curl/7.49.1
> Accept: */*
>
< HTTP/1.1 200 OK
< Cache-Control: public, max-age=60
< Content-Type: text/html; charset=utf-8
< Expires: Mon, 07 Nov 2016 12:33:31 GMT
< Last-Modified: Mon, 07 Nov 2016 12:32:31 GMT
< X-Frame-Options: SAMEORIGIN
< X-Request-Guid: 61dedc6f-13f6-4ea6-970a-a596f885e29c
< Content-Length: 250615
< Accept-Ranges: bytes
< Date: Mon, 07 Nov 2016 12:32:31 GMT
< Via: 1.1 varnish
< Connection: keep-alive
< X-Served-By: cache-hkg6825-HKG
< X-Cache: MISS
< X-Cache-Hits: 0
< X-Timer: S1478521950.280986,VS0,VE949
< Vary: *
< X-DNS-Prefetch-Control: off
< Set-Cookie: prov=ca5856bc-b473-5203-b21e-2b2eb6516fee; domain=.stackoverflow.com; expires=Fri, 01-Jan-2055 00:00:00 GMT; path=/; HttpOnly
<
...
...
...

1 个答案:

答案 0 :(得分:0)

这是可能的。无论如何,它不像在curl -v的示例中那样立即向命令行添加标记。

GET:parameters:success:failure: methodAFHTTPSessionManager上执行GET请求。该方法的声明是:

- (nullable NSURLSessionDataTask *)GET:(NSString *)URLString
                            parameters:(nullable id)parameters 
                               success:(nullable void (^) (NSURLSessionDataTask *task, id _Nullable responseObject))success
                               failure:(nullable void (^) (NSURLSessionDataTask *_Nullable task, NSError *error))failure;

这里有两个感兴趣的块:第一个包含在成功响应的情况下(非常智能地命名success),另一个包含在请求中的failure

两个块都返回NSURLSessionDataTask类型的参数(在失败事件中可以为空,如果是这样的话,则没有响应,最有可能在没有互联网连接的情况下发生) 。根据继承自Apple Documentation for NSURLSessionDataTaskNSURLSessionTask,可以获取响应对象,然后获取标题。这是一个简单的example taken from Github

NSHTTPURLResponse *response = ((NSHTTPURLResponse *)[task response]);
NSDictionary *headers = [response allHeaderFields];
// Do something with the headers in the response here...

再从这里开始,查看Symbols for NSURLSessionTask并获取您需要的内容。