【问题标题】:Getting Error Domain=NSURLErrorDomain Code=-1004 "Could not connect to the server."获取错误域 = NSURLErrorDomain 代码 = -1004 “无法连接到服务器。”
【发布时间】:2012-09-14 17:21:20
【问题描述】:

我使用 NSURLConnection 从我的 ios 应用程序调用 Web 服务。这是我的代码

#define kBaseServerUrl       @"http://winshark.softwaytechnologies.com/bariodwcfservice/baroidservice.svc/Json/"

#define kProductServiceUrl @"Products"

-(void)callService{
    NSURL *url;
    if (currState == ProductServiceState) {
        url = [NSURL URLWithString:[NSString  stringWithFormat:@"%@%@/%d",kBaseServerUrl,kProductServiceUrl,[self getRevisionNumberForProduct]]];
    }
    else if(currState == TankStrickerServiceState){
        url = [NSURL URLWithString:[NSString  stringWithFormat:@"%@%@/%d",kBaseServerUrl,kTankStrickerServiceUrl,[self getRevisionNumberForTankStricker]]];
    }else if(currState == CalculationServiceState){
        url = [NSURL URLWithString:[NSString  stringWithFormat:@"%@%@/%d",kBaseServerUrl,kCalculationServiceUrl,[self getRevisionNumberForCalculation]]];
    }else{
        //Future Use
    }
    NSLog(@"%@",[url absoluteString]);
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
    NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];

    NSLog(@"%@",urlConnection);
}

它最终调用了这个网址http://winshark.softwaytechnologies.com/bariodwcfservice/baroidservice.svc/Json/Products/123

但是当我在浏览器中点击这个 url 时它工作正常,但是在我的应用程序中我得到了这个错误

Error Domain=NSURLErrorDomain Code=-1004 "Could not connect to the server."

请帮我看看可能是什么问题

【问题讨论】:

  • 你是在设备上还是在模拟器上测试这个?

标签: iphone .net ios nsurlconnection


【解决方案1】:

看起来您设置正确,我怀疑您的对象有些问题 - 尝试通过将现有 URL 请求替换为以下内容来调用硬编码 URL:

NSURLRequest *urlRequest = [NSURLRequest 
requestWithURL:[NSURL URLWithString:@"http://www.google.com"]];

应该使故障排除变得更加容易。

【讨论】:

  • 这很奇怪,我在NSURLRequest class documentation 中找不到requestWithString 方法……你能帮帮我吗?
  • 我认为我能给你的最好帮助是为你指出一些好的其他 SO 代码;-) 查看stackoverflow.com/questions/19263186/… 以获取以这种方式发出的 URL 请求的示例。
  • @MaxGontar 没有。我认为他的意思是[NSURLRequest requestWithURL:[NSURL URLWithString:@"…"]]
  • @devios 谢谢,只是在记忆中并没有发现不匹配
猜你喜欢
  • 2017-07-22
  • 2017-11-30
  • 1970-01-01
  • 2022-07-25
  • 2016-11-30
  • 1970-01-01
  • 1970-01-01
  • 2014-10-11
  • 2017-05-07
相关资源
最近更新 更多