网络请求之GET、POST请求
来源:互联网 发布:淘宝不能登陆 编辑:程序博客网 时间:2024/05/21 04:16
网络请求-GET请求:
1,NSURL:请求地址;
2,NSURLRequest :一个NSURLRequest对象就代表一个请求,它包含的信息有:
1)一个NSURL对象
GET请求,不需要写请求头、请求体,只要告诉请求路径和请求参数就可以了。
2)请求方法
3)请求超时
3,NSMutableURLRequest : NSURLRequest的子类
4,NSURLConnection:
负责发送请求,建立客户端和服务器的连接;
NSURLConnection的使用步骤
1,创建一个NSURL对象,设置请求路径
// URL里面不能包含中文
NSURL *url = [NSURL URLWithString:urlStr];
2,传入NSURL创建一个NSURLRequest对象,设置请求头和请求体
// 2.2.创建请求对象
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; //默认就是GET请求
request.timeoutInterval = 5; //设置请求超时
3,使用NSURLConnection发送NSURLRequest
NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:self];
NSURLConnection发送请求
1,同步请求
[NSURLConnection sendSynchronousRequest:<#(NSURLRequest *)#> returningResponse:<#(NSURLResponse *__autoreleasing *)#> error:<#(NSError *__autoreleasing *)#>];
2,异步请求 : 根据对服务器返回数据的处理方式的不同,又可以分为2种
1)block回调:
[NSURLConnection sendAsynchronousRequest:<#(NSURLRequest *)#> queue:<#(NSOperationQueue *)#> completionHandler:<#^(NSURLResponse *response, NSData *data, NSError *connectionError)handler#>];
2)代理:
NSURLConnection *conn1 = [[NSURLConnection alloc] initWithRequest:<#(NSURLRequest *)#> delegate:<#(id)#>];
NSURLConnection *conn1 = [[NSURLConnection alloc] initWithRequest:<#(NSURLRequest *)#> delegate:<#(id)#> startImmediately:<#(BOOL)#>];
NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:self];
在这种情况下,需要调用start方法开始发送请求
- (void)start;
成为NSURLConnection的代理,最好遵守NSURLConnectionDataDelegate协议;
POST请求:
1,设置POST请求
request.HTTPMethod = @"POST"; //设置为POST请求
2,设置请求路径
// 设置请求路径
NSURL *url = [NSURL URLWithString:@"http://192.168.1.200:8080/MJServer/login"];
3,设置请求头
// 通过请求头告诉服务器客户端的类型
[request setValue:@"ios" forHTTPHeaderField:@"User-Agent"];
4,设置请求体
// 设置请求体
NSString *param = [NSString stringWithFormat:@"username=%@&pwd=%@", username, pwd];
request.HTTPBody = [param dataUsingEncoding:NSUTF8StringEncoding];
GET和POST请求
1,GET请求在路径后面要加请求参数,POST请求在路径后面不用加请求参数;
2,GET请求不用请求头和请求体,POST请求要请求体(把请求参数转换为请求体),需要设置HTTPMethod和HTTPBody;
注意: url中不能写中文,如果非有中文,就需要转码:
[url stringByAppendingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
JSON:
什么是JSON:
1,json是一种轻量级的数据格式,一般用于数据交互;
2,服务器返回给客户端的数据,一般都是JSON格式或者XML格式(文件下载除外)
JSON的格式很像OC中的字典和数组
{“name” : "jack", "age" : 10}
{"names" : ["jack","rose","jim"]}
标准JSON格式的注意点:key必须用双引号
JSON解析方案:
1,在IOS中,JSON的常见解析方式有4种
第三方框架: JSONKit、 SBJson、 TouchJSON (性能从左到右,越差);
苹果原生(自带):NSJSONSerialization (性能最好);
2,NSJSONSerialization的常见方法
JSON数据 -> OC对象
[NSJSONSerialization JSONObjectWithData:<#(NSData *)#> options:<#(NSJSONReadingOptions)#> error:<#(NSError *__autoreleasing *)#>];
OC对象 -> JSON数据
[NSJSONSerialization dataWithJSONObject:<#(id)#> options:<#(NSJSONWritingOptions)#> error:<#(NSError *__autoreleasing *)#>];
- 网络请求之GET、POST请求
- HTTPRequest 网络请求之get请求和post请求
- 网络请求(get&&post)
- 网络请求(get Post)
- 简单网络请求/GET请求/POST请求
- android网络请求之post和get请求方式
- IOS开发—网络请求之GET/POST同步请求
- iOS开发:网络请求之get、post同步异步请求
- Android okHttp网络请求之Get/Post请求
- Android okHttp网络请求之Get/Post请求
- Android okHttp网络请求之Get/Post请求
- HttpUtils网络请求框架之Get和Post请求
- Android okHttp网络请求之Get/Post请求
- Android okHttp网络请求之Get/Post请求
- 网络请求,get和post
- Get和post网络请求
- volley网络请求,get/post
- 网络请求方式 POST + GET
- java 值传递 引用传递的理解 言简意赅 一字千金
- HDU 2818 Building Block (并查集)
- ViewPager实现无限循环切换和手势滑动效果
- QWidget: must construct a QApplication before a QPaintDevice问题解决方法
- 基础问答
- 网络请求之GET、POST请求
- leetcode 061 —— Rotate List
- Android之——自定义复合控件的实现
- 安装tomcat服务
- JSVM使用简介
- 自定义PUSH POP跳转动画
- 【C/C++学院】0813-C与CPP不同以及命名空间简介/函数重载与函数默认参数/泛型auto/Newdelete
- Spring MVC学习--------------请求映射到action的方法
- 存储引擎浅谈