gpt4 book ai didi

iphone - 使用用户名和密码对 NSURLRequest 执行 curl 命令

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:50:38 25 4
gpt4 key购买 nike

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

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 发出请求,有人知道该怎么做吗?

最佳答案

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

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

关于iphone - 使用用户名和密码对 NSURLRequest 执行 curl 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10991278/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com