关闭

iOS post请求 (普通请求)

标签: iospost
183人阅读 评论(0) 收藏 举报
分类:

-(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;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:13697次
    • 积分:492
    • 等级:
    • 排名:千里之外
    • 原创:36篇
    • 转载:7篇
    • 译文:0篇
    • 评论:0条