JAVA标准API对接外部接⼝st;
import java.io.IOException;
import java.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.ity.UrlEncodedFormEntity;
import org.apache.hods.CloseableHttpResponse;
import org.apache.hods.HttpGet;
import org.apache.hods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.ity.ContentType;
import org.ity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.ssage.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
app接口测试工具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 = ateDefault();
String resultString = "";
CloseableHttpResponse response = null;
try {
// 创建uri
URIBuilder builder = new URIBuilder(url);
if (param != null) {
for (String key : param.keySet()) {
builder.addParameter(key, (key));
}
}
URI uri = builder.build();
/
/ 创建http GET请求
HttpGet httpGet = new HttpGet(uri);
// 执⾏请求
response = ute(httpGet);
// 判断返回状态是否为200
if (StatusLine().getStatusCode() == 200) {
resultString = Entity(), "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 = ateDefault();
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, (key)));
}
// 模拟表单
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList);
httpPost.setEntity(entity);
}
// 执⾏http请求
response = ute(httpPost);
resultString = Entity(), "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 = ateDefault();
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 = ute(httpPost);
resultString = Entity(), "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 = "bk.tencent/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 = "d/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);
JSONString(jsonObject);
}
public static String getServiceId(){
String url = "d/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 = "d/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;
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。