iOS无法执行HTTP GET请求 - 错误域= NSURLErrorDomain代码= -1012 [英] iOS Can't perform HTTP GET request -Error Domain=NSURLErrorDomain Code=-1012

查看:226
本文介绍了iOS无法执行HTTP GET请求 - 错误域= NSURLErrorDomain代码= -1012的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图执行HTTP Get请求,但我一直在获取Error Domain = NSURLErrorDomain Code = -1012错误。我的代码是:

  @try {
NSString * url = [[NSString alloc] initWithFormat:@%@ %@,@http://api.mintchat.com/agent/chats/,agentSecret];

NSLog(@****** Agents Active List URL Str - %@,url);
NSURL * nsUrl = [NSURL URLWithString:url];
NSLog(@****** Agents Active List URL - %@,nsUrl);

//发出请求,返回一个JSON响应,就像一个普通的旧字符串一样。
// NSData * jsonDataString = [[NSString stringWithContentsOfURL:[NSURL URLWithString:url] encoding:NSUTF8StringEncoding error:nil] dataUsingEncoding:NSUTF8StringEncoding];

NSError * error = nil;

NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:nsUrl];
[request setHTTPMethod:@GET];
[request setValue:API_ID forHTTPHeaderField:@X-API-ID];
[request setValue:API_SECRET forHTTPHeaderField:@X-API-SECRET];

NSHTTPURLResponse * response = nil;
NSData * urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:& response error:& error];

NSLog(@Response Code%d,[response statusCode]); ([response statusCode]> = 200&& [response statusCode]< 300){
NSString * responseData = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@Response ==>%@,responseData);
} else {
if(error)
NSLog(@ERror:%@,error);

@catch(NSException * e){
NSLog(@Exception - %@,e);

$ / code>

我要在JSon中得到回应,但由于事情并不奏效,所以我们正在尝试一些正常的回应。我确认了要添加到请求中的标题。我需要将这两个值添加到标题。



我得到的结果/日志是:
响应代码0


错误:错误域= NSURLErrorDomain代码= -1012操作无法完成
(NSURLErrorDomain错误-1012。)UserInfo = 0x8abf470 {
NSErrorFailingURLKey =。 ...... NSErrorFailingURLStringKey = ...... NSUnderlyingError = 0x8dafed0操作无法完成(kCFErrorDomainCFNetwork错误-1012。)
}




我也在网上搜了很多这个错误,有点帮助不了。我也确认了有关访问Web服务器API的信息。与其他网址也得到相同的结果。从3个网址中,我可以使用POST获得第一个网址的结果,但其他2个网址 - 无法通过它。



任何人都可以请帮助我问题。哪里出错?为什么不能访问api的。
非常感谢任何帮助。
谢谢

解决方案

这个错误是 NSURLErrorUserCancelledAuthentication


当用户将异步认证请求取消
时返回。这通常是通过点击用户名/密码对话框中的取消按钮
而不是用户尝试
进行身份验证而产生的。


这意味着您需要身份验证才能访问正在尝试访问的资源,而无需正确签署您的请求(错误的agentSecret或API_ID或API_SECRET)

I am trying to perform a HTTP Get request, but I keep on getting Error Domain=NSURLErrorDomain Code=-1012 error. MY code is :

@try {
    NSString *url = [[NSString alloc] initWithFormat:@"%@%@", @"http://api.mintchat.com/agent/chats/", agentSecret];

    NSLog(@"******Agents Active List URL Str - %@", url);
    NSURL *nsUrl = [NSURL URLWithString:url];  
    NSLog(@"******Agents Active List URL - %@", nsUrl);

   // Make the request, returning a JSON response, just as a plain old string.
   //NSData *jsonDataString = [[NSString stringWithContentsOfURL:[NSURL URLWithString:url] encoding:NSUTF8StringEncoding error: nil] dataUsingEncoding:NSUTF8StringEncoding];

    NSError *error = nil;

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:nsUrl];
    [request setHTTPMethod:@"GET"];
    [request setValue:API_ID forHTTPHeaderField:@"X-API-ID"];
    [request setValue:API_SECRET forHTTPHeaderField:@"X­-API-­SECRET"];

    NSHTTPURLResponse *response = nil;
    NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    NSLog(@"Response Code %d", [response statusCode]);
    if ([response statusCode] >= 200 && [response statusCode] < 300) {
        NSString *responseData = [ [NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
       NSLog(@"Response  ==> %@", responseData);        
    } else {
       if (error)
          NSLog(@"ERror : %@", error);
    }
}@catch(NSException *e) {
        NSLog(@"Exception - %@", e);
   }

I am to get response in JSon, but as things are not working so we are trying with some normal response. I confirmed the Headers to be added to the request. I need to add the both values to the headers. The url is also proper.

The results/logs I get is : Response Code 0

Error : Error Domain=NSURLErrorDomain Code=-1012 "The operation couldn’t be completed. (NSURLErrorDomain error -1012.)" UserInfo=0x8abf470 { NSErrorFailingURLKey=.......NSErrorFailingURLStringKey=......NSUnderlyingError=0x8dafed0 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1012.)" }

I also searched a lot on net reg this error, bit couldn't get any substantial help. I also confirmed regarding access to the web server api. With other url also I get the same results. From 3 urls, I could achieve results of 1st url using POST, but those other 2 urls - just could't get thru it.

Can anyone please help me with this problem. Where am going wrong ? Why can't access the api's. Any help is highly appreciated. Thanks

解决方案

This error is NSURLErrorUserCancelledAuthentication

Returned when an asynchronous request for authentication is cancelled by the user. This is typically incurred by clicking a "Cancel" button in a username/password dialog, rather than the user making an attempt to authenticate.

It means you need authentication to access the resource which you are trying to access w/o signing your request properly (wrong agentSecret or API_ID or API_SECRET)

这篇关于iOS无法执行HTTP GET请求 - 错误域= NSURLErrorDomain代码= -1012的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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