获取错误域= NSURLErrorDomain代码= -1004“无法连接到服务器”。

时间:2012-09-14 17:21:20

标签: iphone .net ios nsurlconnection

我使用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

但是当我在浏览器中点击此网址时工作正常,但在我的应用中我收到此错误

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

请帮我解决可能存在的问题

1 个答案:

答案 0 :(得分:2)

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

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

应该更容易进行故障排除。