使用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头字段

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...