更新时间: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 passWord = "xxxxxx";

    private static String taskId = "0";

    private static String url = "xxxxxx";

    private static String data = "{\r\n" + 
		"	\"taskId\": \"1\",\r\n" + 
		"	\"userId\": \"test\",\r\n" + 
		"	\"},\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("Authorization", userName + ":" + passWord);
            httpPost.addHeader("X-ACCESS-ADDRESS", "/diskCheck/v1.0/diskCheck-impl/pythonServer");
            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);


    }
}
分享:

    相关文档

    相关产品

close