Printing NSData using NSLog

后端 未结 5 959
孤城傲影
孤城傲影 2020-12-07 18:26

How can I print the contents of an NSData object using NSLog:

-(void) post:(NSString*) msg to:(NSString*) link{
    NSString *myRequestString = [NSString str         


        
5条回答
  •  情话喂你
    2020-12-07 19:28

    Convert NSData to NSString using

    NSString *strData = [[NSString alloc]initWithData:returnData encoding:NSUTF8StringEncoding];
    

    and print NSString in NSLog like below

    NSLog(@"%@",strData);
    

    This answer is edited for JeremyP, as he does not know how to know content is of UTF-8, though it was not a discussion of this question.

    You can get response header in following delegate method

    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
    {
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
    NSDictionary *dic = [httpResponse allHeaderFields];
    }
    

    This dictionary will give you entire header info like below

    {type = immutable dict, count = 7,
    entries =>
    0 : {contents = "X-Aspnet-Version"} = {contents = "2.0.50727"}
    1 : {contents = "Server"} = {contents = "Microsoft-IIS/6.0"}
    2 : {contents = "Content-Length"} = {contents = "385"}
    6 : {contents = "Cache-Control"} = {contents = "private, max-age=0"}
    10 : {contents = "X-Powered-By"} = {contents = "ASP.NET"}
    11 : {contents = "Content-Type"} = {contents = "text/xml; charset=utf-8"}
    12 : {contents = "Date"} = {contents = "Fri, 08 Jul 2011 15:23:10 GMT"}
    } 
    

    Check charset="utf-8", you will get encoding from here.

提交回复
热议问题