
1.get/post介紹
get和post是最常用的http/htttps請求方式,一定要掌握好二者各自特點以及區別,
2.get/post特點
get請求
請求引數在請求地址后面,提交的資料量較小,安全性較差,不建議用來提交敏感資訊(地址欄中會顯示,并且有可能被保存請求地址),post請求
請求引數放在請求體中提交,提高的資料量大小沒有限制,敏感資訊相對安全,
3.get/post區別
GET一般用于獲取/查詢資源資訊,而POST一般用于更新資源資訊,
1、GET引數通過URL傳遞,POST放在Request body中, 2、GET請求會被瀏覽器主動cache,而POST不會,除非手動設定, 3、GET請求引數會被完整保留在瀏覽器歷史記錄里,而POST中的引數不會被保留, 4、GET請求只能進行url編碼,而POST支持多種編碼方式, 5、POST請求的安全性比GET請求的安全性高,因為GET請求的引數是不加密,明文傳輸的,且會直接顯示到瀏覽器的瀏覽框上;POST請求的資料則是可加密的,也不會顯示到瀏覽器的瀏覽框上, 6、GET請求中有非 ASCII 字符,會在請求之前進行轉碼,POST不用,因為POST在Request body中,通過 MIME,也就可以傳輸非 ASCII 字符,
4.Get
1、請求是為了查找資源,HTML表單資料僅為了幫助搜索,
2、地址欄中直接發起的請求都是get請求,form表單的默認提交方式也是get請求,超鏈接發起的也是get請求;
5.Post
1、只有將form表單的提交方式改成post時發起的才是post請求,
2、有敏感資料
3、傳輸的資料不是普通字符
4、傳輸的資料非常多
5、請求是為了修改服務器資源
一般我們在瀏覽器輸入一個網址訪問網站都是GET請求,
登錄一個網站、增加新聞評論是POST請求,
6.http發起get/post請求
#http請求 import requests #post請求 login_url="http://www.qabujiaban.com/user/login" data = {"username":"uuuu222都44","password":"WJHasb124*1"} res = requests.post(login_url,data) #def post(url, data=https://www.cnblogs.com/QAbujiaban/p/None, json=None, **kwargs): #url 請求地址 #data 請求引數,字典形式傳參(dict) #json json格式的引數(需要轉換) print("回應正文:",res.json()) print("回應頭:",res.headers) print("回應Cookies",res.cookies)#此cookie不可用 #get請求 query_url = "http://www.qabujiaban.com/user/query" query_headers = {"Content-Type":"application/json;charset=UTF-8s"} rq = requests.get(url=query_url,headers=query_headers,cookies=res.cookies) #def get(url, params=None, **kwargs): #url 請求地址 #params 請求引數 #**kwargs 攜帶引數(指定) print("回應狀態碼:",rq.status_code) print("回應頭:",rq.headers) print("請求頭:",rq.request.headers) print("回應正文:",rq.text)#html格式 print("回應正文:",rq.json())#json格式
7.http發起get/post請求封裝
#封裝http請求 import requests class HttpRequest(): #需要的引數 #method:請求方式 #url:請求的url #param:請求引數 #headers:請求頭 #cookie:請求的cookie值 def __init__(self, method, url, param=None, headers=None, cookie=None): self.method=method self.url=url self.param=param self.headers=headers self.cookie=cookie def http_request(self): if self.method.lower()=="post": return requests.post(self.url,self.param) elif self.method.lower()=="get": return requests.get(self.url,self.param,headers=self.headers,cookies=self.cookie) else: print("請求方式錯誤:{0}".format(self.method)) if __name__ == '__main__': login_url="http://www.qabujiaban.com/user/login" data = {"username":"uuuu222都44","password":"WJHasb124*1"} #登陸 res = HttpRequest("Post",login_url,data).http_request() print("登陸回應文本:",res.json())# cookie=res.cookies#獲取cookie query_url = "http://www.qabujiaban.com/user/query" query_headers = {"Content-Type":"application/json;charset=UTF-8s"} #查詢 rqs = HttpRequest("Get",query_url,headers=query_headers,cookie=cookie).http_request() print("查詢回應文本:",rqs.json()) # print("Uu".lower())
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/540263.html
標籤:Python
