webservice restful类型接口的调用实例
生活随笔
收集整理的這篇文章主要介紹了
webservice restful类型接口的调用实例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
因為項目是采用maven搭建的,所以先把所需要的jar包定義進來,我是采用apache的httpComponents第三方技術!如下:
?
????????????<!-- httpcomponents --><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpcore</artifactId></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpmime</artifactId></dependency><dependency><groupId>jaxen</groupId><artifactId>jaxen</artifactId></dependency>其次就是寫調用客戶端(get方式):
?
????????@Testpublic void getOrderList() throws Exception {CloseableHttpClient httpClient = HttpClients.createDefault();HttpGet httpGet = new HttpGet("接口請求路徑……"); ?????httpGet.setHeader("Accept", "application/json");//如果要返回json結果,則需要設置此請求頭信息 CloseableHttpResponse httpResponse;try {httpResponse = httpClient.execute(httpGet);HttpEntity entity = httpResponse.getEntity();System.out.println(IOUtils.toString(entity.getContent()));} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}其次就是寫調用客戶端(POST方式):
導包都是一樣,就是發送請求的實體不同 如下:
?
????????/* 獲取用戶留資信息接口:GetUserLeaveMsg */@Testpublic void GetUserLeaveMsg() throws Exception {CloseableHttpClient httpClient = HttpClients.createDefault();HttpPost httpPost = new HttpPost(URL + "?method=GetUserLeaveMsg"); ???????????????//POST傳遞方式?List<NameValuePair> nvps = new ArrayList<NameValuePair>();nvps.add(new BasicNameValuePair("date", "2014-12-01"));// yyyy-MM-ddnvps.add(new BasicNameValuePair("last_leave_msg_id", "0"));nvps.add(new BasicNameValuePair("CurPage", "1"));nvps.add(new BasicNameValuePair("PageSize", "50"));httpPost.setHeader("Accept", "application/json");httpPost.setEntity(new UrlEncodedFormEntity(nvps));CloseableHttpResponse httpResponse;try {httpResponse = httpClient.execute(httpPost);HttpEntity entity = httpResponse.getEntity();System.out.println(IOUtils.toString(entity.getContent()));} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}?
?
還會有一種就是傳Json的方式,我也一并羅列出來了供大家參考:
?
????????/*** 新聞資訊類接口* * @throws Exception* @throws UnsupportedEncodingException*/@Testpublic void newsTest() throws Exception {CloseableHttpClient httpClient = HttpClients.createDefault();HttpPost httpPost = new HttpPost("接口請求url……");httpPost.addHeader("Authorization", "Basic " + sign);httpPost.setHeader("Accept", "application/json");httpPost.setHeader("Content-Type", "application/json");CloseableHttpResponse httpResponse;try { ???????????????????? //通過alibaba的fastJson的jar包來轉成json所需要的數據格式Map<String, Object> params = new HashMap<String, Object>();List<String> list = new ArrayList<String>();list.add("2348");list.add("163"); ????????????????????????params.put("DealerId", 66934);params.put("TypeId", 1);params.put("SeriesIds", list);String data = JSON.toJSONString(params);System.out.println("data-->" + data);/* 構造請求數據 */StringEntity myEntity = new StringEntity(data, ContentType.APPLICATION_JSON);/* 設置請求體 */httpPost.setEntity(myEntity);httpResponse = httpClient.execute(httpPost);HttpEntity entity = httpResponse.getEntity();System.out.println(httpPost.getURI().toString());System.out.println(IOUtils.toString(entity.getContent()));} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}?
?
就先寫到這里了,還有通過SOAP協議定義的接口就下回再寫,寫得比較倉促難免會出錯,如發現錯誤或者需要共同討論的地方
轉載于:https://my.oschina.net/rightemperor/blog/792655
總結
以上是生活随笔為你收集整理的webservice restful类型接口的调用实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux常用命令记录
- 下一篇: 实验十——一维数组的定义及引用