简体   繁体   English

编码以获取URL IOS的​​内容

[英]Encoding for getting contents of a url IOS

I want to get the contents of a url hosted by me. 我想获取我托管的URL的内容。 The contents of the url is 网址的内容是

<p class="citation_style_APA">Blaine, J. D. (1992). <i>Buprenorphine: An alternative treatment for opioid dependence</i>. Rockville, MD: U.S. Dept. of Health and Human Services, Public Health Service, Alcohol, Drug Abuse, and Mental Health Administration, National Institute on Drug Abuse. </p>

Below is my code to get the above contents in a string. 下面是我的代码以字符串形式获取上述内容。

NSString *s =[NSString stringWithFormat:@"%@",@"xyz.com"];
url = [NSURL URLWithString:s];
NSString *content = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:nil];
NSLog(@"%@",content);

I am getting null, can anyone tell where i am going wrong. 我越来越空了,谁能告诉我我要去哪里错了。

Try a complete web address, like @" http://www.xyz.com/myfile.html ". 尝试使用完整的网址,例如@“ http://www.xyz.com/myfile.html ”。 Once you get it working, you'll want to change to an asynch technique. 一旦它开始工作,您将需要更改为一种异步技术。

You need to do two things. 您需要做两件事。

1. you should use a valid url

2. NSString *content=[[NSString string] initWithContentsOfURL:url encoding:NSASCIIStringEncoding error:Nil];

Now check your Log; 现在检查您的日志; It'll definitely work. 肯定会的。

Try this way 试试这个

NSString *strUrl  = < YOUR URL >
NSURL    *url     = [NSURL URLWithString:[strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSError  *error   = nil;
NSString *content = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:&error];
if(content==nil)
    NSLog(@"%@",error!=nil ? error.description : @"");
else
    NSLog(@"%@",content);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM