IOS:OC--网络请求

1.ViewController.h

import "ViewController.h"

@interface ViewController ()
{
long long _totalLenght;
}

@property(nonatomic,strong)NSMutableData * receivedData;

@end

@implementation ViewController

  • (void)viewDidLoad {
    [super viewDidLoad];

}

  • (IBAction)GetButtonAction:(id)sender {
    //创建URL
    NSURL * url = [NSURL URLWithString:@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213"];
    //创建网络请求
    NSURLRequest * request = [[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
    //连接服务器
    NSData * recieved = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString * str = [[NSString alloc] initWithData:recieved encoding:NSUTF8StringEncoding];
    NSLog(@"%@",str);

}

  • (IBAction)PostButtonAction:(id)sender {
    //创建URL
    NSString * string =@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx";
    NSURL * url = [NSURL URLWithString:string];
    //创建请求
    NSString * postStr = @"date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213";
    //进行网络请求
    NSData * poatData = [postStr dataUsingEncoding:NSUTF8StringEncoding];
    NSMutableURLRequest * requedt =[[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
    //设置请求方式为post,默认Get
    [requedt setHTTPMethod:@"post"];
    //设置参数
    [requedt setHTTPBody:poatData];
    //进行连接服务器
    NSData * recieved = [NSURLConnection sendSynchronousRequest:requedt returningResponse:nil error:nil];
    //转换
    NSString * backstr = [[NSString alloc] initWithData:recieved encoding:NSUTF8StringEncoding];
    NSLog(@"%@",backstr);

}

  • (IBAction)DelegateButton:(id)sender {
    NSString * strUrl=@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213";
    NSURL * url = [NSURL URLWithString:strUrl];
    NSURLRequest * request = [NSURLRequest requestWithURL:url];
    //连接服务器
    [NSURLConnection connectionWithRequest:request delegate:self];
    //

}

pragma mark ---NSURLConnection 代理方法

//服务器开始响应,准备返回数据
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(nonnull NSURLResponse *)response{

//记录数据的总长度,但是也可能没有(由服务器决定)
_totalLenght = response.expectedContentLength;

//网络⬆️基于http协议时(url是以http开头),reponse本质就是NSHTTPURLResponse类型的
NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse *)response;
//创建空的可变的data,接收服务器的data片段
self.receivedData= [NSMutableData dataWithCapacity:40];

 //NSLog(@"---%s",__FUNCTION__);

}
//客户端接收数据
-(void)connection:(NSURLConnection *)connection didReceiveData:(nonnull NSData *)data{

 //NSLog(@"---%s",__FUNCTION__);
[_receivedData appendData:data];

}
//数据接收成功,网络请求成功:
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"---%s",FUNCTION);
//开始处理数据
NSString * result = [[NSString alloc] initWithData:_receivedData encoding:NSUTF8StringEncoding];
NSLog(@"result =%@",result);

}

//网络请求失败:
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"---%s",FUNCTION);
}

  • (IBAction)BlokButton:(id)sender {
    NSString* string = @"http://blog.csdn.net/dianziagen/article/details/70185911";
    NSURL * url = [NSURL URLWithString:string];
    NSURLRequest * request = [NSURLRequest requestWithURL:url];
    //建立连接
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {

      //DAta是服务器返回的完整数据
      //数据处理
      NSString * result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
      NSLog(@"%@",result);
    

    }];

}

你可能感兴趣的:(IOS:OC--网络请求)