欢迎您访问我爱IT技术网,今天小编为你分享的电脑教程是网络协议方面的经验知识教程:网络协议的使用,下面是详细的分享!
网络协议的使用
#import ViewController.h
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// [self loadData1];
// [self loadData2];
// [self loadData3];
// [self loadData4];
// [self loadData5];
[self loadData6];
}
//通过URl 获得URL里面的内容(字符串)
- (void)loadData1
{
//把字符串转化成 NSURL
NSURL *url=[NSURL URLWithString:@http://192.168.1.126:8080/cms4/login/doLogin.action];
NSString *content=[NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSLog(@%@,content);
}
//通过URl 获得URL里面的data
- (void)loadData2
{
NSURL *url=[NSURL URLWithString:@http://preview.quanjing.com/is_rm001/is0997q92.jpg];
NSData *data=[NSData dataWithContentsOfURL:url];
UIImageView *imageView=[[UIImageView alloc]initWithFrame:self.view.frame];
imageView.contentMode=UIViewContentModeScaleAspectFill;
imageView.image=[UIImage imageWithData :data];
[self.view addSubview:imageView];
}
//同步请求
- (void)loadData3
{
NSURL *url=[NSURL URLWithString:@http://preview.quanjing.com/is_rm001/is0997q92.jpg];
//实例化一个请求对象 里面携带请求地址
NSURLRequest *request=[NSURLRequest requestWithURL:url];
//data 是服务器返回的数据 NSURLConnection是发送请求的类
NSData *data=[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
UIImageView *imageView=[[UIImageView alloc]initWithFrame:self.view.frame];
imageView.contentMode=UIViewContentModeScaleAspectFill;
imageView.image=[UIImage imageWithData :data];
[self.view addSubview:imageView];
}
//异步请求
- (void)loadData4
{
UIImageView *imageView=[[UIImageView alloc]initWithFrame:self.view.frame];
imageView.contentMode=UIViewContentModeScaleAspectFill;
[self.view addSubview:imageView];
NSURL *url=[NSURL URLWithString:@http://preview.quanjing.com/is_rm001/is0997q92.jpg];
NSURLRequest *request=[NSURLRequest requestWithURL:url];
//需要通过链接 异步发送 请求
//线程
NSOperationQueue *queue=[[NSOperationQueue alloc]init];
//发送一个异步请求 在这个线程里面去执行
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
//response 服务器回应的内容(回应状态的code以及error)data 回应给客户端需要的数据
NSLog(@%@,response);
imageView.image=[UIImage imageWithData :data];
}];
}
//get 把传输的数据放在链接地址里面
- (void)loadData5
{
NSString *interfaceString=@http://apis.baidu.com/showapi_open_bus/mobile/find;
NSString *requestContentString=@num=18786084133;
NSString *uselString=[NSString stringWithFormat:@%@?%@,interfaceString,requestContentString];
//把链接地址的字符窜 转化成NSUTF8StringEncoding
NSURL *url=[NSURL URLWithString:[uselString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
//可变请求 可以添加请求方式以及请求的 请求头或者更多
//(NSTimeInterval)请求需要的时间 超过时间 就不在请求 cachePolicy缓存方式
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
//指定http的请求方式
request.HTTPMethod=@GET;
NSString *apiKey=@e7f5ac9e7c42a6c8cb125ee1d7e8779e;
//把apiKey 发送给服务器指定的请求头 位置;forHTTPHeaderField需要的KEY 是服务器指定的key
[request addValue:apiKey forHTTPHeaderField:@apikey];
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc]init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSLog(@%@,response);
// 解析json文件
// 把data转换成json文件
NSDictionary *info=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@%@ %@ %@,info[@showapi_res_body][@city],info[@showapi_res_body][@name],info[@showapi_res_body][@num]);
}];
}
//post请求
- (void)loadData6
{
NSURL *url=[NSURL URLWithString:@http://www.weihuok.com/customer2/GetService];
//请求的参数 PlatformType设备型号
NSDictionary *dic=@{@PlatformType:@3};
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
//设置HTTP的请求方式
request.HTTPMethod=@POST;
//设置请求的参数
//dataUsingEncoding把字符串转换成data
//HTTPBody要的是data数据
request.HTTPBody=[[NSString stringWithFormat:@%@,dic]dataUsingEncoding:NSUTF8StringEncoding];
//发送请求
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc]init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSDictionary *info=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@%@,info);
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
以上就是关于网络协议的使用的网络协议知识分享,更多电脑教程请移步到>>电脑教程。
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
