使用用户名和密码对NSURLRequest执行curl命令 [英] curl command to NSURLRequest with username and password

查看:126
本文介绍了使用用户名和密码对NSURLRequest执行curl命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在向服务器发出请求。这是Curl命令:

I'm making a request to a server. here's the Curl command:

curl -u admin@example.com:12321 -d 'oauth_signature_method=PLAINTEXT&oauth_consumer_key=0d716e57-5ada-4b29-a33c-2f4af1b26837&oauth_signature=f0963fa5-1259-434f-86fc-8a17d14b16ca%26' 'https://external.ningapis.com/xn/rest/apiexample/1.0/Token?xn_pretty=true'

现在我想用NSURLRequest发出请求,有人知道怎么做吗?

And now I want to make a request with NSURLRequest, does anybody know how to do?

推荐答案

我已经解决了这个问题,这是代码。

I've solved this, here's the code.

NSURL *url = [NSURL URLWithString:@"https://external.ningapis.com/xn/rest/tapatalk/1.0/Token?xn_pretty=true"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
NSString *theUsername = @"username";
NSString *thePassword = @"password";
NSString *loginString = [NSString stringWithFormat:@"%@:%@",theUsername,thePassword];
NSString *authString = [@"Basic " stringByAppendingFormat:@"%@", [self base64Encoding:[loginString dataUsingEncoding:NSUTF8StringEncoding]]];
[request setValue:authString forHTTPHeaderField:@"Authorization"];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

NSString *requestStr = @"oauth_signature_method=PLAINTEXT&oauth_consumer_key=yourconsumerkey&oauth_signature=yourconsumerkeysecret%26";
[request setHTTPBody:[requestStr dataUsingEncoding:NSUTF8StringEncoding]];
[request setValue:[NSString stringWithFormat:@"%d", [[requestStr dataUsingEncoding:NSUTF8StringEncoding] length]] forHTTPHeaderField:@"Content-Length"];

NSError *error = nil;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];

这篇关于使用用户名和密码对NSURLRequest执行curl命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆