版权声明:本文为博主原创文章,未经博主允许不得转载。 https://cloud.tencent.com/developer/article/1338104
// 1. url
NSString *urlString = @"http://127.0.0.1/login.php";
NSURL *url = NSURL URLWithString:urlString;
// 2. 可变的请求
NSMutableURLRequest *request = NSMutableURLRequest requestWithURL:url cachePolicy:1 timeoutInterval:2.0f;
// 2.1 指定http的访问方法,服务器短才知道如何访问
request.HTTPMethod = @"POST";
// 2.2 指定数据体,数据体的内容可以从firebug里面直接拷贝
// username=zhangsan&password=zhang
NSString *username = @"张三";
NSString *pwd = @"zhang";
NSString *bobyStr = NSString stringWithFormat:@"username=%@&password=%@", username, pwd;
// 2.2.1 跟服务器的交互,全部传递的二进制
request.HTTPBody = bobyStr dataUsingEncoding:NSUTF8StringEncoding;
// 3. 连接
[NSURLConnection sendAsynchronousRequest:request queue:NSOperationQueue mainQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
// 反序列化
id result = NSJSONSerialization JSONObjectWithData:data options:0 error:NULL;
NSLog(@"%@", result);
}];