2014-12-13 184 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

相關問題