GET/POST请求

NSLog(@"get同步!");
   
   
 NSString *urlStr = @"http://api.map.baidu.com/place/v2/search?query=银行®ion=大连&output=json&ak=6E823f587c95f0148c19993539b99295";
   
   
 //对中文进行编码
   
 NSString *urlStrEcode = [urlStrstringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
   
   
 //1.创建url地址
   
 NSURL *url = [NSURLURLWithString: urlStrEcode];
   
   
 //2.创建请求对象
   
 NSMutableURLRequest*request = [NSMutableURLRequestrequestWithURL:url];
   
   
 //创建服务器响应信息对象
   
 NSURLResponse*response =nil;
   
   
 //创建错误信息对象
   
 NSError *error = nil;
   
   
 //3.向服务器发送请求
   
 NSData *data =[NSURLConnectionsendSynchronousRequest:requestreturningResponse:&responseerror:&error];
   
   
 NSMutableDictionary*dic =[NSJSONSerializationJSONObjectWithData:dataoptions:NSJSONReadingMutableContainerserror:nil];
   
 NSLog(@"dic = %@", dic);
   
 NSLog(@"response = %@", response);
   
 NSLog(@"----------------------------------");
    NSLog(@"error = %@", error);

NSLog(@"post同步!");
   
 NSString *str = @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx";
   
   
 //post方法是地址参数分离的
   
 NSURL *url = [NSURLURLWithString:str];
   
 NSMutableURLRequest*request = [NSMutableURLRequestrequestWithURL:url];
   
   
 //设置请求方式为post
    [request
 setHTTPMethod:@"POST"];
   
   
 //设置参数转化成data
   
 NSString *bodyStr =@"date=20131129&startRecord=5&len=5&udid=1234567890&terminalType=Iphone&cid=215";
   
   
 NSData *bodyData = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
   
   
 //设置请求对象为body
    [request
 setHTTPBody:bodyData];
   
   
 NSURLResponse*response =nil;
   
 NSError *error = nil;
   
   
 //向服务器发送请求
   
 NSData *data = [NSURLConnectionsendSynchronousRequest:requestreturningResponse:&responseerror:&error];
   
   
 NSMutableDictionary*dic = [NSJSONSerializationJSONObjectWithData:dataoptions:NSJSONReadingMutableContainerserror:nil];
   
 NSLog(@"dic = %@", dic);
    NSLog(@"response = %@", response);

NSLog(@"get异步!");
   
 //NSString *str = @"http://project.lanou3g.com/teacher/yihuiyun/phpJSON.php";
   
   
 NSString *str =@"http://img4.duitang.com/uploads/item/201207/28/20120728105310_jvAjW.thumb.600_0.jpeg";
   
 NSURL *url = [NSURLURLWithString:str];
   
   
 NSMutableURLRequest*request = [NSMutableURLRequestrequestWithURL:url];
   
#pragma mark 设置异步请求代理(get异步)
    [NSURLConnectionconnectionWithRequest:requestdelegate:self];

#pragma mark --收到服务器响应(get异步)
- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response
{
   
 //初始化data属性
   
 self.data= [NSMutableDatadata];
}

#pragma mark --接收数据(get异步)
- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
{
   
 //接受data对象
    [
self.dataappendData:data];
}

#pragma mark --数据请求完成(get异步)
- (void)connectionDidFinishLoading:(NSURLConnection*)connection
{
//    NSMutableArray *array = [NSJSONSerialization JSONObjectWithData:self.data options:NSJSONReadingMutableContainers error:nil];
//    NSLog(@"array = %@", array);
   
   
   
 //显示图片
   
 self.imageview.image= [UIImageimageWithData:self.data];
   
}

#pragma mark --数据请求失败(get异步)
- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
{
   
 NSLog(@"error = %@", error);
   
 NSLog(@"connection = %@", connection);
}


#pragma mark -- post异步
- (void)buttonAction3:(UIButton*)button
{
   
 NSLog(@"post异步!");
   
   
 NSString *str = @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx";
   
 NSURL *url = [NSURLURLWithString:str];
   
   
 NSMutableURLRequest*request = [NSMutableURLRequestrequestWithURL:url];
   
    [request
 setHTTPMethod:@"POST"];
   
   
 NSString *bodyStr =@"date=20131129&startRecord=5&len=5&udid=1234567890&terminalType=Iphone&cid=215";
   
   
 NSData *data = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
   
    [request
 setHTTPBody:data];
   
   
   
 //post 异步请求 block
   
 //参数1.request对象
   
 //参数2.多线程队列NSOperationQueue对象
    [
NSURLConnectionsendAsynchronousRequest:requestqueue:[NSOperationQueuemainQueue]completionHandler:^(NSURLResponse*response,NSData *data,NSError*connectionError){
       
#pragma mark --遇到block一定要会调回来的参数就是你需要的数据
       
       
 NSMutableDictionary*dic = [NSJSONSerializationJSONObjectWithData:dataoptions:NSJSONReadingMutableContainerserror:nil];
       
 NSLog(@"dic = %@", dic);
    }];
}

你可能感兴趣的:(iOS)