Iphone 如何使用HTTPHeaderField创建NSURLRequest
我有如下要求 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%2685E3879B-0b57-481b-a8fc-8eb1426fc72”' 'https://external.ningapis.com/xn/rest/ippinn/1.0/User/?author=0n1s8aln92xhx&fields=iconUrl,电子邮件,url,全名,出生日期,性别' 我想使用NSURLRequest创建相同的请求 我试过了,在论坛上搜索得到了很多解决方案,但没有成功 我做了以下代码Iphone 如何使用HTTPHeaderField创建NSURLRequest,iphone,Iphone,我有如下要求 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%2685E3879B-0b57-481b-a8fc-8eb1426fc72”'
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];
}
请告诉我哪里出了问题Hi@Teodor谢谢你的回答。我试过了,但仍然从服务器得到的响应是“code=1;reason=”未收到必需的参数。这些参数必须存在。oauth_signature_method;status=400;subcode=9;success=0;trace=“8a5d4a32-256c-4c0b-9e0d-54e9a05cebc7”}“可能的原因是什么。查看服务器端,查看服务需要从您的请求中获取的参数列表。必须有强制参数和可选参数(取决于它们的设置方式)。确保您在请求中发送了所有必需的参数。我遇到了观察。如果我用“GET”http方法发送,我们就会得到数据。谢谢你的回答。
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];
}