ios 如何将 AFNetworking 请求打印为 RAW 数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17473264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to print AFNetworking request as RAW data
提问by Jacek Kwiecień
For debuging purposes I'd want to print whole request body. I'm using AFHTTPClient. printing client gives some information, like headers but post/get params are not there.
出于调试目的,我想打印整个请求正文。我正在使用 AFHTTPClient。打印客户端提供了一些信息,例如标题,但没有发布/获取参数。
IS there a way to do it?
有没有办法做到这一点?
回答by Aaron Brager
Built-in AFNetworking tools
内置 AFNetworking 工具
For AFNetworking 1.x, use AFHTTPRequestOperationLogger.
对于 AFNetworking 1.x,使用AFHTTPRequestOperationLogger.
For AFNetworking 2.x, use AFNetworkActivityLogger.
对于 AFNetworking 2.x,使用AFNetworkActivityLogger.
These tools both use the NSNotificationbroadcast by AFNetworking to log request and response data to the console. The amount of information to be displayed is configurable, and they can be configured to ignore certain operations.
这些工具都使用NSNotificationAFNetworking的广播将请求和响应数据记录到控制台。要显示的信息量是可配置的,它们可以配置为忽略某些操作。
Examination in Xcode without these tools
在没有这些工具的情况下在 Xcode 中考试
HTTP Requests (outgoing data)
HTTP 请求(传出数据)
If you want to examine the body of an outgoing request, look at the NSURLRequest's HTTPBodyparameter, which is a property on your AFHTTPRequestOperation.
如果要检查传出请求的正文,请查看NSURLRequest'sHTTPBody参数,它是AFHTTPRequestOperation.
For example, in the method -[AFHTTPClient getPath:
parameters:
success:
failure:], after the request is made, you can type this into the debugger:
例如,在方法中-[AFHTTPClient getPath:
parameters:
success:
failure:],发出请求后,您可以在调试器中键入:
po [[NSString alloc] initWithData:request.HTTPBody encoding:4]
po [[NSString alloc] initWithData:request.HTTPBody encoding:4]
4 is NSUTF8StringEncoding, as defined in NSString.h.
4 是NSUTF8StringEncoding,如 NSString.h 中所定义。
The NSURLRequest's HTTPMethodparameter provides the method (GET, POST, PUT, etc.) as an NSString.
所述NSURLRequest的HTTPMethod参数提供了方法(GET,POST,PUT,等)作为一个NSString。
HTTP Responses (incoming data)
HTTP 响应(传入数据)
When your server responds, your success completion block is passed an AFHTTPRequestOperationobject (called operationby default). You can:
当您的服务器响应时,您的成功完成块会传递一个AFHTTPRequestOperation对象(operation默认调用)。你可以:
p (int)[[operation response] statusCode]to see the status codepo [[operation response] allHeaderFields]to see the headerspo [operation responseString]to see the response bodypo [operation responseObject]to see the response object (which may benilif it couldn't be serialized)
p (int)[[operation response] statusCode]查看状态码po [[operation response] allHeaderFields]查看标题po [operation responseString]查看响应正文po [operation responseObject]查看响应对象(nil如果无法序列化,可能是这样)
回答by Zee
As of AFNetworking 2.0, you should use AFNetworkActivityLogger
从 AFNetworking 2.0 开始,您应该使用AFNetworkActivityLogger
#import "AFNetworkActivityLogger.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
#ifdef DEBUG
[[AFNetworkActivityLogger sharedLogger] startLogging];
[[AFNetworkActivityLogger sharedLogger] setLevel:AFLoggerLevelDebug];
#endif
return YES;
}
If you are using 3.0and using CocoaPods, you will also need to pull AFNetworkActivityLoggerfrom the appropriate branch:
如果你正在使用3.0和使用的CocoaPods,您还需要拉AFNetworkActivityLogger从适当的分支:
pod 'AFNetworkActivityLogger', git: 'https://github.com/AFNetworking/AFNetworkActivityLogger.git', branch: '3_0_0'
回答by Benjamin Toueg
You should have a look at https://github.com/AFNetworking/AFHTTPRequestOperationLoggerwith AFLoggerLevelDebug as level of debugging.
您应该查看https://github.com/AFNetworking/AFHTTPRequestOperationLogger,其中 AFLoggerLevelDebug 作为调试级别。
#import "AFHTTPRequestOperationLogger.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
#ifdef DEBUG
[[AFHTTPRequestOperationLogger sharedLogger] startLogging];
[[AFHTTPRequestOperationLogger sharedLogger] setLevel:AFLoggerLevelDebug];
#endif
return YES;
}
@end
回答by Alex
For AFNetworking 3.0 to be able to set the level of logging, you need the following:
要使 AFNetworking 3.0 能够设置日志记录级别,您需要以下内容:
#import <AFNetworkActivityLogger/AFNetworkActivityLogger.h>
#import <AFNetworkActivityLogger/AFNetworkActivityConsoleLogger.h>
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
AFNetworkActivityConsoleLogger *logger = [AFNetworkActivityLogger sharedLogger].loggers.anyObject;
logger.level = AFLoggerLevelDebug;
[[AFNetworkActivityLogger sharedLogger] startLogging];
return YES;
}

