2012-02-05 70 views
2

虽然有很多相关的问题,但我没有看到一个将多个键/值对添加到NSURLRequest的地址。将键/值对添加到NSMutableURLRequest

我想为请求添加一个简单的用户名和密码。我不确定如何添加多个对以及编码。我得到了有效的连接和响应,但响应表明它无法正确解释请求。

这是我得到的。提前致谢。

NSURL *authenticateURL = [[NSURL alloc] initWithString:@"https://www.the website.com/authenticate"]; 
NSMutableURLRequest *authenticateRequest = [[NSMutableURLRequest alloc] initWithURL:authenticateURL]; 
[authenticateRequest setHTTPMethod:@"POST"]; 
NSString *myRequestString = @"username="; 
[myRequestString stringByAppendingString:username]; 
[myRequestString stringByAppendingString:@"&"]; 
[myRequestString stringByAppendingString:@"password="]; 
[myRequestString stringByAppendingString:password]; 
NSData *requestData = [NSData dataWithBytes:[myRequestString UTF8String] length:[myRequestString length]]; 
[authenticateRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"]; 
[authenticateRequest setHTTPBody: requestData]; 
[authenticateRequest setTimeoutInterval:30.0]; 

connection = [[NSURLConnection alloc] initWithRequest:authenticateRequest delegate:self]; 

回答

5

您没有使用正确NSString(你myRequestString,事实上,将显示 “用户名=”)。相反,试试这个:

NSMutableString *myRequestString = [NSMutableString stringWithString:@"username="]; 
[myRequestString appendString:username]; 
[myRequestString appendString:@"&password="]; 
[myRequestString appendString:password]; 

而且这个伟大的答案,只是一个典型的例子代码:

-(NSString *)buildKeyValuePostString 
    { 
    NSString *username = @"[email protected]"; 
    NSString *password = @"macintosh"; 

    NSMutableString *r = [NSMutableString stringWithString:@""]; 

    [r appendString:@"command=listFileNames"]; 
    [r appendString:@"&"]; 

    [r appendString:@"name=blah"]; 
    [r appendString:@"&"]; 

    [r appendString:@"user="]; 
    [r appendString: [username stringByUrlEncoding] ]; 
    [r appendString:@"&"]; 

    [r appendString:@"password="]; 
    [r appendString: [password stringByUrlEncoding] ]; 

    return r; 
    } 

和这里的类别做URL编码的困难/恼人的工作

-(NSString *)stringByUrlEncoding 
    { 
    return (NSString *)CFBridgingRelease(
      CFURLCreateStringByAddingPercentEscapes(
       NULL, 
       (CFStringRef)self, 
       NULL, 
       (CFStringRef)@"!*'();:@&=+$,/?%#[]", 
       kCFStringEncodingUTF8) 
       ); 

    // with thanks to http://www.cocoanetics.com/2009/08/url-encoding/ 
    // modified for ARC use 2014 
    } 

希望它有助于某人。

+0

OK - 谢谢。但即使修复后我的请求仍然没有正确解释。用户名和密码的设置是否正确完成并编码? – 2012-02-05 03:24:29

+0

嗯,除了'NSMutableString'部分,其余的代码看起来应该像你期望的那样运行。我的代码非常类似于您所写的工作正常。 – donkim 2012-02-05 03:33:11

+0

NSString * myRequestString = @“h = 123”; NSData * myRequestData = [NSData dataWithBytes:[myRequestString UTF8String] length:[myRequestString length]];我们可以使用NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@“http:// localhost:8082/45”]]; [请求setHTTPMethod:@“POST”]; [request setHTTPBody:myRequestData]; [request setValue:@“application/x-www-form-urlencoded”forHTTPHeaderField:@“content-type”]; [[NSURLConnection alloc] initWithRequest:request delegate:self]; – donkim 2012-02-05 03:34:02

0

假设你的意思是你想HTTP报头字段添加到请求,请使用:

-addValue:forHTTPHeaderField: