iOS-Http : GET : POST
来源:互联网 发布:ubuntu jdk下载 编辑:程序博客网 时间:2024/06/05 13:22
一.概述
* HTTP/1.1协议共定义了8中请求方法:OPTIONS, HEAD, GET, POST, PUT, DELETE, TRACE, CONNECT.
* GET方法和POST是我们使用最频繁的网络请求方法。
* GET和POST在应用场合有什么区别呢?
* GET方法向指定资源发出请求,发送的消息显示的跟在URL后面,用户信息不安全,并且传送信息量有限。(如下所示,在请求中能看到用户名和密码)
http://localhost:8080/logandreg/logreg?name=wyg&pwd=1992
* 如果仅仅是向服务器索要数据,没有参数,使用GET比较方便。(如下所示)
http://www.baidu.com
* POST传送的信息量大,并且传送的信息是被隐藏的,传送信息比较安全,如果向服务器传送数据,建议使用POST.
二.GET请求网络数据(同步,异步)
* 如上所述,GET方法可以向指定资源发出请求,比如我们想再网络上请求一张图片在本地上显示,使用GET方法就非常的方便。
* GET请求分为同步请求和异步请求,一般情况下,为了良好的用户体验,我们都使用异步请求。
* GET同步请求一张网络图片(代码折叠)
同步获取网络图片
*GET异步请求一张网络图片(iOS5.0)(代码折叠):
异步获取网络图片
*GET异步请求(iOS2.0)(代码折叠)
异步请求网络图片
三.POST请求网络数据(以异步为例,同步的与GET类似)
* 传输信息安全性比GET高。
* 传输信息量比GET大。
* 代码中带有详细解释,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
- (
IBAction
)postRequest:(
id
)sender
{
//明确请求的url
NSURL
*url = [
NSURL
URLWithString:@
"http://localhost:8080/logandreg/logreg"
];
//创建请求(可变请求)
NSMutableURLRequest
*request = [
NSMutableURLRequest
requestWithURL:url];
//指定请求方式
[request setHTTPMethod:@
"POST"
];
//拼接参数内容
NSString
*body = @
"name=wyg&pwd=1992"
;
//请求数据放到请求的请求体中
[request setHTTPBody:[body dataUsingEncoding:
NSUTF8StringEncoding
]];
//使用post发起异步请求
[
NSURLConnection
connectionWithRequest:request delegate:
self
];
}
- (
void
)connection:(
NSURLConnection
*)connection didReceiveResponse:(
NSURLResponse
*)response
{
//接收到响应之后响应的方法
_buffer = [[
NSMutableData
alloc]init];
}
- (
void
)connection:(
NSURLConnection
*)connection didReceiveData:(
NSData
*)data
{
//接收到数据之后响应的方法
[_buffer appendData:data];
}
- (
void
)connectionDidFinishLoading:(
NSURLConnection
*)connection
{
//数据处理完成之后响应的方法
NSString
*str = [[
NSString
alloc]initWithData:_buffer encoding:
NSUTF8StringEncoding
];
NSLog
(@
"%@"
,str);
}
- (
void
)connection:(
NSURLConnection
*)connection didFailWithError:(
NSError
*)error
{
//请求出错之后响应的方法(如断网,超时)
}
0 0
- iOS-Http : GET : POST
- iOS http get和post
- iOS http get和post
- iOS http访问post 、get
- iOS http get和post(2)
- iOS http get和post(3)
- IOS开发 HTTP相关GET/POST请求
- ios发送http GET和POST请求
- iOS HTTP协议的get,post方法
- iOS http get和post(2)
- iOS http get和post(3)
- IOS HTTP GET POST学习笔记
- iOS原生Http请求,get post 异步
- Get/Post in http
- http get post head
- http GET POST头
- Http Get Post 区别
- HTTP POST GET详解
- Spring DI via constructor
- 二叉树三种遍历的递归和迭代解法
- *LeetCode-Implement Stack using Queues
- 关于Cgroup的简单测试
- Constructor injection type ambiguities in Spring
- iOS-Http : GET : POST
- C# 之屏幕找图
- Spring事务的隔离级别
- HashMap的key可以是可变的对象吗???
- Java + myBatis + MySql
- 程序上面,一般不使用 悲观锁
- Rearrange int array to put odd number to left and even to right
- iOS疯狂详解之SQlite数据库增删改查
- Citirx reciever 客户端添加帐户