Error Domain=NSURLErrorDomain Code=-1017 “cannot parse response”

北战南征 提交于 2019-12-23 08:56:53

问题


Here I am getting error while I post json.

Error Domain=NSURLErrorDomain Code=-1017 "cannot parse response" UserInfo=0x7f89dac01a40 {NSUnderlyingError=0x7f89e0277a20 "cannot parse response", NSErrorFailingURLStringKey=http://test-onboard.qlc.in/FieldSense/authenticate, NSErrorFailingURLKey=http://test-onboard.qlc.in/FieldSense/authenticate, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-1, NSLocalizedDescription=cannot parse response}

Here is my code:

NSDictionary *dictionaryData=[[NSDictionary alloc]initWithObjectsAndKeys:self.txtUsername.text, @"userEmailAddress", self.txtPassword.text, @"password",nil];            
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:dictionaryData options:kNilOptions error:&error];

[request setURL:url];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:jsonData];

回答1:


I am also getting this error but i overcome this by adding a line

request.HTTPMethod = "POST"

for Objective-C programming use this:

[request setHTTPMethod:@"POST"];



回答2:


I had the same problem with Alamofire and the error caused this:

let encoding = Alamofire.ParameterEncoding.JSON let (encodedRequest, _) = encoding.encode(URLRequest, parameters: params)

my variable params was dict [:]. I changed to parameters: nil and it's work.




回答3:


For any other people who got error code -1017 -- I fixed it by manually setting my http headers in my http request. I think the server was having problems parsing the HTTP headers because instead of the string "Authorization" : "qii2nl32j2l3jel" my headers didn't have the quotes like this: Authorization : "1i2j12j". Good luck.

Something like this:

NSDictionary* 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];



回答4:


I got this 1017 error when setting parameters in a get request:

let headers = [
     "Cookie": "",
]

Alamofire.request(urlString, parameters: ["token": token],encoding: JSONEncoding.default, headers: headers).responseJSON { ...

This gave no error:

let headers = [
    "Cookie": "",
    "Authorization" : "Token " + token
]

Alamofire.request(urlString, method: .get, encoding: JSONEncoding.default, headers: headers).responseJSON {


来源:https://stackoverflow.com/questions/29820224/error-domain-nsurlerrordomain-code-1017-cannot-parse-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!