2012-11-14 14 views
12

Desidero chiedere se qualcuno ha mai provato a stampare i valori di una richiesta NSMutableURLRequest *;Stampa NSMutableURLRequest Contenuto

Ecco il mio scenario, ho formato il mio XML e ho provato a inviarlo utilizzando il plugin per Poster di Firefox, sto giocando con contenuti validi e non validi, quindi è ora di passare a iOS, sfortunatamente con iOS non sembra funzionare correttamente ho sempre una risposta di Bad Request (400) che sospetto sia causata da un contenuto xml malformato; Quindi stavo pensando prima di inviare la richiesta che voglio verificare se è nella sua forma corretta.

Così si riduce a "Come posso stampare (NSLog, ecc ..), il contenuto della richiesta *"

codice di esempio riportato di seguito:

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

questo frammento di codice

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

risultati a

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

mentre si fa qualcosa di simile

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

risultati per

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

So che questo insieme di valori esadecimali possono essere convertiti; Come? (per scoprire come è solo una lista dei desideri)

Il vero problema si riduce al contenuto della richiesta che sono ansioso di scoprire come visualizzare i contenuti grezzi ed esaminare se è davvero sulla sua forma corretta.

Grazie,

+6

È possibile utilizzare 'NSLog (@ "Richiesta corpo% @", [[NSString alloc] initWithData: [richiedere HTTPBody] codifica: NSUTF8StringEncoding];' – Moxy

risposta

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

ma se io vuoi richiesta con parametri in una stringa, ovvero URL finale, come .. "http://192.168.100.50:8022/LoginService.svc/GetloginDetailObj/"ALL TH PARAMETRI" come posso ottenere in nslog ...? – g212gs

+2

il trucco precedente è applicabile solo alle richieste PUT/POST. Una richiesta GET ha i parametri nell'URI –

+2

Come si può fare con sw ift? –

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