NSURLConnection block异步post请求
- UID
- 1066743
|
NSURLConnection block异步post请求
//地址字符串
NSString *urlString = @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx";
//资源定位符
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
NSString *dataString = @"date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213";
//把这个字符串 转成NSData类型的对象
NSData *postData = [dataString dataUsingEncoding:NSUTF8StringEncoding];
//设置请求方式 大写 GET不用设置,但是post必须设置
[urlRequest setHTTPMethod"OST"];
//把需要上传的data放进request里面
[urlRequest setHTTPBody:postData];
// 创建子线程
NSOperationQueue *queue = [NSOperationQueue new];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
if (connectionError == nil) {
// response 携带的是接口的一些信息
// data 请求下来的数据, 需要使用的
// connectionError 错误信息
NSDictionary *dic = [NSJSONSerialization
JSONObjectWithData:data optionsNSJSONReadingAllowFragments)
error:nil];
NSLog(@"%@",dic);
}
}];
//取消子线程
[queue cancelAllOperations]; |
|
|
|
|
|