RestTemplate介面請求總結

2021-09-28 11:40:43 字數 1591 閱讀 2039

一、獲取介面返回狀態碼

使用getforentity呼叫介面,返回結果呼叫getstatuscode()方法取得httpstatus物件,然後就可以呼叫裡面的各種方法來滿足你的需求了

//判斷介面返回是否為200

public

static boolean ping()

catch

(exception e)

}

二、什麼都不帶,將引數拼接在請求url後面

將引數拼接在請求url後面,postforobject請求引數為null

public

static

void

login

(string usercode,string md5password,

int companyid,webdriver driver)

catch

(exception e)

}

三、帶cookie,header,引數
/*

獲取瀏覽器的cookie,將其塞入header中

"cookie為:" + cookielist.tostring());

;//將cookie放入header

headers.

setcontenttype

;return headers;

}

放入引數:

1、使用multivaluemap用來放引數,(使用hashmap不行,具體原因可見 ),

2、根據header和引數例項化httpentity ,然後呼叫postforentity方法呼叫介面

完整**如下(使用方法可參考:

public

static

void

gitupload

(webdriver driver,string uploadurl,string filename,string content,string branch,string requesturl)

throws exception

四、使用exchange指定呼叫方式

使用exchange方法可以指定呼叫方式

需要注意的一點是對於返回結果為204 no content,這種沒有返回值的請求,resttemplate會拋錯,有需要的話可以使用httpclient的fluent

public

void

deletequeue

(string vhost,string queue)

參考:

RestTemplate介面請求總結

使用getforentity呼叫介面,返回結果呼叫getstatuscode 方法取得httpstatus物件,然後就可以呼叫裡面的各種方法來滿足你的需求了 判斷介面返回是否為200 public static boolean ping catch exception e 二 什麼都不帶,將引數拼接...

RestTemplate請求http介面示例

springmvc提供 resttemplate請求http介面,resttemplate的底層可以使用第三方的http客戶端工具實現http 的請求,常用的http客戶端工具有apache httpclient okhttpclient等。這裡以okhttpclient為例講解resttempla...

php curl 介面請求

通過url獲取頁面資訊 param string url 位址 return string 返回頁面資訊 function get url url 模擬post提交 param string url 位址 param string data 提交的資料 return string 返回結果 func...