微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用IOS进行身份验证的Restful API调用

我正在使用prestashop API使用restful API调用的应用程序.我是 IOS的新手我在 android中编写了相同的方法
InputStream is = null;
try {

 DefaultHttpClient client = new DefaultHttpClient();  

    /* adding credentials as it is RESTful call */
    String username = "xyz";
    String password = "";
    client.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST,AuthScope.ANY_PORT),new UsernamePasswordCredentials(username,password));  
// HTTP get request       
HttpGet get = new HttpGet("http://www.example.com/api/");
HttpResponse responseGet;
responseGet = client.execute(get);
is = responseGet.getEntity().getContent();
} catch (ClientProtocolException e) {
    Log.e("HTTP Request","Client Protocol exception" );
} catch (IOException e) {
    Log.e("HTTP Request","IO exception" );
}

它适用于Android.对于IOS,我使用了这种编码,但我没有从服务器获取数据.

Nsstring *userName = @"XYZ";
Nsstring *password = @"";
//setting the string of the url taking from appliance IP.

Nsstring *urlString = @"http://www.example.com/api/";

NSMutableuRLRequest *request= [[NSMutableuRLRequest alloc] init];

[request setURL:[NSURL URLWithString:urlString]];

[request setHTTPMethod:@"GET"];

Nsstring *str1 = [Nsstring stringWithFormat:@"%@:%@",userName,password];

NSLog(@" str1 %@",str1);

[request addValue:[Nsstring stringWithFormat:@"Basic %@",str1] forHTTPHeaderField:@"Authorization"];

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

Nsstring *str = [[Nsstring alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"str: %@",str);

请告诉我我做错了什么并提供任何解决方案.

谢谢!

解决方法

您可以通过这种方式构建URL字符串,它应该可以工作: –
Nsstring *str1 = [Nsstring stringWithFormat:@"http://%@:%@@www.example.com/api",password];

我不相信使用HTTP头字段

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐