// 初始化URL
NSURL *url = [NSURL URLWithString:@"http://121.196.200.215"];
// 初始化Request
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// 设置请求方式
request.HTTPMethod = @"POST";
// 设置请求体
NSDictionary *dic = @{@"UserName":@"qdx", @"UserPwd":@"123"};
// 字典转换为二进制
NSData *data = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];
request.HTTPBody = data;
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
// 二进制转换成字符串
// NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
// 二进制装换成字典
// NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
// 获取请求状态码
NSLog(@"StatusCode : %ld", (long)[(NSHTTPURLResponse *)response statusCode]);
}];
[task resume];
NSURLSession网络请求
最新推荐文章于 2022-03-20 03:05:13 发布