|
@@ -7,8 +7,16 @@ import java.io.InputStreamReader;
|
|
|
import java.io.OutputStream;
|
|
|
import java.net.HttpURLConnection;
|
|
|
import java.net.URL;
|
|
|
+import java.security.cert.CertificateException;
|
|
|
+import java.security.cert.X509Certificate;
|
|
|
import java.util.Map;
|
|
|
|
|
|
+import javax.net.ssl.HttpsURLConnection;
|
|
|
+import javax.net.ssl.SSLContext;
|
|
|
+import javax.net.ssl.SSLSocketFactory;
|
|
|
+import javax.net.ssl.TrustManager;
|
|
|
+import javax.net.ssl.X509TrustManager;
|
|
|
+
|
|
|
import net.sf.json.JSONObject;
|
|
|
|
|
|
public class HttpUtil {
|
|
@@ -60,14 +68,15 @@ public class HttpUtil {
|
|
|
*/
|
|
|
public String httpAction(Map<String, String> params, String datas) {
|
|
|
String result = null;
|
|
|
- HttpURLConnection conn = null;
|
|
|
+ HttpsURLConnection conn = null;
|
|
|
OutputStream os = null;
|
|
|
InputStream is = null;
|
|
|
|
|
|
try {
|
|
|
+
|
|
|
// 获取链接
|
|
|
URL url = new URL(uri);
|
|
|
- conn = (HttpURLConnection) url.openConnection();
|
|
|
+ conn = (HttpsURLConnection) url.openConnection();
|
|
|
|
|
|
long timestamp = System.currentTimeMillis();
|
|
|
|
|
@@ -87,6 +96,28 @@ public class HttpUtil {
|
|
|
conn.setRequestProperty(ROOT_ORG_ID, rootOrgId);
|
|
|
// 设置时间
|
|
|
conn.setRequestProperty(TIMESTAMP, String.valueOf(timestamp));
|
|
|
+ // ssl
|
|
|
+ SSLContext context = SSLContext.getInstance("SSL", "SunJSSE");
|
|
|
+ TrustManager[] tm = new TrustManager[] { new X509TrustManager() {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public X509Certificate[] getAcceptedIssuers() {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ } };
|
|
|
+ // 初始化
|
|
|
+ context.init(null, tm, new java.security.SecureRandom());
|
|
|
+ // 获取SSLSocketFactory对象
|
|
|
+ SSLSocketFactory ssf = context.getSocketFactory();
|
|
|
+ conn.setSSLSocketFactory(ssf);
|
|
|
|
|
|
conn.setUseCaches(false);
|
|
|
conn.setDoOutput(true);
|
|
@@ -108,7 +139,7 @@ public class HttpUtil {
|
|
|
}
|
|
|
|
|
|
result = getResult(conn);
|
|
|
- } catch (IOException e) {
|
|
|
+ } catch (Exception e) {
|
|
|
// 操作失败
|
|
|
// log.error("Http connection error!", e);
|
|
|
e.printStackTrace();
|
|
@@ -191,9 +222,14 @@ public class HttpUtil {
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
JSONObject json = new JSONObject();
|
|
|
- json.accumulate("examId", 2);
|
|
|
- json.accumulate("subjectCode", "ldyCos001");
|
|
|
-
|
|
|
+ json.accumulate("examId", 5);
|
|
|
+ String test = "https://ecs-test.ea100.com.cn/api/exchange/outer/question/getSubjectivePaperStruct";
|
|
|
+ String dev = "https://dev.ea100.com.cn/api/exchange/outer/question/getSubjectivePaperStruct";
|
|
|
+ HttpUtil subjectHttp = new HttpUtil(dev, "123456", "11", "0");
|
|
|
+ String subjectJson = subjectHttp.httpAction(null, json.toString());
|
|
|
+ System.out.println(subjectJson);
|
|
|
+
|
|
|
+ // json.accumulate("subjectCode", "ldyCos001");
|
|
|
// HttpUtil subjectHttp = new HttpUtil(
|
|
|
// "http://iepcc-ps.ecs.qmth.com.cn:80/api/exchange/outer/question/getSubjectivePaperStruct",
|
|
|
// "Fu234234CK!fsd9f0", "EPCC", "16923");
|
|
@@ -208,7 +244,7 @@ public class HttpUtil {
|
|
|
|
|
|
// long start = System.currentTimeMillis();
|
|
|
// HttpUtil studentHttp = new HttpUtil(
|
|
|
- // "http://192.168.10.39:8007/api/exchange/outer/question/getSubjectiveQuestion",
|
|
|
+ // "https://192.168.10.39:8007/api/exchange/outer/question/getSubjectiveQuestion",
|
|
|
// "123456", "11", "0");
|
|
|
// json.accumulate("startId", 0);
|
|
|
// json.accumulate("size", 10);
|