打印NSMutableURLRequest的内容

15

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

这是我的情况,我已经构建了XML并尝试使用Firefox Poster插件发送它,我成功地处理了有效和无效的内容,所以现在该转向iOS了,但很不幸,在iOS上它似乎无法正确工作,即使我总是收到"错误请求(400)"的响应,我怀疑这是由于格式不正确的xml内容引起的; 因此,在发送请求之前,我想检查它是否处于正确的形式。

所以问题就是 "如何打印(nslog等)*request的内容"

以下是示例代码:

    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]];

这段代码片段

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

结果为

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

在做某事时,例如:

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

结果为

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

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

真正的问题归结为请求的内容,我急于找到如何查看原始内容并检查它是否在正确的格式中。

谢谢,


6
您可以使用以下代码来输出HTTP请求的请求体内容:NSLog(@"请求体内容:%@", [[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding]); - Moxy
2个回答

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

但是如果我想在一个字符串中使用参数发出请求,即最终的URL,例如 "http://192.168.100.50:8022/LoginService.svc/GetloginDetailObj/"ALL THE PARAMETERS",我该如何在nslog中获取...? - g212gs
2
上述技巧仅适用于PUT/POST请求。GET请求的参数在URI中。 - Zayin Krige
String(data: request!.HTTPBody!, encoding: NSUTF8StringEncoding)// ----(但如果可能的话,请安全地解包请求和HTTPBody!我只在调试控制台中使用它!) - Lucas van Dongen

10
- (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];
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接