在使用Chrome打开项目网站测试时,会经常显示您的连接不是私密连接,例如:
在浏览器里,如果想继续浏览,可以点击高级,并继续前往不安全网页即可。
如果直接使用Postman测试,第一次调用时总是会返回403,多试几次后就可以正常返回。应该是将网站的证书下载到了本地cache中,就可以正常访问了。
那么如何写测试函数呢? 这里使用 Java 来测试获取服务器上的资源,并操作资源。
Https握手并接受信任的证书:
SSLContext sc = SSLContext.getInstance("TLSv1.2");
sc .init(null, new TrustManager[] { new X509TrustManager(){
@ Override
public void checkClientTrusted(X509 cert ificate[] arg0, String arg1) throws CertificateException {}
@Override
public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
}}, new SecureRandom());
具体实例:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.List;
import java.util.Map;
import java.util.concurrent. executor Service;
import java.util.concurrent.Executors;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.wink. json 4j.JSONArray;
import org.apache.wink.json4j.JSONObject;
public class ScaleUpdateLXCATest {
// Common Properties.
private static String CONTENT_TYPE_UTF8 = "application/json; charset=UTF-8";
public static void main(String [] args) {
try {
// User configuration.
String ip = "xxx.xxx.xxx.xxx";
int numOfDevicesinJob = 300;
// Build Connection
URL url = new URL("#34; +ip +"/compliance/applicable?hasPayload=true");
String httpMethod = "GET";
String userpass ="user:password";
String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userpass.getBytes());
//SSLContext sc = SSLContext.getInstance("TLS");
SSLContext sc = SSLContext.getInstance("TLSv1.2");
sc.init(null, new TrustManager[] { new X509TrustManager(){
@Override
public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {}
@Override
public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {}
@Override
public X509Certificate[] get Accept edIssuers() {
return null;
}
}}, new SecureRandom());
//}}, SecureRandom.getInstance("SHA2DRBG", "IBMJCEFIPS"));
HttpsURLConnection uc = (HttpsURLConnection) url.openConnection();
uc.setRequestProperty("Authorization", basicAuth);
uc.setSSLSocketFactory(sc.getSocketFactory());
uc.setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String arg0, SSLSession arg1) {
return true;
}
});
uc.setDoOutput(true);
uc.setRequestMethod(httpMethod);
uc.setRequestProperty("Accept", CONTENT_TYPE_UTF8);
uc.setRequestProperty("Content-Type", CONTENT_TYPE_UTF8);
uc.setConnectTimeout(10000);
System.out.println("try to connect ...");
System.out.println("URL : " +uc.getURL());
uc.connect();
// Fetch resouce from server.
int rc= uc.getResponseCode();
System.out.println("rc = :" + rc);
System.out.println("Request prop: " + uc.getRequestProperty("Accept"));
Map<String, List<String>> headerFields = uc.getHeaderFields();
System.out.println("header fields are: " + headerFields);
JSONObject json = new JSONObject();
JSONArray jsonArray = new JSONArray();
if (rc == 200) {
BufferedReader br = new BufferedReader(new InputStreamReader(uc.getInputStream(), "UTF-8"));
StringBuilder sb = new StringBuilder();
String nextLine = br.readLine();
while (nextLine != null) {
System.out.println(nextLine);
sb.append(nextLine);
nextLine = br.readLine();
}
if (sb.length() > 0) {
if(sb. toString ().startsWith("[")){
System.out.println("This is a array json: {}" + sb.toString() );
jsonArray = new JSONArray(sb.toString());
}else{
json = new JSONObject(sb.toString());
}
}
} else {
System.out.println("URL call failed with rc " + rc);
}
System.out.println("Done");
jsonArray = (JSONArray) json.get("componentTypes");
int count = 0;
// According to the resources, start process for each resource.
ExecutorService executor = Executors.newFixedThreadPool(5);
for (int m=0; m <jsonArray.size(); m++) {
System.out.println("========Start==== : " + count);
if (count >= numOfDevicesinJob) {
System.out.println("Summary job number : " + count);
break;
}
JSONObject obj = jsonArray.getJSONObject(m);
String uuid = obj.getString("uuid");
JSONArray components = new JSONArray();
JSONArray applicableFirmware = obj.getJSONArray("applicableFirmware");
for(int n = 0; n < applicableFirmware.size(); n++) {
JSONObject subObj = applicableFirmware.getJSONObject(n);
JSONArray available = subObj.getJSONArray("available");
if (available.size() > 0) {
JSONObject component = new JSONObject();
component.put("Component", subObj.getString("name"));
component.put("Fixid", available.getJSONObject(0).getString("fixid"));
components.put(component);
}
}
if (components.size() <= 0) {
continue ;
}
JSONObject desObj = new JSONObject();
desObj.put("UUID", uuid);
desObj.put("Components", components);
JSONArray deviceList = new JSONArray();
JSONArray serverList = new JSONArray();
serverList.add(desObj);
JSONObject serverObj = new JSONObject();
serverObj.put("ServerList", serverList);
deviceList.add(serverObj);
JSONObject updateRequest = new JSONObject();
updateRequest.put("DeviceList", deviceList);
System.out.println("updateRequest: " + updateRequest.toString());
final JSONObject request = updateRequest;
executor.submit(new Runnable(){
@Override
public void run() {
try {
URL url = new URL("#34;+ip+"/updatableComponents?type=updates&activationMode=immediate&simulationMode=true");
HttpsURLConnection uc = (HttpsURLConnection) url.openConnection();
String httpMethod = "PUT";
uc = (HttpsURLConnection) url.openConnection();
uc.setRequestProperty("Authorization", basicAuth);
uc.setSSLSocketFactory(sc.getSocketFactory());
uc.setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String arg0, SSLSession arg1) {
return true;
}
});
uc.setDoOutput(true);
uc.setRequestMethod(httpMethod);
uc.setRequestProperty("Accept", CONTENT_TYPE_UTF8);
uc.setRequestProperty("Content-Type", CONTENT_TYPE_UTF8);
uc.setConnectTimeout(10000);
byte[] outputData = request.toString().getBytes();
if (outputData != null && (httpMethod.equals("POST") || httpMethod.equals("PUT"))) {
OutputStream wr = uc.getOutputStream();
wr.write(outputData);
wr.flush();
uc.connect();
int rc= uc.getResponseCode();
System.out.println("Response Code :" + rc);
}
} catch (Exception e) {
}
}
});
count++;
System.out.println("Complete job");
}
System.out.println("Summary job number : " + count);
executor.shutdown();
}catch (Exception ex){
ex.printStackTrace();
}
}
}