JAVA 标准API对接外部接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | package com.erp.finance.test; import java.io.IOException; import java.net.URI; import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.utils.URIBuilder; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; public class bk_itsm_HttpClientUtil { /** * 带参数的get请求 * @param url * @param param * @return String */ public static String doGet(String url, Map<String, String> param) { // 创建Httpclient对象 CloseableHttpClient httpclient = HttpClients.createDefault(); String resultString = "" ; CloseableHttpResponse response = null ; try { // 创建uri URIBuilder builder = new URIBuilder(url); if (param != null ) { for (String key : param.keySet()) { builder.addParameter(key, param.get(key)); } } URI uri = builder.build(); // 创建http GET请求 HttpGet httpGet = new HttpGet(uri); // 执行请求 response = httpclient.execute(httpGet); // 判断返回状态是否为200 if (response.getStatusLine().getStatusCode() == 200 ) { resultString = EntityUtils.toString(response.getEntity(), "UTF-8" ); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (response != null ) { response.close(); } httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } return resultString; } /** * 不带参数的get请求 * @param url * @return String */ public static String doGet(String url) { return doGet(url, null ); } /** * 带参数的post请求 * @param url * @param param * @return String */ public static String doPost(String url, Map<String, String> param) { // 创建Httpclient对象 CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpResponse response = null ; String resultString = "" ; try { // 创建Http Post请求 HttpPost httpPost = new HttpPost(url); // 创建参数列表 if (param != null ) { List<NameValuePair> paramList = new ArrayList<>(); for (String key : param.keySet()) { paramList.add( new BasicNameValuePair(key, param.get(key))); } // 模拟表单 UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList); httpPost.setEntity(entity); } // 执行http请求 response = httpClient.execute(httpPost); resultString = EntityUtils.toString(response.getEntity(), "utf-8" ); } catch (Exception e) { e.printStackTrace(); } finally { try { response.close(); } catch (IOException e) { e.printStackTrace(); } } return resultString; } /** * 不带参数的post请求 * @param url * @return String */ public static String doPost(String url) { return doPost(url, null ); } /** * 传送json类型的post请求 * @param url * @param json * @return String */ public static String doPostJson(String url, String json) { // 创建Httpclient对象 CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpResponse response = null ; String resultString = "" ; try { // 创建Http Post请求 HttpPost httpPost = new HttpPost(url); // 创建请求内容 StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON); httpPost.setEntity(entity); // 执行http请求 response = httpClient.execute(httpPost); resultString = EntityUtils.toString(response.getEntity(), "utf-8" ); } catch (Exception e) { e.printStackTrace(); } finally { try { response.close(); } catch (IOException e) { e.printStackTrace(); } } return resultString; } /** * 聚合接口校验身份证 * @param idCard * @param realName * @return boolean */ public boolean identityCheck(String idCard, String realName){ System.out.println( "-----------------调用聚合数据 身份证验证API BEGIN--------------->" ); String key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" ; String url = "https://bk.tencent.com/api/c/compapi/v2/itsm/create_ticket" ; System.out.println( "请求url:" + url); boolean match = false ; //是否匹配 try { String result = bk_itsm_HttpClientUtil.doGet(url); System.out.println( "请求结果:" + result); IdentityCheckResult identityCheckResult = JsonUtils.parse(result, IdentityCheckResult. class ); } catch (Exception e) { e.printStackTrace(); } System.out.println( "<-----------------调用聚合数据 身份证验证API END---------------" ); return match; } public static void main(String[] args){ String url = "http://paas.xgd.com/api/c/compapi/v2/itsm/create_ticket/" ; String result =bk_itsm_HttpClientUtil.doPostJson(url,getApplyParam()); System.out.println( "result:" +result); } public static String getApplyParam(){ Map<String,String> map = new HashMap<String,String>(); JSONObject jsonObject = new JSONObject(); jsonObject.put( "bk_app_secret" , "0b1dfe6a-52e3-4490-8cbd-6e169975e23a" ); jsonObject.put( "bk_app_code" , "bk_itsm" ); jsonObject.put( "bk_username" , "panzehao" ); jsonObject.put( "service_id" , "17" ); jsonObject.put( "creator" , "panzehao" ); JSONArray jsonArray = new JSONArray(); //传输的数据 JSONObject jsonObject1 = new JSONObject(); jsonObject1.put( "key" , "title" ); jsonObject1.put( "value" , "何耀磊测试2" ); JSONObject jsonObject2 = new JSONObject(); jsonObject2.put( "key" , "WENTIMIAOSHU" ); jsonObject2.put( "value" , "OA对接蓝鲸2020051803" ); JSONObject jsonObject3 = new JSONObject(); jsonObject3.put( "key" , "LIUCHENGBIANHAO" ); jsonObject3.put( "value" , "LJ2020051803" ); jsonArray.add(jsonObject1); jsonArray.add(jsonObject2); jsonArray.add(jsonObject3); jsonObject.put( "fields" ,jsonArray); return JSON.toJSONString(jsonObject); } public static String getServiceId(){ String url = "http://paas.xgd.com/api/c/compapi/v2/itsm/get_services/" ; String result =bk_itsm_HttpClientUtil.doPost(url,getServiceIdParam()); System.out.println( "getServiceId: " +result); return result; } public static Map<String,String> getServiceIdParam(){ Map<String,String> map = new LinkedHashMap<String,String>(); map.put( "bk_app_secret" , "0b1dfe6a-52e3-4490-8cbd-6e169975e23a" ); map.put( "bk_app_code" , "bk_itsm" ); //map.put("bk_token", "Tasvr1Iscx7NfWl1S7hP3HDAUsRfPivbEy_XlvOQpb0"); //map.put("bk_token", "_GnTBIyE42ztKxk4jrY4SiZ4Pvje7nXKWuWuVgQL3yo"); map.put( "bk_token" , "LLgm3mOTHXIK3G6t7ZTnoOHilTRCUkjCv9d3x_kOb4Q" ); map.put( "bk_username" , "panzehao" ); //map.put("catalog_id", getServiceCatalogs()); map.put( "catalog_id" , "1" ); map.put( "service_type" , "request" ); System.out.println( "getServiceIdParam: " +map); return map; } public static String getServiceCatalogs(){ String url = "http://paas.xgd.com/api/c/compapi/v2/itsm/get_service_catalogs/" ; String result =bk_itsm_HttpClientUtil.doPost(url,getServiceCatalogsParam()); System.out.println( "getServiceCatalogs: " +result); return result; } public static Map<String,String> getServiceCatalogsParam(){ Map<String,String> map = new LinkedHashMap<String,String>(); map.put( "bk_app_secret" , "0b1dfe6a-52e3-4490-8cbd-6e169975e23a" ); map.put( "bk_app_code" , "bk_itsm" ); map.put( "bk_token" , "_GnTBIyE42ztKxk4jrY4SiZ4Pvje7nXKWuWuVgQL3yo" ); map.put( "bk_username" , "panzehao" ); map.put( "has_service" , "true" ); map.put( "service_key" , "change" ); System.out.println( "getServiceCatalogsParam: " +map); return map; } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?