2

我有如下卷曲请求

curl -H '授权:OAuth oauth_signature_method="PLAINTEXT",oauth_consumer_key="de4422f-f3ff-4929-bb65-eadc6f5218e2",oauth_token="85ab49a-9081-4641-812c-09d3130e3a8",oauth_signature="0723ba36-8-8 9337-7e81004655c%26e385879b-0b57-481b-a8fc-8eb1426fc72"' https://external.ningapis.com/xn/rest/ippinn/1.0/User/?author=0n1s8aln92xhx&fields=iconUrl,email,url,fullName,birthdate ,性别'

我想使用 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];
}

请建议我哪里出错了

4

1 回答 1

2
    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];
    }
于 2012-07-23T13:05:44.843 回答