ASIHttpRequest抛出错误,我无法弄清楚原因

时间:2011-07-13 18:43:19

标签: iphone objective-c asihttprequest

我一直在关注http://www.iphonedevsdk.com/forum/iphone-sdk-tutorials/76730-webservice-how.html给出的示例,以便尝试让我的iphone应用程序连接到网络服务。但是,当我使用以下代码时,它会抛出一个错误:

-(IBAction) startSend:(id)sender
{
    NSURL *url = [NSURL URLWithString:@"mywebsitehere"];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    NSString *xmlString = [NSString stringWithString:@""];
    xmlString=AS(xmlString,@"<id>");
    xmlString=AS(xmlString,@"the id would go here.");
    xmlString=AS(xmlString,@"</id>");
    xmlString=AS(xmlString,@"<Data>");
    xmlString=AS(xmlString,@"Binary Data would be read here.");
    xmlString=AS(xmlString, @"</Data>");
    NSData* xmlData=[xmlString dataUsingEncoding:NSUTF8StringEncoding];

    [request appendPostData:xmlData];

    [request setDidFinishSelector:@selector(requestCompleted:)];
    [request setDidFailSelector:@selector(requestError:)];

    [request setDelegate:self];
    [request startAsynchronous];
}

- (void)requestCompleted:(ASIHTTPRequest *)request
{
    NSString *responseString = [request responseString];
    NSLog(@"%d", responseString);
}


- (void)requestError:(ASIHTTPRequest *)request
{
    NSError *error = [request error];
    NSLog(@"error description : %@", [error localizedDescription]);
}

当我运行它时,我将收到以下错误消息:错误描述:无法启动HTTP连接。

什么会导致它无法启动HTTP连接?

3 个答案:

答案 0 :(得分:1)

您正在打印出错误对象的地址而不是对象本身。请改用:

NSLog(@"The error is: %@", error);

对于responseString也是如此:

NSLog(@"%@", responseString);

答案 1 :(得分:0)

首先,你确定它是c#?

其次,你可以试试

NSLog(@"error description : %@", [error localizedDescription]);

会更全面

答案 2 :(得分:0)

我知道这已经过时了,但尚未标记为已完成。我和它搏斗了一段时间,结果发现我有这样的圆顶:

NSString *webserviceBase=@"http://some/url"; NSURL *theUrl = [NSURL URLWithString:[webserviceBase stringByAppendingPathComponent:@"cmd"]];

请注意,stringByAppendingPathComponent:会破坏网址的http://部分。因此,如果您正在执行类似操作,请确保基础包含尾部斜杠,然后使用stringByAppendingString: