ios開發-get和post,ios開發-getpost

來源:互聯網
上載者:User

ios開發-get和post,ios開發-getpost

樣本:(在storyboard中拖兩個控制項textField、一個UIButton,連線(userNameText、password、login))

@interface ViewController()

@property(nonatomic,weak)IBOutlet UITextField *userNameText;

@property(nonatomic,weak)IBOutlet UITextField *passWord;

@end

 

-(IBAction)login

{

  [self postLoginWithUserName:self.userNameText.text  password:self.passWord.text];

}

post登陸

-(void)postLoginWithUserName:(NSString *)userName  password:(NSString *)password
{

  //1.url

  NSURL *url = [NSURL URLWithString:@"http://本機IP/login.php"];

  //2.request

  NSMutableNSURLRequest *request = [NSMutableNSURLRequest requestWithURL:url];

  //對於post請求的資料題要和request一起提交

  request.HTTPMethod = @"POST";

 

  //準備資料題

  NSString *bodyString = [NSString *stringWithFormat:@"username=%@&password=%@",username,password];

  //設定位元據

  request.HTTPBody = [bodyString dataUsingEncoding:NSUTF8StringEncoding];

  

 

  //3.發送網路連接,擷取位元據

  [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLReponse *response,NSData *data,NSError *connectionError)]; 

 NSString *result = [NSString alloc]initWithData:data encoding:NSUTF8Encoding];

}

 

 

 

 

get登陸

-(void)getLoginWithUserName:(NSString *)userName  password:(NSString *)password

{

  //1.url

  NSString *urlString = [NSString stringWithFormat:@"http://本機IP地址/login.php?username = %@&password = %@",userName,password];

  //百分比符號轉義

  urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

  NSURL *url = [NSURL URLWithString:urlString];

  //2.request

  NSURLRequest *request = [NSURLRequest requestWithURL:url];

  //3.發送網路連接,擷取位元據

  [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLReponse *response,NSData *data,NSError *connectionError)];

  NSString *result = [NSString alloc]initWithData:data encoding:NSUTF8Encoding];

}

 

小結:

1.

get:

http://本機IP地址/login.php?username = %@&password = %@

http://ip地址/登陸的指令檔?(後面有參數)參數變數名 = 參數數值&(繼續接參數)變數名 = 數值

post

http://ip地址/登陸的指令碼

2.request

get:什麼都不需要處理,因為參數已經在URL中指定了

post:指定存取方法

  HTTPMethod = @"post";

  

  指定位元據

  HTTPBody = 參數字串的位元據

 

3.NSURLConnection 非同步

get和post一樣

 

在實際網路開發中,真正會發生變化的地方就在請求

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.