//
// 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
使用OC进行实现GET和POST请求
最新推荐文章于 2019-12-20 16:50:11 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)