2014-12-13 88 views
4

我刚开始ios开发,我试图与我的api交换数据当我做POST请求时,一切都很好,但当我'想要做一个GET请求,我得到以下错误:错误域= NSURLErrorDomain代码= -1017“操作不能

Error Domain=NSURLErrorDomain Code=-1017 "The operation couldn’t be completed. (NSURLErrorDomain error -1017.)" UserInfo=0x145a2c00 {NSErrorFailingURLStringKey= http://myAPI.com/ , _kCFStreamErrorCodeKey=-1, NSErrorFailingURLKey= http://myAPI.com , _kCFStreamErrorDomainKey=4, NSUnderlyingError=0x145b21d0 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1017.)"}

有人能解释发生了什么错误,我怎么能解决这个问题

我的要求:

-(void)hitApiWithURL:(NSString*)url HTTPMethod:(NSString*)HTTPMethod params:(NSDictionary*)params successBlock:(successTypeBlock)success failureBlock:(errorTypeBlock)failure{ 


    NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration]; 
    NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:nil]; 


    [sessionConfig setHTTPAdditionalHeaders:@{@"Content-type": @"application/json"}]; 
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]]; 
    [request setHTTPMethod:HTTPMethod]; 

    // The body 
    NSError *error = nil; 
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:params options:0 error:&error]; 
    [request setHTTPBody:jsonData]; 


    NSURLSessionDataTask *dataTaks = [session dataTaskWithRequest:request]; 
    [dataTaks resume]; 

    NSLog(@"dataTask started"); 

} 


- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task 
didCompleteWithError:(NSError *)error { 
    if (error) { 
     //Gives my error 
    } 
    else { 
     // do something: 
    } 
} 

回答

6

你有什么与...错误您的JSON参数:

kCFURLErrorCannotParseResponse = -1017

+0

由于发生时,工作!这样一个简单的错误。我会尽快接受你的维修人员! – Jab 2014-12-13 12:44:50

+3

你好user1141351.如果你的问题解决了,你为什么不与所有人分享你的解决方案?这对一些人会有帮助。请添加它。 – 2015-04-14 11:05:45

+1

可以请你建议你做了什么改变?,因为我使用GET – Mukesh 2016-01-12 07:36:14

0

对于谁得到了错误代码-1017任何其他人 - 我通过手动设置我的HTTP标头在我的http请求固定它。我认为服务器在解析HTTP标头时遇到问题,因为我的标头没有像这样的引号:Authorization : "1i2j12j",而不是字符串"Authorization" : "qii2nl32j2l3jel"。祝你好运。

事情是这样的:

NSMutableDictionary* newRequestHTTPHeader = [[NSMutableDictionary alloc] init]; 
[newRequestHTTPHeader setValue:authValue forKey:@"\"Authorization\""]; 
[newRequestHTTPHeader setValue:contentLengthVal forKey:@"\"Content-Length\""]; 
[newRequestHTTPHeader setValue:contentMD5Val forKey:@"\"Content-MD5\""]; 
[newRequestHTTPHeader setValue:contentTypeVal forKey:@"\"Content-Type\""]; 
[newRequestHTTPHeader setValue:dateVal forKey:@"\"Date\""]; 
[newRequestHTTPHeader setValue:hostVal forKey:@"\"Host\""]; 
[newRequestHTTPHeader setValue:publicValue forKey:@"\"public-read-write\""]; 

//the proper request is built with the new http headers. 
NSMutableURLRequest* request2 = [[NSMutableURLRequest alloc] initWithURL:request.URL]; 
[request2 setAllHTTPHeaderFields:newRequestHTTPHeader]; 
[request2 setHTTPMethod:request.HTTPMethod]; 
6

如果您忘记提及列举HTTPMethod,错误也可以发生,我所面临的问题“NSURLErrorDomain代码= -1017”,不知何故,我忘了添加行“的要求。列举HTTPMethod = “POST”。

添加行,我的代码工作完美之后。

0

请检查该链接

Alamofire.request(method, "(URLString)" , parameters: parameters, headers: headers).responseJSON { response in } 

在POST方法您还添加

parameters:parameters, encoding: .JSON 

(如果您添加了编码一线得到POST然后错误会来“无法解析响应”)

第二个请的标题为好。

["authentication_token" : "sdas3tgfghret6grfgher4y"] 

然后解决了这个问题。

+0

此问题的格式需要清理。 – Kmeixner 2016-06-03 17:14:56

4

此错误,当你执行GET请求与身体集(setHTTPBody

相关问题