ios 打印 NSMutableURLRequest 内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13375658/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:12:00  来源:igfitidea点击:

Print NSMutableURLRequest Contents

iosnslognsmutableurlrequest

提问by mirageservo

I want to ask if anybody has ever tried printing out the values of a NSMutableURLRequest *request;

我想问一下是否有人试过打印出 NSMutableURLRequest *request; 的值;

Here's my scenario, I have formed my XML and tried sending it using Firefox Poster plugin, I am successful playing with valid and invalid contents, so its time to move into iOS, unfortunately with iOS it doesn't seem to work correctly tho I always get a response of Bad Request (400) which I suspect is caused by a malformed xml content; So I was thinking before sending the request I want to check if it's on its correct form.

这是我的场景,我已经形成了我的 XML 并尝试使用 Firefox Poster 插件发送它,我成功地处理了有效和无效的内容,所以是时候进入 iOS 了,不幸的是,对于 iOS,它似乎无法正常工作,但我总是得到错误请求 (400) 的响应,我怀疑这是由格式错误的 xml 内容引起的;所以我在发送请求之前想检查它是否在其正确的形式上。

So it boils down to "How can I print out (nslog, etc..) the content of the *request"

所以它归结为“我如何打印出(nslog等)*请求的内容”

Sample code below:

示例代码如下:

    NSURL *URL = [NSURL URLWithString:@"http://xxx.xxx.xxx.xxx/ps"];

    NSMutableURLRequest *request = [NSMutableURLRequest                                         requestWithURL:URL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60];
    NSString *msgLength = [NSString stringWithFormat:@"%d", [self.xmlLogin length]];

    //NSString *params = [[NSString alloc] initWithFormat:@"%@",[self xmlLogin]];
    [request addValue:@"application/xxx.ven.xml" forHTTPHeaderField:@"Content-Type"];
    [request addValue:msgLength forHTTPHeaderField:@"Content-Length"];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"identity" forHTTPHeaderField:@"Accept-Encoding"];
    [request setValue:@"windows-1251" forHTTPHeaderField:@"Accept-Charset"];
    [request setCachePolicy:NSURLRequestReturnCacheDataElseLoad];
    [request setHTTPBody:[[self xmlLogin] dataUsingEncoding:NSUTF8StringEncoding]];

this code snippet

这个代码片段

NSLog(@"Request %@\n",request);

results to

结果

<NSMutableURLRequest http://xxx.xxx.xxx.xxx/ps>

while doing something like

在做类似的事情时

NSLog(@"Request %@\n",[request HTTPBody]);

results to

结果

<4c697665 3e383634 30303c2f 54696d65 546f4c69 76653e20 20202020 20202020 20202020 20202020 20202020 20202020 20202020 20202020 20202020 20202020 203c5365 7373696f 6e436f6f 6b69653e 436f6c69 62726961 494d5053 5f333734 36323032 39322e34 38373931 345f3737 33313c2f 53657373 696f6e43 6f6f6b69 653e2020 20202020 20202020 20202020 20202020 20202020 20202020 20202020 20202020 20202020 20202020 3c2f4c6f 67696e2d 52657175 6573743e 3c2f5472 616e7361 6374696f 6e436f6e 74656e74 3e3c2f54 72616e73 61637469 6f6e3e20 20202020 20202020 20202020 20202020 20202020 20202020 20202020 20202020 20202020 20202020 203c2f53 65737369 6f6e3e3c 2f57562d 4353502d 4d657373 6167653e>

I know this collection of hex values can be converted; how? (tho finding how is just a wish list)

我知道可以转换这个十六进制值的集合;如何?(虽然找到方法只是一个愿望清单)

The real issue boils to the contents of the request which I am eager to find out how to view the raw contents and examine if its really on its correct form.

真正的问题归结为请求的内容,我很想知道如何查看原始内容并检查它是否真的采用正确的形式。

Thanks,

谢谢,

回答by Zayin Krige

NSLog(@"Request body %@", [[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding]);

回答by Todd Lahtinen

- (NSString *)formatURLRequest:(NSURLRequest *)request
{
    NSMutableString *message = [NSMutableString stringWithString:@"---REQUEST------------------\n"];
    [message appendFormat:@"URL: %@\n",[request.URL description] ];
    [message appendFormat:@"METHOD: %@\n",[request HTTPMethod]];
    for (NSString *header in [request allHTTPHeaderFields])
    {
        [message appendFormat:@"%@: %@\n",header,[request valueForHTTPHeaderField:header]];
    }
    [message appendFormat:@"BODY: %@\n",[[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding]];
    [message appendString:@"----------------------------\n"];
    return [NSString stringWithFormat:@"%@",message];
}

- (NSString *)formatURLResponse:(NSHTTPURLResponse *)response withData:(NSData *)data
{
    NSString *responsestr = [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding];
    NSMutableString *message = [NSMutableString stringWithString:@"---RESPONSE------------------\n"];
    [message appendFormat:@"URL: %@\n",[response.URL description] ];
    [message appendFormat:@"MIMEType: %@\n",response.MIMEType];
    [message appendFormat:@"Status Code: %ld\n",(long)response.statusCode];
    for (NSString *header in [[response allHeaderFields] allKeys])
    {
        [message appendFormat:@"%@: %@\n",header,[response allHeaderFields][header]];
    }
    [message appendFormat:@"Response Data: %@\n",responsestr];
    [message appendString:@"----------------------------\n"];
    return [NSString stringWithFormat:@"%@",message];
}