日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

webservice restful类型接口的调用实例

發布時間:2025/3/18 编程问答 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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类型接口的调用实例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。