如何使用目标c在url中附加查询字符串?

时间:2009-10-12 06:30:42

标签: iphone objective-c

NSString * post = @“& username = adam& password = test”;     NSData * postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];     NSString * postLength = [NSString stringWithFormat:@“%d”,[postData length]];     NSMutableURLRequest * request = [[[NSMutableURLRequest alloc] init] autorelease];

[request setURL:[NSURL URLWithString:@"http://ws.myurl.com/svc-public/iPhoneAuthenticate.aspx?uname=adam&pword=test"]];

[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSData *returnData = [ NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil ];
NSString *response = [[NSString alloc] initWithData:returnData encoding:NSASCIIStringEncoding];
NSLog(response);

这是我的网址http://ws.myurl.com/svc-public/iPhoneAuthenticate.aspx,现在我需要动态添加用户名和密码。

如何在url中附加查询字符串?请帮帮我。

1 个答案:

答案 0 :(得分:2)

你可以这样做:

[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://ws.myurl.com/svc-public/iPhoneAuthenticate.aspx?uname=%@&pword=%@",name, password]]];