NSURLConnection 失败 - 1003 [英] NSURLConnection failure - 1003

查看:148
本文介绍了NSURLConnection 失败 - 1003的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用命令从某个 url 检索数据:

I try to retrive data from certain url with command:

-(NSMutableData *) callUrl: (NSString *)url withData:(NSMutableDictionary *)data delegate:(id) delegate {

    NSURL *executeUrl = [NSURL URLWithString:<string>];
    NSURLRequest *request = [NSURLRequest requestWithURL: executeUrl
                             cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                             timeoutInterval:60];

    NSMutableData *receivedData = nil;

    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:delegate];
    if (theConnection) {
        receivedData = [[NSMutableData data] retain];
    } else {
        @throw @"Connection error";
    }

    return receivedData;
}

在委托中(在 connectionDidFinish 和 connectionDidFailWithError 之后)我这样做:

In delegate (both after connectionDidFinish and connectionDidFailWithError) I do:

//some uninvasive alerts
// release the connection, and the data object
[connection release];
[receivedData release];

问题是当我提供错误的 url 时,我得到了正确的错误 - 这是很好的部分 - 但是我想执行第二个 url - 肯定好,我有 1003 错误 - NSURLErrorCannotFindHost.

Problem is when I provide bad url I got proper error - it's good part - but then I want to execute second url - good for sure, I've got 1003 error - NSURLErrorCannotFindHost.

大约 1-2 分钟后,我成功调用了 url 并获取了数据.我怀疑一些超时和端口业务,但更改 NSURLRequest 中的超时不会改变任何事情.

After around 1-2 min I'm succesfully call url and get data. I suspect some timeouts and ports business, but changing timeout in NSURLRequest doesn't change a thing.

更新

事实证明 - 管理员在通过 WiFi 网络访问 DNS 服务器时遇到了一些问题.代码没问题.感谢您的回复.如果有类似的问题:尝试使用 ip 地址而不是主机名.

As it turned out - Administrators had some issues with DNS server reached through WiFi network. Code is fine. Thanks for response. If some has similiar problems: try ip address instead of hostname.

推荐答案

我做了两件事来解决这个问题:

I did 2 things to fix this issue :

  1. 我在启动 NSUrlConnection 之前使用过这个

  1. I used this before initiating my NSUrlConnection

[NSURLConnection cancelPreviousPerformRequestsWithTarget:self];

不知道是哪一个修复了它,但问题已解决.

Don't know which one fixed it but the issue was resolved.

这篇关于NSURLConnection 失败 - 1003的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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