我正在向安全网站发送发布请求。当我从网络上这样做时
<body>
<form method=POST action= "https://www.abc.com" >
<textarea name="Request" rows="30%" cols="80%"></textarea>
<br>
<br>
<br>
<input type="Submit">
</form>
</body>
我做什么,我将 xml 粘贴到 textares 中并提交表单并获得响应。美好的。现在,当我尝试从普通 java 做同样的事情时,我得到了证书问题
sun.security.validator.ValidatorException: PKIX path building failed:
sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid
certification path to requested target
如果我绕过 ssl,我会得到响应systemError
。为什么 ?
这是我在做什么
@Override
public HttpsURLConnection getHttpsConnection() throws Exception {
HttpsURLConnection urlConnection = null;
// Create a trust manager that does not validate certificate chains
final TrustManager[] trustAllCerts = new TrustManager[] {
new X509TrustManager() {
public void checkClientTrusted( final X509Certificate[] chain, final String authType ) {
}
public void checkServerTrusted( final X509Certificate[] chain, final String authType ) {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
}
} ;
// Install the all-trusting trust manager
final SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
// Create all-trusting host name verifier
HostnameVerifier allHostsValid = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
// Install the all-trusting host verifier
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
try {
URL myUrl = new URL(this.url);
urlConnection = (HttpsURLConnection) myUrl.openConnection();
} catch (Exception e) {
throw new Exception("Error in getting connecting to url: " + url + " :: " + e.getMessage());
}
return urlConnection;
} //end of getHttpsConnection()
private void processHttpRequest(HttpsURLConnection connection, String method, Map<String, String> params) throws Exception {
StringBuffer requestParams = new StringBuffer();
if (params != null && params.size() > 0) {
Iterator<String> paramIterator = params.keySet().iterator();
while (paramIterator.hasNext()) {
String key = paramIterator.next();
String value = params.get(key);
requestParams.append(URLEncoder.encode(key, "UTF-8"));
requestParams.append("=").append(URLEncoder.encode(value, "UTF-8"));
requestParams.append("&");
}
}
try {
connection.setUseCaches(false);
connection.setDoInput(true);
if ("POST".equals(method)) {
// set request method to POST
connection.setDoOutput(true);
} else {
// set request method to GET
connection.setDoOutput(false);
}
String parameters = requestParams.toString();
if ("POST".equals(method) && params != null && params.size() > 0) {
OutputStream os = connection.getOutputStream();
DataOutputStream wr = new DataOutputStream(os);
wr.writeBytes(URLEncoder.encode(parameters, "UTF-8"));
wr.writeBytes(parameters);
wr.flush();
wr.close();
/**
*
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.write(requestParams.toString());
writer.flush();
writer.close();
*/
}
// reads response, store line by line in an array of Strings
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
List<String> response = new ArrayList<String>();
String line = "";
while ((line = reader.readLine()) != null) {
response.add(line);
}
reader.close();
String[] myResponse = (String[]) response.toArray(new String[0]);
if (myResponse != null && myResponse.length > 0) {
System.out.println("RESPONSE FROM: " + this.url);
for (String myLine : response) {
System.out.println(myLine);
}
}
} catch(Exception e) {
throw new Exception("Error in sending Post :: " + e.getMessage());
}
}
我怎么称呼它
String req = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
req = req + "<abc>";
req = req + "
....
req = req + " </xyz>";
req = req + "</abc>";
StringBuffer buffer = new StringBuffer();
buffer.append(req);
HttpServices httpServices = context.getBean("httpService", HttpServices.class);
String method = "POST";
Map<String, String> params = new HashMap<String, String>();
params.put("request", buffer.toString());
try {
HttpsURLConnection connection = httpServices.getHttpsConnection();
httpServices.processHttpRequest(connection, method, params);
} catch (Exception e) {
System.out.println(e.getMessage());
}
我得到这样的回应。当然,我无法显示确切的回复。但它看起来像这样。
<?xml version="1.0" encoding="UTF-8"?>
<Result>
<SystemError>
<Message>87b24972</Message>
</SystemError>
</Result>
为什么我从纯 java 得到不同的响应?我也通过 SSL 对 Apache http 客户端进行了同样的尝试,但是从 Apache 客户端我得到了相同的响应。
谢谢。