我有如下的curl请求
curl -H '授权:OAuth oauth_signature_method="明文",oauth_consumer_key="de4422f-f3ff-4929-bb65-eadc6f5218e2",oauth_token="85ab49a-9081-4641-812c-09d3130e3a8",oauth_signature="0723ba36-8d19 -4fab-9337-7e81004655c%26e385879b-0b57-481b-a8fc-8eb1426fc72"''https://external.ningapis.com/xn/rest/ippinn/1.0/User/?author=0n1s8aln92xhx&fields=iconUrl,电子邮件,url,全名、出生日期、性别'
我想使用 NSURLRequest 创建相同的请求 我尝试了一下,在论坛上搜索得到了很多解决方案,但没有成功
我做了以下代码
NSURL *url = [NSURL URLWithString:@"https://external.ningapis.com/xn/rest/ippinn/1.0/User/?author=0n1s8aln92xhr&fields=iconUrl,email,url,fullName,birthdate,gender"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setHTTPMethod:@"POST"];
NSString *authorization = @"OAuth ";
authorization = [authorization stringByAppendingFormat:@"oauth_signature_method=%@",@"PLAINTEXT"];
authorization = [authorization stringByAppendingFormat:@"&oauth_consumer_key=%@",@"de4422f-f3ff-4929-bb65-eadc6f5218e2"];
authorization = [authorization stringByAppendingFormat:@"&oauth_token=%@",@"85ab49a-9081-4641-812c-09d3130e3a8"];
authorization = [authorization stringByAppendingFormat:@"&oauth_signature=%@",@"0723ba36-8d19-4fab-9337-7e81004655c%26e385879b-0b57-481b-a8fc-8eb1426fc72"];
NSLog(@"authorization =>%@",authorization);
[request setValue: authorization forHTTPHeaderField: @"Authorization"];
self.connection = [NSURLConnection connectionWithRequest:request delegate:self];
if (_connection) {
self.receivedData = [[NSMutableData data] retain];
}
请告诉我哪里出错了
最佳答案
NSURL *url = [NSURL URLWithString:@"https://external.ningapis.com/xn/rest/ippinn/1.0/User/?author=0n1s8aln92xhr&fields=iconUrl,email,url,fullName,birthdate,gender"];
NSString *authorization = @"OAuth ";
authorization = [authorization stringByAppendingFormat:@"oauth_signature_method=%@",@"PLAINTEXT"];
authorization = [authorization stringByAppendingFormat:@"&oauth_consumer_key=%@",@"de4422f-f3ff-4929-bb65-eadc6f5218e2"];
authorization = [authorization stringByAppendingFormat:@"&oauth_token=%@",@"85ab49a-9081-4641-812c-09d3130e3a8"];
authorization = [authorization stringByAppendingFormat:@"&oauth_signature=%@",@"0723ba36-8d19-4fab-9337-7e81004655c%26e385879b-0b57-481b-a8fc-8eb1426fc72"];
NSData *postData = [authorization dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *urlRequest = [[[NSMutableURLRequest alloc] init] autorelease];
[urlRequest setURL:url];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
[urlRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[urlRequest setValue:@"utf-8" forHTTPHeaderField:@"charset"];
[urlRequest setHTTPBody:postData];
[urlRequest setTimeoutInterval:60.0];
NSURLConnection *tmpConn = [[[NSURLConnection alloc] initWithRequest:urlRequest delegate:self] autorelease];
self.connection = tmpConn;//I suppose "connection" is a retained property
if (_connection) {
self.receivedData = [[NSMutableData data] retain];
}
关于iphone - 如何使用 HTTPHeaderField 创建 NSURLRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11612913/