java 调用webapi_java一个调用webapi的工具类

package com.hy.fddsvr.utils;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileOutputStream;

import java.sql.Timestamp;

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.List;

import com.fadada.sdk.util.http.SSLClient;

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.NameValuePair;

import org.apache.http.client.HttpClient;

import org.apache.http.client.entity.UrlEncodedFormEntity;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.util.EntityUtils;

public class MyHttpsUtil {

public static final String charset = "UTF-8";

public MyHttpsUtil() {

}

public static String doPost(String url, List params) {

return doPost(url, params, 15000, 35000);

}

public static String doCaPost(String url, List params) {

return doPost(url, params, 5000, 180000);

}

public static String doPost(String url, List params, int connect_time, int timeout) {

HttpClient httpClient = null;

HttpPost httpPost = null;

String var8;

try {

httpClient = new SSLClient();

httpPost = new HttpPost(url);

httpClient.getParams().setParameter("http.connection.timeout", connect_time);

httpClient.getParams().setParameter("http.socket.timeout", timeout);

if (null != params && params.size() > 0) {

UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");

httpPost.setEntity(entity);

}

//ProxyHttpClient.getProxyHttpClient(httpClient);

HttpResponse response = httpClient.execute(httpPost);

if (response == null) {

throw new RuntimeException("HttpResponse is null.");

}

if (response.getStatusLine().getStatusCode() != 200) {

throw new RuntimeException("connect fail. http_status:" + response.getStatusLine().getStatusCode());

}

HttpEntity entity = response.getEntity();

if (null == entity) {

throw new RuntimeException("HttpEntity is null.");

}

var8 = EntityUtils.toString(entity, "UTF-8");

} catch (Exception var17) {

var17.printStackTrace();

throw new RuntimeException(var17);

} finally {

try {

httpClient.getConnectionManager().shutdown();

} catch (Exception var16) {

var16.printStackTrace();

}

}

return var8;

}

public static String doPost(String url, HttpEntity entity) {

HttpClient httpClient = null;

HttpPost httpPost = null;

String var6;

try {

httpClient = new SSLClient();

httpPost = new HttpPost(url);

httpPost.setEntity(entity);

ProxyHttpClient.getProxyHttpClient(httpClient);

HttpResponse response = httpClient.execute(httpPost);

if (response == null) {

throw new RuntimeException("http_response is null.");

}

if (response.getStatusLine().getStatusCode() != 200) {

throw new RuntimeException("connect fail. http_status:" + response.getStatusLine().getStatusCode());

}

HttpEntity respEtity = response.getEntity();

if (null == entity) {

throw new RuntimeException("response http_entity is null.");

}

var6 = EntityUtils.toString(respEtity, "UTF-8");

} catch (Exception var15) {

var15.printStackTrace();

throw new RuntimeException(var15);

} finally {

try {

httpClient.getConnectionManager().shutdown();

} catch (Exception var14) {

var14.printStackTrace();

}

}

return var6;

}

public static String doPostDownload(String path, String url, List params) {

HttpClient httpClient = null;

HttpPost httpPost = null;

try {

httpClient = new SSLClient();

httpPost = new HttpPost(url);

if (null != params && params.size() > 0) {

UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");

httpPost.setEntity(entity);

}

ProxyHttpClient.getProxyHttpClient(httpClient);

HttpResponse response = httpClient.execute(httpPost);

if (response == null) {

throw new RuntimeException("HttpResponse is null.");

}

if (response.getStatusLine().getStatusCode() != 200) {

throw new RuntimeException("connect fail. http_status:" + response.getStatusLine().getStatusCode());

}

HttpEntity entity = response.getEntity();

if (null == entity) {

throw new RuntimeException("HttpEntity is null.");

}

if (!"application/zip".equals(response.getEntity().getContentType().getValue()) && !"application/pdf".equals(response.getEntity().getContentType().getValue())) {

String var37 = EntityUtils.toString(entity, "UTF-8");

return var37;

}

byte[] result = EntityUtils.toByteArray(response.getEntity());

BufferedOutputStream bw = null;

try {

File f = new File(path);

if (f.exists()) {

f.delete();

}

if (!f.getParentFile().exists()) {

f.getParentFile().mkdirs();

}

bw = new BufferedOutputStream(new FileOutputStream(path));

bw.write(result);

} catch (Exception var32) {

throw new RuntimeException(var32);

} finally {

try {

if (bw != null) {

bw.close();

}

} catch (Exception var31) {

var31.printStackTrace();

}

}

} catch (Exception var34) {

var34.printStackTrace();

throw new RuntimeException(var34);

} finally {

try {

httpClient.getConnectionManager().shutdown();

} catch (Exception var30) {

var30.printStackTrace();

}

}

return null;

}

public static String getTimeStamp() {

Timestamp ts = new Timestamp(System.currentTimeMillis());

DateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");

return sdf.format(ts);

}

}

调用方法:

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.NameValuePair;

import org.apache.http.message.BasicNameValuePair;

。。。。。。

//定义参数列表

List urlParameters = new ArrayList<>();

try {

//拼接参数

urlParameters.add(new BasicNameValuePair("doc_title", ADocTitle));

urlParameters.add(new BasicNameValuePair("template_id", ATemplateId));

urlParameters.add(new BasicNameValuePair("contract_id", AContractId));

urlParameters.add(new BasicNameValuePair("font_size", AFontSize));

urlParameters.add(new BasicNameValuePair("font_type", AFontType));

urlParameters.add(new BasicNameValuePair("parameter_map", AParameterMap));

urlParameters.add(new BasicNameValuePair("msg_digest", LMsgDigest));

res = MyHttpsUtil.doPost(url,urlParameters);

}catch (Exception ex){

ex.printStackTrace();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值