get和post请求数据
来源:互联网 发布:sql从入门到精通的书籍 编辑:程序博客网 时间:2024/06/05 09:24
今天简单介绍一下系统自带的网络数据请求方法,分为同步和异步的get和post。虽然现在程序猿们都用主流的数据请求工具AFN或者ASI,但是相对于一些简单的数据还是用系统的方法比较方法,至少不用导入相关的库。
一、先将一下同步请求数据(包括get和post)
1)get (对于那种后面直接带参数的网络地址用get请求)
NSString *str = @"http://....?name=张三&age=20";//参数为name和age
NSString *strEcode = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];// 对中文进行编码
NSURL *url = [NSURL URLWithString:strEcode];// 创建URL地址
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];// 创建可变请求对象
NSURLResponse *response = nil;// 将对象发给服务器
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data option:0 error:nil];// 如果 请求数据返回是一个字典,就用字典接受。是json数据格式,故json解析方法解析。(这里先不讨论xml数据格式)
2)post (参数没有直接写在地址里,而是封装在一个body体里。优点:相对于get比较安全)
NSString *str = @"http://.... .ashx"; // 这里没有参数
NSURL *url = [NSURL URLWithString:str];// 创建URL地址
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];// 创建可变请求对象
[request setHTTPMethod:@"POST"];// 设置请求方式为post
NSString *bodyStr = @"name=张三&age=20";// 设置参数
NSData *data = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];// 将参数转为data型
[resquest setHTTPBody:data];// 设置请求对象body体
NSURLResponse *response = nil;// 将对象发给服务器
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
二、异步请求
1)get
NSString *str = @"http://....?name=张三&age=20";//参数为name和age
NSString *strEcode = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; // 对中文进行编码
NSURL *url = [NSURL URLWithString:strEcode];// 创建URL地址
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicytimeoutInterval:30.f];// 创建可变请求对象
[request setHTTPMethod:@"GET"];// 设置请求方式
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
id objects = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
// objects 即为返回的数据
}];
2) post
NSString *str = @"http://.... .ashx";// 这里没有参数
NSURL *url = [NSURL URLWithString:str];// 创建URL地址
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];// 创建可变请求对象
[request setHTTPMethod:@"POST"];// 设置请求方式为post
NSString *bodyStr = @"name=张三&age=20";// 设置参数
NSData *data = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];// 将参数转为data型
[resquest setHTTPBody:data]; // 设置请求对象body体
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
id objects = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
// objects 即为返回的数据
}];
注:由于xCode出了问题,所以代码只能纯手打,若有差错,望见谅。
- get和post请求数据
- get 和post 方式请求数据
- iOS中GET 和 POST 数据请求
- HttpClient的get和post请求数据
- java GET和POST请求返回数据
- get和post模拟请求数据
- Okhttp的post和get请求数据
- 数据请求get post
- Volley请求数据--Get请求和Post请求
- GET和POST请求
- Get和Post请求
- post和get请求
- Get和Post请求
- post和get请求
- get和post请求
- get和post请求
- post和get请求
- get和post请求
- mapreduce来清洗数据生成RCFile
- UVa 1611 - Crane(构造法)
- hdu 4535 吉哥系列故事——礼尚往来
- iframe 的缓存文件及解决方法以及 jquery deffered.then() 方法的使用
- NFS挂载中遇到的 svc: failed to register lockdv1 RPC service (errno 111).(connection refused)
- get和post请求数据
- 关于 信息熵/信息量/信息论/信息增益
- DDR1时钟的问题
- 【连接网络】Raspberry B+上手教程--自己梳理,亲测有效
- bzoj1430(B站AC第九题?还没交。。。)
- PHP: forbidden you dont have permission to access 问题
- 非root不能gdb attach的限制
- 【超级用户】Raspberry B+上手教程--自己梳理,亲测有效
- 网址参考