import java.io.IOException;
import java.io.InputStream;
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.HttpStatus;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import com.eidpay.util.RegexUtil;
import com.intelac.fft.fftsp.intf.dto.RespMsg;
public class HttpsPost {
public static RespMsg doPost(String httpsUrl, String json) throws Exception{
String response = HttpsPost.post(httpsUrl, json);
RespMsg respMsg = RegexUtil.getRespMsg(response);
if(RegexUtil.verifySign(respMsg)){
System.out.println("签名校验正确");
return respMsg;
}
else {
System.out.println("签名数据不正确");
return null;
}
}
/**
* 发送请求.
*
* @param httpsUrl
* 请求的地址
* @param json
* 请求的数据
*/
public static String post(String httpsUrl, String json) throws Exception {
// logger.info(logSeq, "请求到达http客户端,请求地址:" + url);
System.out.println("请求到达http客户端,请求地址:" + httpsUrl);
String str = "";
if (httpsUrl.toLowerCase().startsWith("https")) {
SSLContext sslcontext = SSLContext.getInstance("TLS");
X509TrustManager trustManager = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
// Now put the trust manager into an SSLContext.
sslcontext.init(null, new TrustManager[] { trustManager }, null);
SSLSocketFactory sslSocFactory = new SSLSocketFactory(sslcontext,
SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 15000);
HttpConnectionParams.setSoTimeout(httpParams, 15000);
DefaultHttpClient httpsClient = new DefaultHttpClient(httpParams);
httpsClient.getConnectionManager().getSchemeRegistry()
.register(new Scheme("https", 443, sslSocFactory));
httpsClient
.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(
3, false));
HttpPost postMethod = new HttpPost(httpsUrl);
System.out.println("设置请求内容:" + json);
postMethod.setHeader("Content-Type",
"application/json;charset=UTF-8");
StringEntity reqEntity = new StringEntity(json, "application/json",
"UTF-8");
postMethod.setEntity(reqEntity);
InputStream in = null;
HttpEntity repsEntity = null;
int statusOK = HttpStatus.SC_OK;
try {
HttpResponse response = httpsClient.execute(postMethod);
int statusCode = response.getStatusLine().getStatusCode();
repsEntity = response.getEntity();
in = repsEntity.getContent();
int count = 0;
byte[] b = new byte[1024];
byte word;
while ((word = (byte) in.read()) != -1) {
if (count == b.length) {
byte temp[] = new byte[b.length + 1024];
System.arraycopy(b, 0, temp, 0, b.length);
b = temp;
}
b[count++] = (byte) word;
}
str = new String(b, "UTF-8").trim();
if (statusCode != statusOK) {
// throw new ServiceInvokeException(statusCode);
System.out.println("调用第三方服务返回失败,失败代号:" + statusCode
+ ";返回的错误信息:" + str);
}
System.out.println("成功" + str);
}
// catch (ServiceInvokeException e){
// logger.error(logSeq, "调用第三方服务返回失败,失败代号:" + e.getStatusCode()
// +";返回的错误信息:" + str);
// throw e;
// }
catch (IOException e) {
throw e;
} catch (IllegalArgumentException e) {
throw e;// 不规范的url
} catch (Exception e) {
throw e;
} finally {
try {
if (in != null) {
in.close();
}
httpsClient.getConnectionManager().shutdown();
} catch (Exception se) {
// logger.error(logSeq, "关闭httpClient时出错," +
// ExceptionUtils.printStackTrace(se));
System.out.println("关闭httpClient时出错,");
}
}
}
return str;
}
}
所需jar包
sommons-logging-1.1.jar
httpclient-4.1.3.jar
httpcore-4.1.4.jar
json.org.jar