不同請求方式的介面呼叫
在工作中不可避免的要去呼叫第三方的介面去獲得和使用相應的資料,請求的方式也有很多,包括post,get,put等等,不過最為常見的就是post和get兩種方式了,下面我就簡單的進行一下總結。
1.get請求無引數
@Override public BaseResponse queryAirSearchList() { if(search==null||search.equals("")){ String gm = PropertiesUtil.getInstance().getProperty("rh.airsearch"); HttpClient client = new HttpClient(); try { GetMethod getMethod = new GetMethod(gm); getMethod.setRequestHeader("Authorization",TaskDDToken.DDTOKEN); logger.info("第三方介面開始呼叫"); client.executeMethod(getMethod); String info = new String(getMethod.getResponseBody(),"UTF-8"); JSONObject responseJson = JSON.parseObject(info); JSONObject datas = (JSONObject) responseJson.get("data");//拿到key String code = responseJson.getString("code"); logger.info("第三方介面呼叫完成"); //判斷code是不是10001 if(code.equals("10001")){ TaskDDToken.genToken(); }else{ } //將獲取到的資料放到search裡面 search = datas; System.out.println(info); logger.info("航線搜尋成功{}", info); System.out.println(search.toJSONString()); return new BaseResponse("0000", "航線搜尋成功",search); } catch (Exception e) { logger.error("呼叫失敗"); e.printStackTrace(); return new BaseResponse("9999", "航線搜尋失敗"); } }else{ return}
這個是一個get請求的無引數的一個示例。在這裡我把我的思路解釋一下。首先要有一個HttpClient請求,其次定義出你請求第三方介面的路徑,接下來就利用get請求的方式進行組裝,組裝完成後client進行呼叫。不管呼叫是否成功,都要接收返回回來的引數,最常見的就是json格式的資料,我現在也沒有碰到其他型別的資料。將接受到的返回引數進行解析,最後列印。成功的話一般返回的是200或者0000.由於我這邊沒有正在執行demo,大家就只能湊合著腦補啦。大致就是這麼一個樣子。
2.get請求有引數(一個或者多個)
@Override public BaseResponse queryAirSearchList(String departure, String arrive, String departure_at, String category, String return_at) { if(search==null||search.equals("")){ //System.out.println(departure+arrive+departure_at+category+return_at); String gm = PropertiesUtil.getInstance().getProperty("rh.airsearch"); //String gms = gm+"?departure="+departure+"&arrive="+arrive+"&departure_at="+departure_at+"&category="+category+"&return_at="+return_at; //System.out.println(gms); HttpClient client = new HttpClient(); List<NameValuePair> params = Lists.newArrayList(); params.add(new BasicNameValuePair("departure",departure)); params.add(new BasicNameValuePair("arrive",arrive)); params.add(new BasicNameValuePair("departure_at",departure_at)); params.add(new BasicNameValuePair("category",category)); params.add(new BasicNameValuePair("return_at",return_at)); String str= null; try { str = EntityUtils.toString(new UrlEncodedFormEntity(params, Consts.UTF_8)); GetMethod getMethod = new GetMethod(gm+"?"+str); getMethod.setRequestHeader("Authorization",TaskDDToken.DDTOKEN); logger.info("第三方介面開始呼叫"); client.executeMethod(getMethod); String info = new String(getMethod.getResponseBody(),"UTF-8"); JSONObject responseJson = JSON.parseObject(info); JSONObject datas = (JSONObject) responseJson.get("data");//拿到key String code = responseJson.getString("code"); logger.info("第三方介面呼叫完成"); //判斷code是不是10001 if(code.equals("10001")){ TaskDDToken.genToken(); }else{ } //將獲取到的資料放到search裡面 search = datas; System.out.println(info); logger.info("航線搜尋成功{}", info); System.out.println(search.toJSONString()); return new BaseResponse("0000", "航線搜尋成功",search); } catch (Exception e) { logger.error("呼叫失敗"); e.printStackTrace(); return new BaseResponse("9999", "航線搜尋失敗"); } }else{ return
這個是我在get無參上面修改的一個請求。
這裡需要注意的是引數的組裝,一個是前臺傳給你,你這邊後臺負責接收,一個是你這邊demo測試的時候自己給定它相應的值。
前臺傳值大致可以這樣,自己新建一個dto或者entity,將所傳引數的欄位寫進去,後臺接收相應的dto即可。
後臺給定相應的值,可以自己寫一個map,將相應的請求引數臨時傳到請求中。
在這裡我就是用前臺請求的方式進行組裝引數,待會在post請求中我會利用map進行臨時引數儲存。
3.post請求帶引數
Map<String,String> params = new HashMap<String, String>(); params.put("accountId","8451252"); params.put("name","8888"); params.put("cardNo","999999"); params.put("orderId","8888888"); params.put("purpose","還款"); params.put("amount","0.01"); params.put("responseUrl","http://IP:PORT"); params.put("key","123456abc"); String paymentStr = getPaymentStr(params); System.out.println(paymentStr); String mac = BankMD5Utils.MD5(paymentStr); System.out.println(mac); params.put("mac",mac); HttpClient httpclient = new HttpClient(); PostMethod post = new PostMethod("http://182.251.210.21:4545/delegate-pay-front-dp/delegatePay/pay"); post.setRequestHeader("Accept", "application/json"); post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8"); post.addParameter("accountId", params.get("accountId")); post.addParameter("name", params.get("name")); post.addParameter("cardNo", params.get("cardNo")); post.addParameter("orderId", params.get("orderId")); post.addParameter("purpose", params.get("purpose")); post.addParameter("amount", params.get("amount")); post.addParameter("responseUrl", params.get("responseUrl")); post.addParameter("mac", params.get("mac")); try { httpclient.executeMethod(post); String info = new String(post.getResponseBody(), "utf-8"); JSONObject responseJson = JSON.parseObject(info); System.out.println(info); System.out.println("resp"+responseJson); } catch (IOException e) { e.printStackTrace(); }
由於有了get請求的相關示例,這裡我就直接把post請求進行一個合併。這裡我採用的是map進行存值操作,並且根據要求對其中的值進行加密,對於加密我在最後會提及。最後new出一個post請求,將這些引數組裝進去,最後接收返回的值進行列印。大致和get請求差不多,不過post是隱式傳參,get是顯示傳參。
4.加密方式
目前我所接觸到的主要有四種:CA證書,Token,MD5,MD5+鹽
CA證書的話需要自己公司這邊協商CA那邊,然後通過頒發相應的證書以及其他加密規則進行加密。
Token的話,大致來說即使客戶端請求服務端,服務端會頒發一個token,客戶端拿到後會請求服務端將token進行比對,從而校驗通過。
MD5:這個可以說是加密界的元老了,網上也有很多的加密規則,包括16位和32位的加密規則。還有大小寫轉換等等。比如上面的post請求就是用用md5加密規則生成32位的小寫加密密文。很不幸加密程式碼丟掉了,不過網上還是可以找的到的,大家可以研究一下。
最後一種只是在專案中驚鴻一bie,就不到這裡說了。
總結一下吧,其實像我這樣寫的介面請求還是有很多問題的,比如沒有經過驗證,驗證欄位的合理性和相應規則,返回引數的解析列印和log日誌的輸出,所以呀,還是有很多需要學習的地方,大家一起加油吧。。。