2012-02-06 115 views
4

如何将GET参数添加到ASIHttpRequest?如何将GET参数添加到ASIHttpRequest?

我想以编程方式从http://mysite.com/serverhttp://mysite.com/server?a=1&b=2

我有我的所有参数作为NSDictionary对象中的键值对。

感谢

+1

这已经在这里讨论: http://stackoverflow.com/questions/718429/creating-url-query-parameters-from-nsdictionary -objects-in-objectivec 希望这有助于! – Goeran 2012-02-06 12:02:06

回答

5

使用字符串格式像

NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"1",@"a",@"2",@"b", nil]; 

NSMutableString *prams = [[NSMutableString alloc] init]; 

for (id keys in dict) { 
    [prams appendFormat:@"%@=%@&",keys,[dict objectForKey:keys]]; 
} 
NSString *removeLastChar = [prams substringWithRange:NSMakeRange(0, [prams length]-1)]; 
NSString *urlString = [NSString stringWithFormat:@"http://mysite.com/server?%@",removeLastChar]; 

NSLog(@"urlString %@",urlString); 
+0

它不会转义特殊字符。 – jweyrich 2013-09-23 21:28:33