更新时间:2021-09-18 GMT+08:00
Java代码样例
Java代码样例如下所示:
package main.java.com.huawei.softcom.ai.dfp; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.ClientConnectionManager; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.scheme.SchemeRegistry; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; /** * 利用HttpClient进行post请求的工具类 * @ClassName: HttpClientUtil * @Description:用户需要填写userName,passWord,taskId,url,data的值 * */ @SuppressWarnings("deprecation") public class DiskPredictClient { private static String userName = "xxxxxx"; private static String token = "xxxxxx"; private static String taskId = "0"; private static String url = "xxxxxx"; private static String data = "[{\r\n" + " \"disk_capacity\": \"820.6184120178223\",\r\n" + " \"disk_media_type\": \"HDD\",\r\n" + " \"disk_production\": \"6800V3\",\r\n" + " \"disk_data_source\": \"huawei\",\r\n" + " \"disk_pot\": \"31615\",\r\n" + " \"disk_collect_time\": \"20191205022933\",\r\n" + " \"disk_sn\": \"disk_001\",\r\n" + " \"device_sn\": \"device_001\",\r\n" + " \"disk_errorcount_nonmedium_ec\": \"0\",\r\n" + " \"disk_slow_type\": null,\r\n" + " \"disk_firmware_version\": \"A440\",\r\n" + " \"disk_slot\": \"8\",\r\n" + " \"disk_manufacturer\": \"HITACHI\",\r\n" + " \"disk_model\": \"HUC109090CSS600\",\r\n" + " \"disk_failure_status\": \"0\",\r\n" + " \"disk_selftest_failure_count\": \"0\",\r\n" + " \"disk_glist_count\": \"0\",\r\n" + " \"disk_bms_count\": \"0\",\r\n" + " \"disk_temperature_current\": \"30\",\r\n" + " \"disk_temperature_reference\": \"85\",\r\n" + " \"r_ecsd\": \"0\",\r\n" + " \"r_ecpd\": \"0\",\r\n" + " \"r_operations\": \"0\",\r\n" + " \"r_ec\": \"0\",\r\n" + " \"r_times\": \"1019321\",\r\n" + " \"r_processed\": \"8443956112248\",\r\n" + " \"r_uce\": \"0\",\r\n" + " \"w_ecsd\": \"0\",\r\n" + " \"w_ecpd\": \"0\",\r\n" + " \"w_operations\": \"0\",\r\n" + " \"w_ec\": \"0\",\r\n" + " \"w_times\": \"158125\",\r\n" + " \"w_processed\": \"10701100018960\",\r\n" + " \"w_uce\": \"0\",\r\n" + " \"v_ecsd\": \"0\",\r\n" + " \"v_ecpd\": \"8\",\r\n" + " \"v_operations\": \"0\",\r\n" + " \"v_ec\": \"8\",\r\n" + " \"v_times\": \"1904211\",\r\n" + " \"v_processed\": \"99871936124480\",\r\n" + " \"v_uce\": \"0\",\r\n" + " \"disk_ie_asc\": \"0\",\r\n" + " \"disk_ie_ascq\": \"0\",\r\n" + " \"disk_dha_logic_status\": null,\r\n" + " \"disk_dha_link_status\": null,\r\n" + " \"slow_type\": null,\r\n" + " \"m_ip\": \"10.128.241.145\",\r\n" + " \"created_dt\": \"2019-12-06T04: 13: 44\",\r\n" + " \"dha_log\": [],\r\n" + " \"interfacetype\": \"SAS\"\r\n" + "}]"; @SuppressWarnings("deprecation") class SSLClient extends DefaultHttpClient { public SSLClient() throws Exception{ super(); SSLContext ctx = SSLContext.getInstance("TLS"); X509TrustManager tm = new X509TrustManager() { @Override public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override public X509Certificate[] getAcceptedIssuers() { return null; } }; ctx.init(null, new TrustManager[]{tm}, null); SSLSocketFactory ssf = new SSLSocketFactory(ctx,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); ClientConnectionManager ccm = this.getConnectionManager(); SchemeRegistry sr = ccm.getSchemeRegistry(); sr.register(new Scheme("https", 443, ssf)); } } @SuppressWarnings("resource") public String doPost(String url,String jsonstr,String charset){ HttpClient httpClient = null; HttpPost httpPost = null; String result = null; try{ httpClient = new SSLClient(); httpPost = new HttpPost(url); httpPost.addHeader("Content-Type", "application/json"); httpPost.addHeader("X-Auth-Token",token); StringEntity se = new StringEntity(jsonstr); se.setContentType("application/json"); httpPost.setEntity(se); HttpResponse response = httpClient.execute(httpPost); if(response != null){ HttpEntity resEntity = response.getEntity(); if(resEntity != null){ result = EntityUtils.toString(resEntity,charset); } } }catch(Exception ex){ ex.printStackTrace(); } return result; } public static void main(String[] args){ String request_data = "{\r\n" + " \"taskId\":" + taskId + ",\r\n" + " \"userId\":" + '"'+ userName + '"'+ ",\r\n" + " \"data\":" + data + "\r\n" + " }"; String httpOrgCreateTestRtn = new DiskPredictClient().doPost(url, request_data, "utf-8"); System.out.println(httpOrgCreateTestRtn); } }
父主题: 硬盘故障预测接口
