IOS中POST和GET本地服務測試

來源:互聯網
上載者:User

標籤:

首先,要瞭解get和post的概念,get和post都是訪問伺服器的方法,但使用還是有區別的。從字面意思上看,get是“取”的意思,post有“推送”之意,我們可以做個簡單的PHP頁面,放在根網站下(關於PHP+MySQL配置,以及Apache的配置本人就不在多講了,大家問度娘即可,熟悉java的也可以用java+Apache tomcat的方法)下面是PHP原始碼,大家可以拷貝,儲存為.php檔案即可(做get測試的時候,將代碼中的POST改為GET即可)。

<?php
//定義常量
define(DB_HOST, ‘127.0.0.1‘);
define(DB_USER, ‘root‘);
define(DB_PASS, ‘123456‘);
define(DB_DATABASENAME, ‘itcast‘);
define(DB_TABLENAME, ‘userInfo‘);

//mysql_connect
$conn = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die("連結資料庫失敗" . mysql_error());
mysql_select_db(DB_DATABASENAME, $conn);

$userName = $_POST[‘userName‘];
$userPwd  = $_POST[‘userPwd‘];

//根據提交的使用者名稱和密碼查詢
$sql = sprintf("select * from %s where userName=‘%s‘ and userPwd=‘%s‘", DB_TABLENAME, $userName, $userPwd);
$result = mysql_query($sql, $conn);
//表格
$count = mysql_num_rows($result);

echo "使用者名稱:".$_POST[‘userName‘].",密碼:".$_POST[‘userPwd‘]."的記錄有".$count."條<br/>";
mysql_free_result($result);
mysql_close($conn);

?>

假設,我們做了個PHP網頁,命名為GetUserInfo.php,並放在跟網站之下,假設我要通過此頁面訪問我的資料庫itcast中的table:userInfo,資料庫如下:

1、get:get方法是用於直接從伺服器上取資源,我們在串連總,url形式為:http://localhost/GetUserInfo.php?userName=zhang&userPwd=1

2、post:通過資料體發送請求。url形式為:http://localhost/GetUserInfo.php.

兩者的區別在於,get是直接擷取資料,post需要封裝資料體,塞給伺服器才可以擷取到資料,具體差異,請參見原始碼:

#import "QZLViewController.h"

@interface QZLViewController ()
@property (weak, nonatomic) IBOutlet UITextField *userName;
@property (weak, nonatomic) IBOutlet UITextField *userPwd;
@property (weak, nonatomic) IBOutlet UILabel *stateLabel;

@end

@implementation QZLViewController

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

- (IBAction)testGet {
    
    //1、建立一個url
    NSString *urlStr = [NSString stringWithFormat:@"http://localhost/GetUserInfo.php?userName=%@&userPwd=%@",self.userName.text,self.userPwd.text];
    
    NSURL *url = [NSURL URLWithString:urlStr];
    
    //2、定義一個請求
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    //3、發送非同步網路請求
    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        
        //如果伺服器串連正常,執行一下操作
        if (connectionError == nil)
        {
            //輸出伺服器返回的資料
            NSString *tempStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@"%@",tempStr);
            
            //設定登入狀態(更新UI在主線程中進行)
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                self.stateLabel.text = @"登陸完成";
            }];
        }
    }];

}
- (IBAction)testPost {
    //1、定義url串
    NSString *urlStr = @"http://localhost/GetUserInfo.php";
    NSURL *url = [NSURL URLWithString:urlStr];
    
    //2、定義可變請求
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    
    //3、佈建要求方法
    request.HTTPMethod = @"POST";
    
    //4、設定資料體字串
    NSString *requestStr = [NSString stringWithFormat:@"userName=%@&userPwd=%@",self.userName.text,self.userPwd.text];
    
    //5、將資料體字串轉化為二進位請求資料流
    NSData *requestData = [requestStr dataUsingEncoding:NSUTF8StringEncoding];
    request.HTTPBody = requestData;
    
    //6、發送網路請求
    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        
        if (connectionError == nil)
        {
            NSString *tempStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@"%@",tempStr);
            
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                self.stateLabel.text = @"登陸完成";
            }];
        }
    }];
    
}

@end

執行post測試結果如下(要想搞清楚,請細讀原始碼,區分下兩者的URL有何不同)

IOS中POST和GET本地服務測試

聯繫我們

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