dataWithContentsOfURL不检索数据 [英] dataWithContentsOfURL doesn't retrieves data

查看:146
本文介绍了dataWithContentsOfURL不检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取此页面的源代码(http://www.sanmarinocard.sm),但dataWithContentsOfURL不会检索数据。

I'm trying to get source code of this page (http://www.sanmarinocard.sm) but dataWithContentsOfURL doesn't retrieves data.

NSURL *url = [NSURL URLWithString:@"http://www.sanmarinocard.sm"];
NSData *responseData = [NSData dataWithContentsOfURL:url];
NSString *result = [[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding];

直到几天前一切正常,但从昨天开始没有结果。

Until a few days ago everything worked fine, but since yesterday no results.

你能帮助我吗?谢谢。

推荐答案

我发现通过过滤用户代理阻止了我的应用程序。
我更改了值,现在它完美无缺。

I discovered that my application is blocked from the site by filtering the user-agent. I changed the value and now it works perfectly.

我修改了这样的代码:

NSString *userAgent = @"Mozilla/4.0 (compatible; MSIE 5.23; Mac_PowerPC)";
NSURL *url = [NSURL URLWithString: @"http://www.sanmarinocard.sm"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url];
[request setValue:userAgent forHTTPHeaderField:@"User-Agent"];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *result = [[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding];

谢谢!

这篇关于dataWithContentsOfURL不检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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