网络请求

//发起同步网络请求
-(IBAction)button1Touched
{
    NSURL *url=[NSURL URLWithString:@"http://www.163.com"];
    ASIHTTPRequest *request=[ASIHTTPRequest requestWithURL:url];
    
    [request startSynchronous]; //发起同步网络请求
    
    NSError *error=[request error];//获取错误代码
    
    if (error)
    {
        NSLog(@"%@",error);
    }
    else
    {
        NSString *response=[request responseString];//获取字符串形式的返回信息
        NSLog(@"%@",response);
    }
    
}

//发起异步网络请求
-(IBAction)button2Touched //发起请求
{
    NSURL *url=[NSURL URLWithString:@"http://www.163.com"];
    ASIHTTPRequest *request=[ASIHTTPRequest requestWithURL:url];
    request.delegate=self;
    [request startAsynchronous];
    
}

-(void)requestFinished:(ASIHTTPRequest *)request //通过代理获得返回值
{
    NSString *response=[request responseString];//获取字符串形式的返回信息
    NSLog(@"%@",response);
    
    
    /*
     
    NSData *response=[request responseData];//获取图片、影音形式的返回信息
     
    */
    
}

-(void)requestFailed:(ASIHTTPRequest *)request
{
    NSError *error=[request error];
    NSLog(@"%@",error);
}

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