【发布时间】:2015-04-03 15:39:24
【问题描述】:
我是 IOS 开发的新手。 我正在请求具有 POST 方法的 Web 服务,并在标题中接受两个参数密码和确认。 这是网址 https://abc.xyz.eu/pqr/Api 这是方法 api/Account/ChangePassword?password={password}&confirmation={confirmation}
如何调用这个方法 我尝试了很多代码,但得到 400,401,404 错误
最后我通过使用基本身份验证在 chrome 中获得 200 个状态代码。现在的问题是我很困惑如何传递基本身份验证和标头参数的值 请帮帮我`
NSString *UrlWithParameters=[NSString stringWithFormat:@"https://abc.xyz.eu/abc/api/Account/ChangePassword?"];
NSURL *ServiceURL =[NSURL URLWithString:UrlWithParameters];
NSMutableURLRequest * request =[NSMutableURLRequest requestWithURL:ServiceURL];
NSString *post = [NSString stringWithFormat:@"password=%@&confirmation=%@",self.txtNewPassord.text,self.txtConPassord.text];
NSData* postData = [post dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPMethod:@"POST"];
NSString *authValue = [NSString stringWithFormat:@"Basic %@",self.appDelegate.UserPasswordBase64];
[request setValue:authValue forHTTPHeaderField:@"Authorization"];
[request setValue:@"application/json" forHTTPHeaderField:@"accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"content-type"];
[request setHTTPBody:postData];
NSURLResponse *response;
NSError *err;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
`
【问题讨论】:
-
向我们展示您的代码,您是如何发送 HTTP 请求的?
-
你是否像这样正确绑定密码和确认值 NSString *temp = [NSString stringWithFormat:@"api/Account/ChangePassword?password=%@&confirmation=%@",pw,confirm]]
-
您是否尝试删除“[serviceRequest setValue:@"application/json" forHTTPHeaderField:@"content-type"];" ?毕竟你不是在发送 json 内容......
-
我试过它给我显示 404 错误,而且该方法支持 json 响应