IOS:OC--網(wǎng)絡(luò)請求

1.ViewController.h

import "ViewController.h"

@interface ViewController ()
{
long long _totalLenght;
}

@property(nonatomic,strong)NSMutableData * receivedData;

@end

@implementation ViewController

  • (void)viewDidLoad {
    [super viewDidLoad];

}

}

  • (IBAction)PostButtonAction:(id)sender {
    //創(chuàng)建URL
    NSString * string =@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx";
    NSURL * url = [NSURL URLWithString:string];
    //創(chuàng)建請求
    NSString * postStr = @"date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213";
    //進(jìn)行網(wǎng)絡(luò)請求
    NSData * poatData = [postStr dataUsingEncoding:NSUTF8StringEncoding];
    NSMutableURLRequest * requedt =[[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
    //設(shè)置請求方式為post,默認(rèn)Get
    [requedt setHTTPMethod:@"post"];
    //設(shè)置參數(shù)
    [requedt setHTTPBody:poatData];
    //進(jìn)行連接服務(wù)器
    NSData * recieved = [NSURLConnection sendSynchronousRequest:requedt returningResponse:nil error:nil];
    //轉(zhuǎn)換
    NSString * backstr = [[NSString alloc] initWithData:recieved encoding:NSUTF8StringEncoding];
    NSLog(@"%@",backstr);

}

}

pragma mark ---NSURLConnection 代理方法

//服務(wù)器開始響應(yīng),準(zhǔn)備返回?cái)?shù)據(jù)
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(nonnull NSURLResponse *)response{

//記錄數(shù)據(jù)的總長度,但是也可能沒有(由服務(wù)器決定)
_totalLenght = response.expectedContentLength;

//網(wǎng)絡(luò)??基于http協(xié)議時(shí)(url是以http開頭),reponse本質(zhì)就是NSHTTPURLResponse類型的
NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse *)response;
//創(chuàng)建空的可變的data,接收服務(wù)器的data片段
self.receivedData= [NSMutableData dataWithCapacity:40];

 //NSLog(@"---%s",__FUNCTION__);

}
//客戶端接收數(shù)據(jù)
-(void)connection:(NSURLConnection *)connection didReceiveData:(nonnull NSData *)data{

 //NSLog(@"---%s",__FUNCTION__);
[_receivedData appendData:data];

}
//數(shù)據(jù)接收成功,網(wǎng)絡(luò)請求成功:
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"---%s",FUNCTION);
//開始處理數(shù)據(jù)
NSString * result = [[NSString alloc] initWithData:_receivedData encoding:NSUTF8StringEncoding];
NSLog(@"result =%@",result);

}

//網(wǎng)絡(luò)請求失敗:
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"---%s",FUNCTION);
}

  • (IBAction)BlokButton:(id)sender {
    NSString* string = @"http://blog.csdn.net/dianziagen/article/details/70185911";
    NSURL * url = [NSURL URLWithString:string];
    NSURLRequest * request = [NSURLRequest requestWithURL:url];
    //建立連接
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {

      //DAta是服務(wù)器返回的完整數(shù)據(jù)
      //數(shù)據(jù)處理
      NSString * result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
      NSLog(@"%@",result);
    

    }];

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • iOS開發(fā)系列--網(wǎng)絡(luò)開發(fā) 概覽 大部分應(yīng)用程序都或多或少會牽扯到網(wǎng)絡(luò)開發(fā),例如說新浪微博、微信等,這些應(yīng)用本身可...
    lichengjin閱讀 4,058評論 2 7
  • 同步請求可以從因特網(wǎng)請求數(shù)據(jù), 一旦發(fā)送同步請求,程序?qū)⑼V褂脩艚换?直至服務(wù)器返回?cái)?shù)據(jù)完成, 才可以進(jìn)行下一步操...
    小灬博閱讀 944評論 2 4
  • 本文首發(fā)于我的個(gè)人博客:「程序員充電站」[https://itcharge.cn]文章鏈接:「傳送門」[https...
    ITCharge閱讀 6,271評論 0 24
  • 1、同步請求可以從因特網(wǎng)請求數(shù)據(jù),一旦發(fā)送同步請求,程序?qū)⑼V褂脩艚换?,直至服?wù)器返回?cái)?shù)據(jù)完成,才可以進(jìn)行下一步操...
    lilinjianshu閱讀 6,341評論 4 4
  • 田野下的童年 (三) 一件小事 欣怡媽媽是一個(gè)特別熱情的家長,見了老師總會主動(dòng)打招呼。交談時(shí),臉上總是帶著甜蜜的笑...
    佳美腳蹤閱讀 396評論 0 0

友情鏈接更多精彩內(nèi)容