一:与上一篇博客中的GET方法类似
只不过需要多注意,如果要改变请求的类型,需要生成NSMutableURLRequest对象才可以设置请求的类型。
NSURL *url = [NSURL URLWithString:
@"http://120.25.226.186:32812/login"];
// 创建请求
NSMutableURLRequest *request =
[NSMutableURLRequest requestWithURL:url];
request.HTTPMethod =
@"POST";
// 设置为POST请求
request.HTTPBody = [
@"username=122&pwd=1222" dataUsingEncoding:NSUTF8StringEncoding];
// 设置请求体,需要注意将字符串转换为DATA
// 发送请求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError *
_Nullable connectionError) {
NSString *str =
[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@",str);
}];
转载于:https://www.cnblogs.com/BJTUzhengli/p/5152440.html