81,122
社区成员




测试类1:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SocketFactory;
import org.apache.http.conn.ssl.SSLSocketFactory;
public class ZTest2 {
public static void main(String[] args) {
// 构造HttpClient的实例
HttpClient httpClient = new HttpClient();
httpClient.getHostConfiguration().setProxy("10.99.60.201",8080);
KeyStore keyStore = getKeyStore("D:/B/eps.keystore", "111111");
System.out.println("getSocketFactory****************");
SocketFactory socketFactory = null;
try {
socketFactory = new SSLSocketFactory(keyStore);
} catch (Exception e) {
e.printStackTrace();
}
// System.out.println("registryScheme****************");
// Scheme scheme = new Scheme("https", socketFactory, 80);
// ((ClientConnectionManager)httpClient.getHttpConnectionManager().).getSchemeRegistry().register(scheme);
// 创建GET方法的实例
GetMethod getMethod = new GetMethod("https://eps.dev.surepush.cn:443/epscu/");
// 使用系统提供的默认的恢复策略
//getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
try {
// 执行getMethod
int statusCode = httpClient.executeMethod(getMethod);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: "
+ getMethod.getStatusLine());
}
// 读取内容
byte[] responseBody = getMethod.getResponseBody();
// 处理内容
System.out.println(new String(responseBody));
} catch (HttpException e) {
// 发生致命的异常,可能是协议不对或者返回的内容有问题
System.out.println("发生致命的异常,可能是协议不对或者返回的内容有问题!");
e.printStackTrace();
} catch (IOException e) {
// 发生网络异常
e.printStackTrace();
} finally {
// 释放连接
getMethod.releaseConnection();
}
}
public static KeyStore getKeyStore(String uri, String storepass) {
System.out.println("getKeyStore****************");
KeyStore keyStore = null;
try {
keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
} catch (KeyStoreException e) {
e.printStackTrace();
System.out.println("Failed to create keystore");
}
FileInputStream fis = null;
try {
fis = new FileInputStream(uri);
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("File read exception");
}
try {
keyStore.load(fis, storepass.toCharArray());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return keyStore;
}
}
public static void main(String[] args) throws Exception {
ZTest test = new ZTest();
//如果和服务器测试,就打开这两行
HttpHost proxy = new HttpHost("10.99.60.201", 8080);
test.getHttpClient().getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, proxy);
KeyStore keyStore = test.getKeyStore("D:/B/eps.keystore", "111111");
System.out.println("getSocketFactory****************");
SocketFactory socketFactory = null;
try {
socketFactory = new SSLSocketFactory(keyStore);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("registryScheme****************");
Scheme scheme = new Scheme("https", socketFactory, 80);
httpClient.getConnectionManager().getSchemeRegistry().register(scheme);
Protocol myhttps = new Protocol("https", new MySecureProtocolSocketFactory(), 443);
Protocol.registerProtocol("https ", myhttps);
// String url = "https://eps.dev.surepush.cn/epscu/log4j";
String url = "https://eps.dev.surepush.cn:443/epscu/";
HttpGet httpPost = new HttpGet(url);
// HttpPost httpPost = test.getHttpPost("http://localhost:8080/epscu/1.0/uploaditems");
// test.addFileBody(new String[] { "volumes" },
// new FileBody[] { new FileBody(new File("D:/D/SVN_EPS/multiuploadClient/src/volumes.xml"))});
//
// test.addContentBody("cpid", new StringBody("900", Charset.forName("utf-8")));
// test.addContentBody("uploadpassword", new StringBody("123456", Charset.forName("utf-8")));
// test.addContentBody("itemtype", new StringBody("0", Charset.forName("utf-8")));
// test.addContentBody("uploadtype", new StringBody("1", Charset.forName("utf-8")));
// test.setEntity(httpPost);
/***********************************************************************/
HttpResponse httpresponse = httpClient.execute(httpPost);
System.out.println("\n-------------------------------------------------------------\n");
HttpEntity httpentity = httpresponse.getEntity();
System.out.println("StatusLine" + httpresponse.getStatusLine());
if (httpentity != null) {
System.out.println("------------------Response content start------------------------");
try {
InputStream in = httpentity.getContent();
BufferedReader br = new BufferedReader(
new InputStreamReader(in));
String str;
while ((str = br.readLine()) != null) {
System.out.println(URLDecoder.decode(str, "utf-8"));
}
} catch (Exception e) {
e.printStackTrace();
}
// String responsesString=EntityUtils.toString(httpentity);
// System.out.println(responsesString);
System.out.println("--------------------Response content end----------------------");
System.out.println("Response Content-Type: "
+ httpentity.getContentType());
try {
httpentity.consumeContent();
} catch (IOException e) {
e.printStackTrace();
}
}
test.closeConnection();
}