开发者

How to append querystring in url using objective c?

开发者 https://www.devze.com 2022-12-08 11:40 出处:网络
NSString *post = @\"&username=adam&password=test\"; NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

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:@开发者_JAVA百科"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);

This is my url http://ws.myurl.com/svc-public/iPhoneAuthenticate.aspx, now I need to append the username and password dynamically.

How to append querystring in url? Please help me.


You can do it this way:

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

精彩评论

暂无评论...
验证码 换一张
取 消