POST
URL, PARAMS 分开发送
public class HttpClient {
private static final AllowAllHostnameVerifier HOSTNAME_VERIFIER = new AllowAllHostnameVerifier();
private static X509TrustManager xtm = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
private static X509TrustManager[] xtmArray = new X509TrustManager[] { xtm };
static String paths = "";
static Map<String, String> paramss = null;
static String encodings = "";
// static String bakold = "";
static String RetBak = "";
static boolean isRe = true;
static int isStop = 0;
// 增加捕获超时异常
public static String sendPOSTRequestForInputStream(String path, Map<String, String> params, String encoding) throws ConnectTimeoutException {
paths = path;
paramss = params;
encodings = encoding;
// bakold = "";
RetBak = "";
isRe = true;
isStop = 0;
class acceptThread extends Thread {
Map<String, String> paramsa = null;
public acceptThread(Map<String, String> params) {
this.paramsa = params;
}
InputStream in = null;
private HttpsURLConnection conn = null;
@Override
public void run() {
String bakn = "";
try {
StringBuilder entityBuilder = new StringBuilder("");
if (paramsa != null && !paramss.isEmpty()) {
for (Map.Entry<String, String> entry : paramss.entrySet()) {
entityBuilder.append(entry.getKey()).append('=');
entityBuilder.append(URLEncoder.encode(entry.getValue(), encodings));
entityBuilder.append('&');
}
entityBuilder.deleteCharAt(entityBuilder.length() - 1);
}
byte[] entity = entityBuilder.toString().getBytes();
URL url = new URL(paths);
conn = (HttpsURLConnection) url.openConnection();
if (conn instanceof HttpsURLConnection) {
// Trust all certificates
SSLContext context = SSLContext.getInstance("TLS");
context.init(new KeyManager[0], xtmArray, new SecureRandom());
SSLSocketFactory socketFactory = context.getS