IOS_简单POST请求

//
//  PJViewController.m
//  GET
//
//  Created by pj on 14-8-3.
//  Copyright (c) 2014年 pj. All rights reserved.
//

#import "PJViewController.h"

@interface PJViewController ()

@end

@implementation PJViewController

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

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)post
{
    NSString *urlString = [NSString stringWithFormat:@"http://www.baidu.com"];
    NSURL *url = [NSURL URLWithString:urlString];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"POST";
    request.HTTPBody = [@"a=2&b=2" dataUsingEncoding:NSUTF8StringEncoding];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"success%@%@",result,[NSThread currentThread]);
    }];
    NSLog(@"end");
}
- (void)get
{
    NSString *urlString = [NSString stringWithFormat:@"http://www.baidu.com/?a=2&b=2"];
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    // 在主线程上执行这条异步指令
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"success%@%@",result,[NSThread currentThread]);
    }];
    NSLog(@"end");
}

@end

 

posted @ 2014-08-03 16:41  宝贝,我永远都在  阅读(131)  评论(0)    收藏  举报