Ios 异步NSURLConnection在每个字节之间接收空字节

Ios 异步NSURLConnection在每个字节之间接收空字节,ios,objective-c,xml,cocoa-touch,nsurlconnection,Ios,Objective C,Xml,Cocoa Touch,Nsurlconnection,我试图从服务器获取XML文档,但得到的明文响应只是ÿþ开头的fffe是一个字节顺序标记,表示响应是little-endian UTF-16。(请参阅。)如果您查看其余数据,就会进一步确认这一点,其中3c00是的little-endian UTF-16表示形式,我打赌您的问题是因为服务器的编码是“windows-1252”,您正试图使用“NSUTF8StringEncoding”来管理事情,这可能是不匹配的。我想到了这一点,但这只是发送我的帖子数据的部分,似乎没有任何问题。这是我遇到麻烦的响应数据

我试图从服务器获取XML文档,但得到的明文响应只是
ÿþ开头的
fffe
是一个字节顺序标记,表示响应是little-endian UTF-16。(请参阅。)如果您查看其余数据,就会进一步确认这一点,其中
3c00
的little-endian UTF-16表示形式,我打赌您的问题是因为服务器的编码是“
windows-1252
”,您正试图使用“
NSUTF8StringEncoding
”来管理事情,这可能是不匹配的。我想到了这一点,但这只是发送我的帖子数据的部分,似乎没有任何问题。这是我遇到麻烦的响应数据。尝试
gzopen
响应数据。当响应数据注销到控制台时,它似乎没有被压缩,我用NSLog登录到控制台的内容对数据进行了编辑。在十六进制编辑器中,您可以清楚地看到文本,但每个字节之间都有空字节。通常每个字节之间的零字节是unicode编码的标志。尝试忽略xml告诉
encoding=“windows-1252”
这一事实。
NSString *postData = @"SomePostData";
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSString *URLString = [NSString stringWithFormat:@"https://www.example.com/xmlexport?sessionid=%@", sessionID];
[request setURL:[NSURL URLWithString:URLString]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:[postData dataUsingEncoding:NSUTF8StringEncoding]];

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
myData = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [myData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSString *myString = [[NSString alloc] initWithData:myData encoding:NSWindowsCP1252StringEncoding];
    NSLog(@"%@", myString);
}
{ URL: https://www5.whentowork.com/cgi-bin/w2wE.dll/mgrexportemplist?SID=2835115254228 } { status code: 200, headers {
    "Access-Control-Allow-Origin" = "*";
    "Content-Disposition" = "attachment;filename=empdata.xml";
    "Content-Encoding" = gzip;
    "Content-Type" = "application/x-msdownload";
    Date = "Mon, 14 Apr 2014 19:37:54 GMT";
    Server = "Microsoft-IIS/6.0";
    "Transfer-Encoding" = Identity;
    Vary = "Accept-Encoding";
} }
<?xml version="1.0" encoding="windows-1252" standalone="yes"?>
<!-- Generated by Exmaple.com -->
<!DOCTYPE tutorial [
    <!ENTITY auml "&amp;auml;">
    <!ENTITY ouml "&amp;ouml;">
    <!ENTITY uuml "&amp;uuml;">
    <!ENTITY Auml "&amp;Auml;">
    <!ENTITY Ouml "&amp;Ouml;">
    <!ENTITY Uuml "&amp;Uuml;">
fffe3c00 3f007800 6d006c00 20007600 65007200 73006900 6f006e00 3d002200 31002e00 30002200
20006500 6e006300 6f006400 69006e00 67003d00 22007700 69006e00 64006f00 77007300 2d003100 
32003500 32002200 20007300 74006100 6e006400 61006c00 6f006e00 65003d00 22007900 65007300 
22003f00 3e000d00 0a003c00 21002d00 2d002000 47006500 6e006500 72006100 74006500 64002000 
62007900 20005700 68006500 6e005400 6f005700 6f007200 6b002d00 2d003e00 0d000a00 3c002100 
44004f00 43005400 59005000 45002000 74007500 74006f00 72006900 61006c00 20005b00 0d000a00 
20002000 20002000 3c002100 45004e00 54004900 54005900 20006100 75006d00 6c002000 22002600 
61006d00 70003b00 61007500 6d006c00 3b002200 3e000d00 0a002000 20002000 20003c00 21004500 
4e005400 49005400 59002000 6f007500 6d006c00 20002200 26006100 6d007000 3b006f00 75006d00 
6c003b00 22003e00 0d000a00 20002000 20002000 3c002100 45004e00 54004900 54005900 20007500 
75006d00 6c002000 22002600 61006d00 70003b00 75007500 6d006c00 3b002200 3e000d00 0a002000 
20002000 20003c00 21004500 4e005400 49005400 59002000 41007500 6d006c00 20002200
fffe3c00 3f007800 6d006c00 20007600 65007200 73006900 6f006e00 3d002200 31002e00 30002200 20006500 6e006300 6f006400 69006e00 67003d00 22007700 69006e00 64006f00 77007300 2d003100 32003500 32002200 20007300 74006100 6e006400 61006c00 6f006e00 65003d00 22007900 65007300 22003f00 3e00
<?xml version="1.0" encoding="windows-1252" standalone="yes"?>
NSString *myString = [[NSString alloc] initWithData:myData encoding:NSUTF16LittleEndianStringEncoding];
NSStringEncoding encoding;
NSError *error;
NSString *string = [NSString stringWithContentsOfFile:path usedEncoding:&encoding error:&error];
if (string) {
    NSLog(@"string   = %@", string);
    NSLog(@"encoding = %d", encoding);
} else {
    NSLog(@"stringWithContentsOfFile error: %@", error);
}