자바 웹 api 를 호출 하 는 도구 클래스
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();
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.