POST异步请求(代理)

1、遵循<NSURLConnectionDataDelegate>

@interface ViewController ()<NSURLConnectionDataDelegate>

2、NSMutableData类型的reData属性是用来拼接数据的

@property (nonatomic,strong)NSMutableData *reDtata;

3、获取url

 NSString *urlString = @"http://api.tudou.com/v3/gw";
NSURL *url = [NSURL URLWithString:urlString];

4、创建request请求

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

5、设置HTTPMethod为POST请求(默认为GET请求)

request.HTTPMethod = @"POST";

6、设置HTTPBody(url中的body部分,如果body部分含有中文需要转化)

 NSString *bodyStr = @"method=album.channel.get&appKey=myKey&format=json&channel=c&pageNo=1&pageSize=15";
NSData *bodyData = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
request.HTTPBody = bodyData;

7、创建连接并设置代理

  [NSURLConnection connectionWithRequest:request delegate:self];

8、实现代理方法

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
self.reDtata = [NSMutableData data];
} - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[_reDtata appendData:data];
} - (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:_reDtata options:(NSJSONReadingAllowFragments) error:nil];
NSLog(@"%@",dic);
} - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{ }

下面是实现的所有代码

- (IBAction)postAsyc:(id)sender{}是从storyboard里面拖出来的控件代码,也可以直接写代码实现,写一个button和它的实现方法即可。
#import "ViewController.h"

@interface ViewController ()<NSURLConnectionDataDelegate>
@property (nonatomic,strong)NSMutableData *reDtata;
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)postAsyc:(id)sender {
NSString *urlString = @"http://api.tudou.com/v3/gw";
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST";
NSString *bodyStr = @"method=album.channel.get&appKey=myKey&format=json&channel=c&pageNo=1&pageSize=15";
NSData *bodyData = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
request.HTTPBody = bodyData;
[NSURLConnection connectionWithRequest:request delegate:self]; } - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
self.reDtata = [NSMutableData data];
} - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[_reDtata appendData:data];
} - (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:_reDtata options:(NSJSONReadingAllowFragments) error:nil];
NSLog(@"%@",dic);
} - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{ } - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

iOS中POST异步请求的更多相关文章

  1. iOS中发送HTTP请求的方案

    在iOS中,常见的发送HTTP请求的方案有 苹果原生(自带) NSURLConnection:用法简单,最古老最经典的一种方案 NSURLSession:功能比NSURLConnection更加强大, ...

  2. iOS NSURLConnection POST异步请求封装,支持转码GBK,HTTPS等

    .h文件 #import <Foundation/Foundation.h> //成功的回调 typedef void(^successBlock)(id responseObj); // ...

  3. Springmvc中 同步/异步请求参数的传递以及数据的返回

    转载:http://blog.csdn.net/qh_java/article/details/44802287 注意: 这里的返回就是返回到jsp页面 **** controller接收前台数据的方 ...

  4. Jquery中Ajax异步请求中的async参数的作用

    之前不知道这个参数的作用,上网找了前辈的博客,在此收录到自己的博客,希望能帮到更多的朋友: test.html <a href="javascript:void(0)" on ...

  5. springmvc中同步/异步请求参数的传递以及数据的返回

    注意: 这里的返回就是返回到jsp页面 **** controller接收前台数据的方式,以及将处理后的model 传向前台***** 1.前台传递数据的接受:传的属性名和javabean的属性相同 ...

  6. for循环中嵌套异步请求问题

    for循环中嵌套了异步请求会导致顺序错乱,用递归代替for循环,可以保证正常执行顺序:

  7. CAT中实现异步请求的调用链查看

    CAT简介 CAT(Central Application Tracking),是美团点评基于 Java 开发的一套开源的分布式实时监控系统.美团点评基础架构部希望在基础存储.高性能通信.大规模在线访 ...

  8. iOS中的网络请求 和 网络监测

    1.网络监测 //根据主机名判断网络是否连接 Reachability *reach = [Reachability reachabilityWithHostName:@"www.baidu ...

  9. IOS中UITableView异步加载图片的实现

    本文转载至 http://blog.csdn.net/enuola/article/details/8639404  最近做一个项目,需要用到UITableView异步加载图片的例子,看到网上有一个E ...

随机推荐

  1. Composer设置忽略版本匹配的方法

    Composer简介 Composer 是 PHP 的一个依赖管理工具.它允许你申明项目所依赖的代码库,它会在你的项目中为你安装他们.Composer 不是一个包管理器.是的,它涉及 "pa ...

  2. Java 大数运算

    import java.util.*; import java.math.*; public class Main{ public static void main(String args[]){ S ...

  3. 【转】贾扬清:希望Caffe成为深度学习领域的Hadoop

    [转:http://www.csdn.net/article/2015-07-07/2825150] 在深度学习(Deep Learning)的热潮下,Caffe作为一个高效.实用的深度学习框架受到了 ...

  4. 在linux下python爬虫进程发生异常时自动重启直至正常结束的方法

    之前在做爬虫的时候遇到一种情况,当网络情况不太好的时候,爬虫爬到的链接在urlopen时会因为无法正常连接而报URLError或者timeout的错误导致陈序报错而终止:但是这些错误在重新运行陈序后能 ...

  5. [ActionScript 3.0] AS3.0 Socket通信实例

    以下类是充当Socket服务器的例子 package { import flash.display.Sprite; import flash.events.Event; import flash.ev ...

  6. Uart的Verilog建模

    开发工具:Quartus II 9.1: 仿真软件:Questa Sim 10.0c: 硬件平台:Terasic DE2-115(EP2C35F672C6): 外设:MAX3232: 3个工程文件:& ...

  7. 学习Python的第一课(简单的单元测试)

    由于有C#开发基础,感觉学习Python应该不难,主要是一些语法了,再加上现在互联网这么发达. 感觉还是要有思路,否则学什么也只能是什么. 话不多说,简单发下这几天的学习成果吧: 第一次写博客,大家不 ...

  8. 树莓派B+上手小记--使用HDMI线连接显示器

    入手还算比较顺利,一开始使用网上下的别人精简的OS,发现ACT及PWR灯一直亮着,上网查说用HDMI连接显示器需要修改配置文件config.txt,但修改后情况依旧. 如果还是用官方的系统试试吧,上网 ...

  9. 【转载】Fast Inserts to PostgreSQL with JDBC and COPY FROM

    source: http://rostislav-matl.blogspot.com/2011/08/fast-inserts-to-postgresql-with-jdbc.html Thanks ...

  10. Attribute "resource" must be declared for element type "mapper".

    今天在玩mybatis的时候,遇到这个奇葩问题. 最后发现,原因是 dtd文件配置错误了.错把Mapper的直接copy过来 把DOCTYPE mapper改成configuration,Mapper ...