NSMutableData NSString - 如何知道哪个编码用于HTTP响应 [英] NSMutableData to NSString - how to know which encoding to use for HTTP response

查看:272
本文介绍了NSMutableData NSString - 如何知道哪个编码用于HTTP响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Google Protobuf将序列化类发送到http服务器。执行此操作的命令是:

message.SerializeToString(& out); 注意我们正在序列化到字符串。 strong>服务器将完全相同的对象返回给我。

I'm using Google Protobuf to send a serialized class to an http server. The command to do this is:
message.SerializeToString(&out); Notice that we are serializing to a String. The server returns the exact same object back to me.

所以,在我的连接:didReceiveData方法,我获得数据。

So, in my connection: didReceiveData method, I am getting data.

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)data
{
    if (self.receivingData) {
        [self.dataReceived appendData:data];
    }
}



在我的connectionDidFinishLoading方法中,我想我需要把NSMutableData(self.dataReceived)into a NSString。

In my connectionDidFinishLoading method I think I need to put the NSMutableData (self.dataReceived) into an NSString.

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    self.receivingData = NO;
    NSLog(@"%@", self.dataReceived);

    NSString *data = [[NSString alloc] initWithData:self.dataReceived encoding:NSASCIIStringEncoding];  // Wrong encoding ????

    NSMutableDictionary *processedData = [NSMutableDictionary dictionaryWithCapacity:1];
    [processedData setObject:data forKey:@"ImageData"];

    NSNotificationCenter *processedNote = [NSNotificationCenter defaultCenter];
    [processedNote postNotificationName:@"DataReceived" object:nil userInfo:processedData];
}

但我不知道使用什么编码。当我发送数据时,它看起来像这样:

But I'm not sure what encoding to use. When I send the data, it looks like this:

"\b\x01\x12\x04Lucy\x1a\xd4\xdc;\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\...... (There's more)

当我收到数据时,像这样:

When I receive the data, it looks like this:

<08011204 4c756379 1ad4dc3b ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ..... (There's more)

NSString with data above(encoding NSASCIIStringEncoding),我得到这个:

When I init the NSString with data above (encoding NSASCIIStringEncoding), I get this:

LucyÔÜ;ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ ....... (There's more)



最后,我需要从字符串使用Google Protobuf方法: message.ParseFromString(data);

我如何知道使用哪种编码?

How can I know which encoding to use?

推荐答案

尝试此操作。

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSLog(@"%@", [response textEncodingName]);
}

这篇关于NSMutableData NSString - 如何知道哪个编码用于HTTP响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆