您的位置:首页 > 理论基础 > 计算机网络

HTTPRequest 网络请求之get请求和post请求

2015-11-17 15:59 447 查看
1.网络请求方式:Get和Post

相同点:Get请求通常用于获取数据,Post请求通常用于提交数据. Get也可以提交数据,Post也可以获取数据

不同点:1.请求的格式不一样: get请求接口地址和参数之间?连接,参数之间&连接,每个参数的键和值=号链接

2.安全程度不一样: get请求可以直接看到请求的参数,post请求则看不到

2.同步链接和异步链接

同步链接:请求服务器的过程在主线程完成,容易导致UI假死
异步链接:请求的过程在子线程完成,不会导致UI假死

3.废话少说了,下面就上代码了啊,哈哈

3.1.用Xib 或者storyBoard拖拽四个button空间,分别为button的title写上"异步Get","异步Post","同步Get","同步Post"

3.2.异步Get的button关联的方法代码如下:

- (void)asynchronizeGet:(UIButton *) btn {
//不要缺http://
//否则会导致失败
NSString * url = @"http://api.map.baidu.com/place/v2/search?q=银行®ion=郑州&output=json&ak=6E823f587c95f0148c19993539b99295";
//把原地址中的汉字进行URLEncode编码
NSString * newUrl = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//1.把字符串转成URL对象
NSURL * URL = [NSURL URLWithString:newUrl];
//2.根据URL对象生成请求
//    NSURLRequest * request = [NSURLRequest requestWithURL:URL];
NSURLRequest * request = [NSURLRequest requestWithURL:URL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:3.0];
//3.和服务器建立异步链接
[NSURLConnection connectionWithRequest:request delegate:self];

}
注意:1.大家有没有注意到
[NSURLConnection connectionWithRequest:request delegate:self];
这段代码,NSURLConnection的delegate设为当前对象,可是我们前前后后在当前对象的.h或.m中没有发现当前类中接受这个delegate.所以,这个delegate是不需要我们手动进行添加的,但是delegate方法是必须要实现的!

2.还需要声明两个属性

@property (nonatomic, retain)NSMutableData * data;
@property (nonatomic, retain)NSURLConnection * connection;
3.3. 异步Post的button关联的方法代码如下:

//异步post
- (void)asynchronizePost:(UIButton *) btn {
NSString * urlStr = @"http://api.tudou.com/v3/gw";
//把字符串转成URL对象
NSURL * URL = [NSURL URLWithString:urlStr];
//根据URL生成请求对象
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:URL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:3.0];
//设置请求的方式
[request setHTTPMethod:@"POST"];
//参数字符串,实际工作中需要根据接口文档自己去拼接
NSString * paramsString = @"method=user.item.get&appKey=43db911a75b88c11&format=json&user=ttr2008&pageNo=1&pageSize=10";
//把字符串转成二进制数据
NSData * paramData = [paramsString dataUsingEncoding:NSUTF8StringEncoding];
//把参数放到请求体中
[request setHTTPBody:paramData];
//和服务器建立链接
[NSURLConnection connectionWithRequest:request delegate:self];
}
3.4. 同步Get的button关联的方法代码如下

//同步get
- (void)synchronizeGet: (UIButton *)btn {
NSLog(@"下载图片...");
NSString * imageUrl = @"http://c.hiphotos.baidu.com/image/pic/item/bd315c6034a85edfdf82833d4a540923dd5475a5.jpg";
NSURL * url = [NSURL URLWithString:imageUrl];
NSURLRequest * request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:3.0];
//发送同步请求, 任务在主线程完成,数据不下载完,不会往下执行,会导致UI假死
NSData * data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
UIImage * image = [UIImage imageWithData:data];
UIImageView * imageView = (UIImageView *)[self.view viewWithTag:100];
imageView.image = image;

}
3.5. 同步Post的button关联的方法代码自己脑补吧...

4. delegate方法如下

1.//收到服务器响应
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(@"didReceiveResponse");
self.data = [NSMutableData data];
}
2.//接收服务器传回的数据
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSLog(@"didReceiveData");
[self.data appendData:data];
//    NSLog(@"%@",_data);
}
3.//接口请求完成
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"connectionDidFinishLoading");
if (connection == _connection) {
UIImage * image = [UIImage imageWithData:_data];
UIImageView * imageView = (UIImageView *)[self.view viewWithTag:100];
imageView.image = image;

}else {
//把data转成json字符串
NSString * jsonString = [[NSString alloc] initWithData:self.data encoding:NSUTF8StringEncoding];
NSLog(@"%@",jsonString);

NSLog(@"data length = %ld", self.data.length);
NSError * error = nil;
NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:self.data options:0 error:&error];
if (error) {
NSLog(@"error = %@",error);
}
NSLog(@"%@", dic);
}
}
4.//请求失败
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"didFailWithError");
NSLog(@"%@", error);
}
注意:我在运行的时候是打的有log的,有时网速不太好可以看出第二步不仅仅只是执行了一次,所以,我们在第一步中把接受data的这个属性给alloc出来了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: