5 回答

TA貢獻1836條經驗 獲得超13個贊
// 使用HttpClient發送請求、接收響應很簡單,一般需要如下幾步即可。
// 1. 創建HttpClient對象。
// 2. 創建請求方法的實例,并指定請求URL。如果需要發送GET請求,創建HttpGet對象;如果需要發送POST請求,創建HttpPost對象。
// 3. 如果需要發送請求參數,可調用HttpGet、HttpPost共同的setParams(HetpParams params)方法來添加請求參數;對于HttpPost對象而言,也可調用setEntity(HttpEntity entity)方法來設置請求參數。
// 4. 調用HttpClient對象的execute(HttpUriRequest request)發送請求,該方法返回一個HttpResponse。
// 5. 調用HttpResponse的getAllHeaders()、getHeaders(String name)等方法可獲取服務器的響應頭;調用HttpResponse的getEntity()方法可獲取HttpEntity對象,該對象包裝了服務器的響應內容。程序可通過該對象獲取服務器的響應內容。
// 6. 釋放連接。無論執行方法是否成功,都必須釋放連接
//入口函數
public static void main(String[] args) throws Exception {
??????? //cookie存儲類
??????? BasicCookieStore cookieStore = new BasicCookieStore();
??????? //創建一個httpclient,并設置默認cookie存儲cookieStore
??????? CloseableHttpClient httpclient = HttpClients.custom()
??????????????? .setDefaultCookieStore(cookieStore)
??????????????? .build();
??????? try {
??????????? //創建HttpGet對象,需要發送GET請求https://someportal/
??????????? HttpGet httpget = new HttpGet("https://someportal/");
??????????? //HttpClient對象的execute(HttpUriRequest request)發送請求
??????????? CloseableHttpResponse response1 = httpclient.execute(httpget);
??????????? //try開始,試圖執行
??????????? try {
??????????????? //獲取服務器返回的HttpEntity對象
??????????????? HttpEntity entity = response1.getEntity();
??????????????? //打印返回信息,response.getStatusLine().getStatusCode()==200鏈接成功,==500失敗。
??????????????? System.out.println("Login form get: " + response1.getStatusLine());
??????????????? //HttpEntity對象提供的靜態幫助類,EntityUtils.consume關閉資源
??????????????? EntityUtils.consume(entity);
??????????????? //打印Initial set of cookies:
??????????????? System.out.println("Initial set of cookies:");
??????????????? //獲取Cookies并循環,空的輸出None,否則輸出cookie
??????????????? List<Cookie> cookies = cookieStore.getCookies();
??????????????? if (cookies.isEmpty()) {
??????????????????? System.out.println("None");
??????????????? } else {
??????????????????? for (int i = 0; i < cookies.size(); i++) {
??????????????????????? System.out.println("- " + cookies.get(i).toString());
??????????????????? }
??????????????? }
??????????? } finally {
??????????????? //關閉
??????????????? response1.close();
??????????? }
??????????? //POST請求,并設置URI和參數
??????????? HttpUriRequest login = RequestBuilder.post()
??????????????????? .setUri(new URI("https://someportal/"))
??????????????????? .addParameter("IDToken1", "username")
??????????????????? .addParameter("IDToken2", "password")
??????????????????? .build();
??????????? //執行POST請求
??????????? CloseableHttpResponse response2 = httpclient.execute(login);
??????????? try {
??????????????? //獲取實體對象
??????????????? HttpEntity entity = response2.getEntity();
??????????????? //打印服務器返回信息
??????????????? System.out.println("Login form get: " + response2.getStatusLine());
??????????????? //關閉流
??????????????? EntityUtils.consume(entity);
??????????????? //打印cookie
??????????????? System.out.println("Post logon cookies:");
??????????????? List<Cookie> cookies = cookieStore.getCookies();
??????????????? if (cookies.isEmpty()) {
??????????????????? System.out.println("None");
??????????????? } else {
??????????????????? for (int i = 0; i < cookies.size(); i++) {
??????????????????????? System.out.println("- " + cookies.get(i).toString());
??????????????????? }
??????????????? }
??????????? } finally {
??????????????? response2.close();
??????????? }
??????? } finally {
??????????? //關閉httpclient
??????????? httpclient.close();
??????? }
??? }

TA貢獻1817條經驗 獲得超6個贊
public static void main(String [] args){拋出異常
basiccookiestore =新basiccookiestore()存儲機制;
closeablehttpclient HttpClient = httpclients custom()。
setdefaultcookiestore(存儲機制)。
build();
嘗試{
HttpGet HttpGet =新HttpGet(“https:/ / someportal /”);
closeablehttpresponse response1 = HttpClient執行(HTTPGET);
嘗試{
HttpEntity實體= response1 getentity();
系統。了。println(“登錄形式得到:“+ response1 getstatusline()。);
entityutils消耗(實體);
系統。了。println(“初始設定的餅干:“);
列表<餅干>餅干= getcookies()存儲機制;
如果(餅干。isempty()){
系統。了println(“無”);
{ }
為(int i = 0;i <餅干。size();i++){
系統。了。println(“-”+餅干。讓(我)。tostring());
}
}
最后{
response1();
}
httpurirequest登錄= requestbuilder post()。
seturi。(新的URI(“http:/ / someportal /”))
addparameter。(“idtoken1”,“用戶名”)
addparameter。(“idtoken2”,“密碼”)
build();
closeablehttpresponse反應= HttpClient執行(登錄);
嘗試{
HttpEntity實體=反應。getentity();
系統。了。println(“登錄形式得到:“+反應。getstatusline());
entityutils消耗(實體);
系統。了。println(“后登錄餅干:”);
列表<餅干>餅干= getcookies()存儲機制;
如果(餅干。isempty()){
系統。了println(“無”);
{ }
為(int i = 0;i <餅干。size();i++){
系統。了。println(“-”+餅干。讓(我)。tostring());
}
}
最后{
反應。();
}
最后{
HttpClient();
}
}
添加回答
舉報