iOS GET請求和POST請求

2021-07-05 13:21:06 字數 2359 閱讀 7761

我們都知道請求資料時字首會有乙個http,而http請求資料時會有兩種方法:get 和 post。

get 和 post的區別:

get 特徵

1.瀏覽器和伺服器對url長度有限制,因此在url後面附帶的引數是有限制的,通常不能超過1kb

2.會把請求的資料暴露在介面裡面

post 特徵

1.引數全部放在請求體中這樣就保證了資料的安全。

2.沒有具體的長度限制,唯一的限制就是伺服器的承受能力

選擇get和post的建議

(1)如果要傳輸大量資料,比如檔案上傳,只能用post請求

(2)get的安全性比post差,如果包含機密/敏感資訊。建議用post

(3)如果僅僅是索取資料(資料查詢),建議用get

(4)如果是增加,修改,刪除資料,建議用post

下面是get和post在不同介面裡面的使用:

get

nsstring *inte***cestring = @"";

nsstring *requestcontentstring = @"num=18096072457";

// 準備好位址字串 urlstring

nsstring *urlstring = [nsstring stringwithformat:@"%@?%@",inte***cestring,requestcontentstring];

// 把鏈結位址字串轉換成utf8格式 這一步不是必須的

nsurl *url = [nsurl urlwithstring:[urlstring stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]];

// 可變的請求 可以新增 請求方式 以及請求的請求頭 或者更多

// timeoutinterval 請求所需的時間 超過 時間 不再傳送這個請求 cachepolicy快取內容的方式

nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:url cachepolicy:nsurlrequestuseprotocolcachepolicy timeoutinterval:10];

// get 一般是大寫 指定http的請求方式 為get

// 把apikey 傳送給 伺服器 指定的請求頭 位置

// 請求的引數

// 3 表示ios裝置

nsdictionary *dic = @;

nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:url cachepolicy:nsurlrequestuseprotocolcachepolicy timeoutinterval:10];

// 設定http請求的方式 post 把請求的資料放到 httpbody 裡面

// 設定請求的引數 datausingencoding 把字串轉成 nsdata

// httpbody 要的是nsdata型別的資料,需要把字典轉換為nsdata資料

get請求和post請求

1.get請求的請求資料在url中,post請求的請求資料在請求體中 2.資料長度,get請求有長度限制,post請求的長度限制由伺服器決定 3.資料安全性,兩者都不安全,但post相對安全一些 4.應用場景不同,get請求一般用於訪問頁面,post請求一般用於上傳資料 response reque...

GET請求和POST請求

http請求,由客戶端向服務端發出,可以分為四部分內容 請求方法 request method 請求的 request url 請求頭 request headers 請求體 request body 請求方法有get head post put delete options connect tra...

get請求和post請求

一 使用requests傳送get請求 二 使用urllib傳送get請求 urllib3主要使用連線池進行網路的訪問,以訪問之前我們需要建立乙個連線池物件 備註 這邊會有警告,這是因為urllib3時代,官方強制要求驗證http安全證書,如果沒有通過不能通過請求,這邊新增 urllib3.disa...