- (void)getMethod{
NSString *username = @"123456";
NSString *password = @"123456";
//用户名和密码在同一个url(用户名和密码不能为中文,有中文需要转译)
NSString *string = [NSString stringWithFormat:@"http://10.10.161.56/login.php?username=%@&password=%@",username, password];
//如果有汉字,转译成二进制字符串
string = [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:string];
NSURLRequest *request = [NSURLRequest requestWithURL:url];//创建连接
[NSURLConnection sendAsynchronousRequest:request queue:0 completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
//请求结束, 获得想请求的数据
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
NSLog(@"%@", dict);
}];
}
#pragma mark----创建POST请求
- (void)postMethod{
//url中不存在用户名和密码
NSString *string = @"http://10.10.161.8/login.php";
NSURL *url = [NSURL URLWithString:string];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//设置网络请求方式(默认为GET)
request.HTTPMethod = @"POST";
NSString *username = @"张三";
NSString *userpassword = @"123456";
NSString *bodyStr = [NSString stringWithFormat:@"username=%@&userpassword=%@",username, userpassword];
//添加请求体
request.HTTPBody = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
[NSURLConnection sendAsynchronousRequest:request queue:0 completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
//完成请求
NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
NSLog(@"%@",connectionError);
}];
NSLog(@"11111");
}