Print NSMutableURLRequest Contents

后端 未结 2 1903
情书的邮戳
情书的邮戳 2021-02-13 03:57

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

Here\'s my scenario, I have formed my XML and tried sending it using F

2条回答
  •  孤城傲影
    2021-02-13 04:21

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

提交回复
热议问题