【发布时间】:2012-02-05 03:09:08
【问题描述】:
虽然有很多相关的问题,但我没有看到一个解决向 NSURLRequest 添加多个键/值对的问题。
我想在请求中添加一个简单的用户名和密码。我不确定如何添加多对以及编码。我得到了有效的连接和响应,但响应表明它无法正确解释请求。
这就是我所拥有的。提前致谢。
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];
【问题讨论】:
标签: ios cocoa-touch