NSMutableURLRequest获取的NSString中的管道 [英] Pipes in NSString fetched by NSMutableURLRequest
问题描述
我有以下方法可以从网络服务器获取名为parsertest.html的文件的内容.但是,大约每5次运行程序一次,读取到的NSString在末尾包含一行管道
I have the following method to get the content of a file called parsertest.html from a webserver. However about one every 5 times i run my program, the fetched NSString contains a line of pipes at the end
|||||||||||||||||||||||||||||||||||||||||||| ||||||||||||||||||||||
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
这是我的代码:
-(NSString *)fetchTest
{
NSURL* url = [NSURL URLWithString:@"http://www.mywebserver.com/parsertest.html"];
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setValue:scraperUserAgent forHTTPHeaderField:@"User-Agent"];
NSURLResponse* response = nil;
NSError* error = nil;
NSData* data = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&error];
NSString *dataInStringFormat = [NSString stringWithUTF8String:[data bytes]];
NSLog(@"%@",dataInStringFormat);
return dataInStringFormat;
}
scraperUserAgent设置为"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:15.0) Gecko/20100101 Firefox/15.0"
scraperUserAgent is set to "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:15.0) Gecko/20100101 Firefox/15.0"
这是parsertest.html的内容
This is the content of parsertest.html
<parse>HELLO</parse>
<parse>World</parse>
<parse>digit</parse>
<parse>wow</parse>
<parse>hellonewitem</parse>
<parse>lastitem</parse>
这是发生错误时NSLog的完整输出:
this is the complete output of NSLog when the error occurs:
<parse>HELLO</parse>
<parse>World</parse>
<parse>digit</parse>
<parse>wow</parse>
<parse>hellonewitem</parse>
<parse>lastitem</parse>
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
感谢您的帮助!
Matthias
推荐答案
stringWithUTF8String
期望以NULL终止的C字符串,但[data bytes]
并非以NULL终止的字符串.使用
stringWithUTF8String
expects a NULL-terminated C string, but [data bytes]
is not NULL-terminated. Use
NSString *dataInStringFormat = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
相反.
这篇关于NSMutableURLRequest获取的NSString中的管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!