NSURLSession(二)POST請求,nsurlsessionpost

來源:互聯網
上載者:User

NSURLSession(二)POST請求,nsurlsessionpost
    //1.構造URL
    NSURL *url = [NSURL URLWithString:@"https://api.weibo.com/2/statuses/update.json"];
    
    //2.構造Request
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    
    //(1)設定為POST請求
    [request setHTTPMethod:@"POST"];
    
    //(2)逾時
    [request setTimeoutInterval:60];
    
    //(3)佈建要求頭
    //[request setAllHTTPHeaderFields:nil];
    
    //(4)佈建要求體
    //發新浪微博
    //請求體裡需要包含至少兩個參數
    //指定使用者的令牌   微博本文
    //access_token    status
    //這裡的 access_token 大家可以用自己的微博來測試 access_token->是通過自己的微博帳號密碼產生的 具體流程可以參照 http://www.cnblogs.com/ok-lanyan/archive/2012/07/15/2592070.html
    NSString *bodyStr = @"access_token=xxxxx&status=微博內容";

    NSData *bodyData = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];

     //佈建要求體

    [request setHTTPBody:bodyData];
    
    
    
    //3.構造Session
    NSURLSession *session = [NSURLSession sharedSession];
    
    //4.task
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        NSLog(@"response : %@", response);
    }];
    
    //5.
    [task resume];

   


   話不多說,大家去測試一下吧,通過自己編碼的程式就輕輕鬆鬆的發出微博了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.