NSString *urlStr = @"http://localhost:8080/datainterface/interfacename";
NSMutableURLRequest *jsonRequest = [[NSMutableURLRequest alloc] init];
[jsonRequest setURL:[NSURL URLWithString:urlStr]];
[jsonRequest setHTTPMethod:@"POST"];
[jsonRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
NSDictionary *postDic = @{
@"p" : @"1",
@"psize" : @"20"
};
NSData *postData = [NSJSONSerialization dataWithJSONObject:postDic options:NSJSONWritingPrettyPrinted error:nil];
NSString *postStr = [[NSString alloc] initWithData:postData encoding:NSUTF8StringEncoding];
postStr = [NSString stringWithFormat:@"q=%@&accesstoken=",postStr];
NSLog(@"post:%@",postStr);
postData = [postStr dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
[jsonRequest setHTTPBody:postData];
[NSURLConnection sendAsynchronousRequest:jsonRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *res ponse, NSData *data, NSError *connectionError) {
NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
NSDictionary *jsonQ = [jsonDic objectForKey:@"q"];
NSLog(@"1=====%@", jsonQ);
//NSLog(@"2=====%@", jsonDic);
}];
Objective-C使用form方式提交json
最新推荐文章于 2021-03-17 22:06:09 发布