//1.创建post方式的 参数字符串url

1
2
3
4
5
6
7
8
9
10
11
12
13
14
+(NSString *)createPostURL:(NSMutableDictionary *)params
{
NSString *postString=@ "" ;
for (NSString *key in [params allKeys])
{
NSString *value=[params objectForKey:key];
postString=[postString stringByAppendingFormat:@ "%@=%@&" ,key,value];
}
if ([postString length]>1)
{
postString=[postString substringToIndex:[postString length]-1];
}
return postString;
}

//2.zwh -自定义的通用方法------post数据回服务器,并返回结果数据集

1
2
3
4
5
6
7
8
9
10
11
12
+(NSData *)getResultDataByPost:(NSMutableDictionary *)params
{
 
NSString *postURL=[Utility createPostURL:params];
NSError *error;
NSURLResponse *theResponse;
NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:BASEURL]];
[theRequest setHTTPMethod:@ "POST" ];
[theRequest setHTTPBody:[postURL dataUsingEncoding:NSUTF8StringEncoding]];
[theRequest addValue:@ "application/x-www-form-urlencoded" forHTTPHeaderField:@ "Content-Type" ];
return [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&theResponse error:&error];
}

//调用实例代码

1
2
3
4
5
6
7
NSMutableDictionary *params=[[NSMutableDictionary alloc] init];
[params setObject:@ "taobao.taobaoke.items.get" forKey:@ "method" ];
[params setObject:@ "num_iid,title,pic_url,price,score" forKey:@ "fields" ];
[params setObject:@ "淘宝帐户" forKey:@ "nick" ];
[params setObject:selectedItemCat.cid forKey:@ "cid" ];
[params setObject:@ "true" forKey:@ "is_mobile" ];
NSData *resultData=[Utility getResultData:params];

from:[url=http://blog.sina.com.cn/s/blog_56c7537001015j8q.html]