使用OC进行实现GET和POST请求

//
//  ViewController.m
//  OC-13_02
//
//  Created by Ibokan on 15/12/28.
//  Copyright © 2015年 ibokan. All rights reserved.
//

#import "ViewController.h"
#import "WeiboModel.h"

@interface ViewController ()<NSURLConnectionDataDelegate>
{
    NSMutableData *mData;
    NSURLConnection *connentGet;
    NSMutableData *mPostData;
    NSURLConnection *connetionPost;

}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
}

//同步post
- (IBAction)synPost:(id)sender {
    NSString *urlString = @"https://api.weibo.com/2/statuses/update.json";
    //编码
    urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    //转成NSURL
    NSURL *url = [NSURL URLWithString:urlString];
    //在OC中使用NSMutableURLRequest进行post请求
    NSMutableURLRequest *mRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
    
    NSString*bodyString = @"status=你好&access_token=2.00IujYFE8Evp8C32ce30bc02uNGpHE";
    
    NSData *data = [bodyString dataUsingEncoding:4];
    //设置方法体
    [mRequest setHTTPMethod:@"post"];
    [mRequest setHTTPBody:data];
    
    NSData *resultData = [NSURLConnection sendSynchronousRequest:mRequest returningResponse:nil error:nil];
    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:resultData options:NSJSONReadingAllowFragments error:nil];
    NSLog(@"dictionary = %@",dictionary);
    
}
//异步post
- (IBAction)asynPost:(id)sender {
    
    NSString*urlString = @"https://api.weibo.com/2/statuses/update.json";
    urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSURL *url = [NSURL URLWithString:urlString];
    
    //在OC中使用NSMutableURLRequest发送post请求
    NSMutableURLRequest *mAsynRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
    
    NSString *bodyString = @"sta
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值