//客户端代码,用来测试访问Servlet:
package com.abin.lee.ssh.basic.https;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.security.KeyStore;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
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.client.params.AllClientPNames;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.junit.Test;
public class GetUnionpayMessageTest {
private static final String HTTPURL = "https://124.288.188.28:1443/stsf/deal/unionpay/UnionPayDeal";
@Test
public void testGetMessageState() throws Exception {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost =new HttpPost(HTTPURL);
httpClient.getParams().setParameter(AllClientPNames.PROTOCOL_VERSION,HttpVersion.HTTP_1_1);
httpClient.getParams().setParameter(AllClientPNames.USE_EXPECT_CONTINUE, Boolean.TRUE);
httpClient.getParams().setParameter(AllClientPNames.HTTP_CONTENT_CHARSET,"UTF_8");
httpClient.getParams().setParameter(AllClientPNames.CONN_MANAGER_TIMEOUT, 10000l);
httpClient.getParams().setParameter(AllClientPNames.CONNECTION_TIMEOUT, 2000);
httpClient.getParams().setParameter(AllClientPNames.SO_TIMEOUT, 10000);
try {
KeyStore trustStore = KeyStore.getInstance(KeyStore
.getDefaultType());
FileInputStream instream = new FileInputStream(new File(
"D:/tomcat.keystore"));
try {
trustStore.load(instream, "longcode".toCharArray());
} finally {
try {
instream.close();
} catch (Exception ignore) {
}
}
SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);
Scheme sch = new Scheme("https", 1443, socketFactory);
httpClient.getConnectionManager().getSchemeRegistry().register(sch);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("userName", "abin"));
nvps.add(new BasicNameValuePair("createTime", new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss").format(new java.util.Date())));
HttpResponse httpResponse = null;
String result = "";
try {
httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
httpResponse = httpClient.execute(httpPost);
BufferedInputStream buffer = new BufferedInputStream(
httpResponse.getEntity().getContent());
byte[] bytes = new byte[1024];
int line = 0;
StringBuilder builder = new StringBuilder();
while ((line = buffer.read(bytes)) != -1) {
builder.append(new String(bytes, 0, line, "UTF-8"));
}
result = builder.toString();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (httpPost.isAborted()) {
httpPost.abort();
}
httpClient.getConnectionManager().shutdown();
}
System.out.println("result=" + result);
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpClient.getConnectionManager().shutdown();
}
}
}
//在%TOMCAT_HOME%/conf/server.xml里面添加的内容
<Connector port="1443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="false" keystoreFile="D:/tomcat.keystore"
keystorePass="longcode" sslProtocol="TLS" />
//UnionPayDeal.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author Administrator
*
*/
public class UnionPayDeal extends HttpServlet {
private static final long serialVersionUID = -1370581177759574628L;
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException{
System.out.println("接收到银联的异步通知");
String accept=null;
BufferedReader reader=new BufferedReader(new InputStreamReader(request.getInputStream()));
int num=0;
char[] buffer=new char[1024];
while((num=reader.read(buffer))!=-1){
accept=new String(buffer,0,num);
System.out.println("accept="+accept);
}
}
public void destroy() {
super.destroy();
}
}