//创建请求路径
NSString *strURL = [NSStringstringWithFormat:@"http://192.168.10.252/upload/mapi/index.php?act=register&r_type=1&email=%@&password=%@&user_name=%@",@"123456@qq.com",@"123456",@"zhangsan"];
//通过url创建网络请求
NSURLRequest *request = [NSURLRequestrequestWithURL:[NSURLURLWithString:strURL]];
NSError *error =nil;
//同步方式连接服务器
NSData *data = [NSURLConnectionsendSynchronousRequest:request
returningResponse:nilerror:&error];
//json 解析返回数据
NSDictionary *dic = [NSJSONSerializationJSONObjectWithData:data
options:kNilOptionserror:nil];
NSLog(@"%@",[dicobjectForKey:@"info"]);
2.post 同步请求
//通过url创建网络请求
NSString *strURL = [NSStringstringWithFormat:@"http://192.168.10.252/upload/mapi/index.php?"];
//通过url创建网络请求
NSMutableURLRequest *request = [NSMutableURLRequestrequestWithURL:[NSURLURLWithString:strURL]];
//设置请求方式也POST(默认是GET)
[request setHTTPMethod:@"POST"];
//设置请求参数
NSString *body = [NSStringstringWithFormat:@"act=%@&r_type=%d&email=%@&password=%@&user_name=%@",@"register",1,@"12345@qq.com",@"123456",@"lisi"];
//需要NSUTF8StringEncoding转码
[request setHTTPBody:[bodydataUsingEncoding:NSUTF8StringEncoding]];
//同步方式连接服务器
NSData *data = [NSURLConnectionsendSynchronousRequest:request
returningResponse:nilerror:nil];
//json 解析返回数据
NSDictionary *dic = [NSJSONSerializationJSONObjectWithData:data
options:kNilOptionserror:nil];
NSLog(@"%@",[dicobjectForKey:@"info"]);
3.GET 异步代码块请求
NSString *strURL = [NSString
stringWithFormat:@"http://192.168.10.252/upload/mapi/index.php?act=register&r_type=1&email=%@&password=%@&user_name=%@",@"123456@qq.com",@"123456",@"zhangsan"];
NSURLRequest *request = [NSURLRequest
requestWithURL:[NSURL
URLWithString:strURL]];
NSError *error =
nil;
NSLog(@"111==%@",[NSThread
currentThread]);
//创建异步代码方式
[NSURLConnection
sendAsynchronousRequest:request
queue:[NSOperationQueue
new] completionHandler:^(NSURLResponse *response,
NSData *data, NSError *connectionError) {
NSLog(@"222==%@",[NSThread
currentThread]);
//网络请求结束
NSDictionary *dic = [NSJSONSerialization
JSONObjectWithData:data options:kNilOptions
error:nil];
NSLog(@"%@",[dic
objectForKey:@"info"]);
//回到主线程,去刷新界面
// self performSelectorOnMainThread:<#(SEL)#> withObject:<#(id)#> waitUntilDone:<#(BOOL)#>
dispatch_async(dispatch_get_main_queue(), ^{
//回主线程要做的事情
});
}];
4.POST 异步代码块请求
NSString *strURL = [NSStringstringWithFormat:@"http://192.168.10.252/upload/mapi/index.php?"];
NSMutableURLRequest *request = [NSMutableURLRequestrequestWithURL:[NSURLURLWithString:strURL]];
[request setHTTPMethod:@"POST"];
[request setTimeoutInterval:10];
NSString *body = [NSStringstringWithFormat:@"act=%@&r_type=%d&email=%@&password=%@&user_name=%@",@"register",1,@"12345@qq.com",@"123456",@"lisi"];
[request setHTTPBody:[bodydataUsingEncoding:NSUTF8StringEncoding]];
[NSURLConnectionsendAsynchronousRequest:request
queue:[NSOperationQueuemainQueue]
completionHandler:^(NSURLResponse *response,NSData *data,
NSError *connectionError) {
NSDictionary *dic = [NSJSONSerializationJSONObjectWithData:data
options:kNilOptionserror:nil];
NSLog(@"%@",[dicobjectForKey:@"info"]);
}];
5. 异步代理请求
用代理需要导入协议:NSURLConnectionDataDelegate
@interface ViewController ()<NSURLConnectionDataDelegate>
NSString *strURL = [NSString
stringWithFormat:@"http://192.168.10.252/upload/mapi/index.php?"];
NSMutableURLRequest *request = [NSMutableURLRequest
requestWithURL:[NSURL
URLWithString:strURL]];
[request setHTTPMethod:@"POST"];
NSString *body = [NSString
stringWithFormat:@"act=%@&r_type=%d&email=%@&password=%@&user_name=%@",@"register",1,@"12345@qq.com",@"123456",@"lisi"];
[request setHTTPBody:[body
dataUsingEncoding:NSUTF8StringEncoding]];
//使用代理的方式做网络请求
[NSURLConnection
connectionWithRequest:request
delegate:self];
#pragma mark -- 网络请求代理方法实现
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse
*)response;
{
NSLog(@"网络请求总数据量,这个只执行一次");
infoData = [[NSMutableData
alloc]init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"这个方法会执行多次!");
[infoData
appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"网络请求结束!!!");
NSDictionary *dict = [NSJSONSerialization
JSONObjectWithData:infoData
options:kNilOptions
error:nil];
NSLog(@"%@",dict);
NSLog(@"%@",dict[@"info"]);
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"网络请求失败。失败原因%@",error);
}
5. 使用第三方做网络请求,AFNetWorking2.0
头文件导入:
#import "AFNetworking.h"
/*
AFNetWorking2.0 post 出现code=-1016错误怎么解决?
2.0 的你可以修改他的一个文件就可以做到了。AFURLResponseSerialization.m
这个文件找到这句话self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", nil];
然后把 text/html 加进去就可以了!
*/
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager
alloc]init];
//设置请求超时
manager.requestSerializer.timeoutInterval =
10;
//请求参数配置
NSDictionary *dict =
@{@"act":@"register",@"r_type":@1,@"email":@"111222@qq.com",@"password":@"111111",@"user_name":@"wangwu1"};
// manager POST:nil parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
// //传文件的时候,会使用到这里
// } success:^(AFHTTPRequestOperation *operation, id responseObject) {
// <#code#>
// } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// <#code#>
// }
[manager POST:@"http://192.168.10.252/upload/mapi/index.php?"
parameters:dict success:^(AFHTTPRequestOperation *operation,
id responseObject) {
//网络请求成功了
NSLog(@"11====%@",responseObject);
NSDictionary *dic = responseObject;
NSLog(@"%@",[dic
objectForKey:@"info"]);
} failure:^(AFHTTPRequestOperation *operation,
NSError *error) {
//网络请求失败了。
NSLog(@"error == %@",error);
}];