-(void) postData
{
NSDictionary *paras =@{@"username":@"aaa",@"pwd":@"bbb"};
NSURL * url = [NSURLURLWithString:@"http://api.baiyue.baidu.com/sn/api/newchosenlist"];
NSMutableURLRequest *request = [NSMutableURLRequestrequestWithURL:url];
request.HTTPBody = [selfformatFormDataWithParas:paras];
request.HTTPMethod =@"POST";
NSURLResponse *response ;
NSError *error ;
NSData *data = [NSURLConnectionsendSynchronousRequest:request returningResponse:&response error:&error];
NSDictionary *dict = [NSJSONSerializationJSONObjectWithData:data options:kNilOptionserror:nil];//将请求得到的json数据 转化成字典
NSLog(@"%@",dict);
}
//设置请求体
-(NSData*) formatFormDataWithParas:(NSDictionary*) paras
{
NSMutableString *strReq = [[NSMutableStringalloc] initWithCapacity:1];
for (NSString * keyin paras.allKeys) {
if (strReq.length==0) {
[strReq appendFormat:@"%@=%@",key,[parasobjectForKey:key]];
}
else
{
[strReq appendFormat:@"&%@=%@",key,[parasobjectForKey:key]];
}
}
NSData *data = [strReqdataUsingEncoding:NSUTF8StringEncoding];
return data;
}
iOS post请求 (普通请求)
最新推荐文章于 2023-11-26 20:35:43 发布