JAVA新手,在項目中應用到的工具,進行簡單的總結,隨便寫寫,有不對但願大神指正,謝謝....java
在開發項目中, 若是須要進行跨域請求,都會涉及到在代碼中實現HTTP的請求訪問.apache
(百度過許屢次都沒有發現比較簡單的方法,因此就簡單整理一下)json
這裏以Maven項目爲例進行講解:跨域
除其餘配置外還須要Maven包:服務器
<!-- 發送HTTP請求 --> <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/fluent-hc --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>fluent-hc</artifactId> <version>4.5.3</version> </dependency> <!-- net.sf.json.JSONObject 相關jsOn轉化 --> <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> <classifier>jdk15</classifier> </dependency>
請求工具代碼以下:微信
package com.wx.tool; import org.apache.http.client.fluent.Request; import org.apache.http.client.fluent.Response; import net.sf.json.JSONObject; public class HttpUtil { /** * <p>方法說明: HTTP POST 請求 * <p>編碼格式: UTF8 * <p>參數說明: String urL 請求的路徑 * <p>參數說明: String parAms 請求的參數 * <p>返回說明: JSONObject * */ public static JSONObject doPost(String url, String params) throws Exception { Request request = Request.Post(url); request.bodyByteArray(params.getBytes("UTF8")); Response response = request.execute(); String jsonData = response.returnContent().asString(); /* 轉化爲 JSONObject 數據 */ JSONObject json = JSONObject.fromObject(jsonData); return json; } /** * <p>方法說明: HTTP GET 請求 * <p>編碼格式: UTF8 * <p>參數說明: String urL 請求的路徑 * <p>返回說明: JSONObject * */ public static JSONObject doGet(String url) throws Exception{ Request request = Request.Get(url); request.setHeader("Content-type", "application/json;charset=UTF8"); Response response = request.execute(); String jsonData = response.returnContent().asString(); JSONObject json = JSONObject.fromObject(jsonData); return json; } /** * <p>方法說明: HTTP GET 請求 * <p>編碼格式: UTF8 , 微信編碼轉爲UTF-8 * <p>參數說明: String urL 請求的路徑 * <p>返回說明: JSONObject * */ public static JSONObject doGetUTF8(String url) throws Exception { Request request = Request.Get(url); request.setHeader("Content-type", "application/json;charset=UTF8"); Response response = request.execute(); String jsonData = response.returnContent().asString(); String string = new String(jsonData.getBytes("ISO-8859-1"),"UTF-8"); JSONObject json = JSONObject.fromObject(string); return json; } /** * <p>方法說明: HTTP POST 請求 * <p>編碼格式: UTF8 * <p>參數說明: String urL 請求的路徑 * <p>參數說明: String parAms 請求的參數 * <p>返回說明: String * */ public static String doPostToStr(String url, String params) throws Exception { Request request = Request.Post(url); request.bodyByteArray(params.getBytes("UTF8")); Response response = request.execute(); return response.returnContent().asString(); } /** * <p>方法說明: HTTP GET 請求 * <p>編碼格式: UTF8 * <p>參數說明: String urL 請求的路徑 * <p>返回說明: String * */ public static String doGetToStr(String url) throws Exception { Request request = Request.Get(url); request.setHeader("Content-type", "application/json;charset=UTF8"); Response response = request.execute(); return response.returnContent().asString(); } }
上面是我封裝的幾個簡單方法,這樣就能夠直接使用了:app
package com.wx.service.impl; import java.text.SimpleDateFormat; import java.util.Date; import com.tool.IsNull; import com.wx.bean.AppPath; import com.wx.tool.HttpUtil; import net.sf.json.JSONObject; /** * <p>類說明: 獲取token線程 * <p>建立人: geYang * <p>建立時間:2017.08.29 * */ public class TokenThread implements Runnable { private static String access_token; private boolean close = false; //線程控制 /* 外界拿取access_token方法 */ public static String getToken(){ return access_token; } public void performClose() { this.close = true; } @Override public void run() { while (!close){ try{ if( IsNull.isNull(access_token) ){ Thread.sleep(1000*3); //獲取的access_token爲空休眠3秒 }else{ Thread.sleep(7000*1000); //獲取到access_token 休眠7000秒 } access_token = getAccessToken(); //向服務器發起請求 System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); System.out.println(access_token); }catch(Exception e){ System.out.println("發生異常"); e.printStackTrace(); try{ Thread.sleep(1000*1); //發生異常休眠1秒 }catch (Exception e1){ e.printStackTrace(); System.out.println("休眠發生異常"); } } } } /** * <p>方法說明: 獲取access_token * */ private static String getAccessToken(){ try { /* 進行HTTP GET 請求 */ JSONObject jsonObj = HttpUtil.doGet(AppPath.getUrl()); String access_token = jsonObj.getString("access_token"); return access_token; } catch (Exception e) { e.printStackTrace(); System.out.println("com.wx.tool.TokenThread.getAccessToken請求異常"); } return null; } /** * <p>方法說明:手動獲取access_token * */ public static void main(String[] args) { String access_token = getAccessToken(); System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); System.out.println(access_token); } }
在 fluent-hc 包 中自動爲咱們處理了HTTP協議等相關高深問題.咱們只須要傳入路徑和參數就能夠使用GET和POST等請求,是否是很是簡單.....ide