2016-11-07 76 views
0

我想製作一個http測試工具來幫助我們使用我們的應用程序查找用戶問題,並且我喜歡使用curl -v進行測試。每條線在調試時都很有用。如IP,端口,證書和頭部等。 有沒有iOS平臺上的任何工具可以做到這一點?AFNetworking可以請求curl -v嗎?

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 
< 
... 
... 
... 

回答

0

這是可能的。無論如何,它不像在你的例子中使用curl -v那樣在命令行中添加一個標誌。

讓我們拿AFHTTPSessionManager上的GET:parameters:success:failure:method來執行一個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的參數(在失敗事件中爲空,如果是這種情況,那麼沒有響應,最有可能在沒有互聯網連接時發生)。根據從NSURLSessionTask繼承的Apple Documentation for NSURLSessionDataTask,可以獲取響應對象,然後獲取標題。這裏簡單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一看,把你所需要的。