我有一个包含Web服务的Web应用程序。该应用程序在安装了SSL证书的tomcat上运行。该应用程序和Web服务正在运行。如果我尝试通过输入以下内容通过浏览器使用Web服务:
https://myapp:8080/myapp/rest/test/hello
我有一个可以正常工作的XML,它是由我的Web服务生成的。现在,我编写了一个Client应用程序,该应用程序试图使用此Web服务,获取XML并对其进行解析。它加载信任库,获取证书,然后建立SSL握手。按设计工作!
但是,通过尝试向Web服务启动GET请求,我收到了一个空响应。我尝试尽可能多地记录日志,并记录了正在发出的请求:
GET https://myapp:8080/myapp/rest/test/hello返回的响应状态为401未经授权
因为我没有得到任何例外,我的问题是,当我通过浏览器而不是通过代码使用Web服务时,为什么Web服务能正常工作?
遵循我的客户使用的代码:
import java.io.FileInputStream;
import java.security.KeyStore;
import java.security.SecureRandom;
import java.util.ArrayList;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;
import javax.ws.rs.core.MediaType;
import org.apache.log4j.Logger;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.client.urlconnection.HTTPSProperties;
import execution_units.SQLTestExecutionUnit;
import execution_units.ScriptTestExecutionUnit;
import execution_units.UnixTestExecutionUnit;
public class WebServiceGET {
/*
* VARIABLES
*/
// Client
private Client clt_output = null;
// ClientConfig
private ClientConfig cnf_output = null;
private SSLContext ctx_ssl = null;
// ClientResponse
private ClientResponse clr_output = null;
// WebResources
private WebResource wre_output = null;
// Booleans for Connections
private boolean boo_output_connection = false;
// Parsing
private ResponseParsingUnit rpu_parser = null;
// Log
private Logger log = null;
/*
* CONSTRUCTOR
*/
public WebServiceGET(String str_server_url, String str_host_name, int int_timeout, String str_truststore_path,
String str_truststore_password, int int_exit_xmlinvalid, int int_exit_notestcases) {
// Add Class to Log4J framework
log = Logger.getLogger("wsg");
log.info("\t\tLog for Web Service GET initialized.");
// Create SSL Configs
try {
ctx_ssl = SSLContext.getInstance("TLS");
KeyStore truststore = KeyStore.getInstance("JKS");
truststore.load(new FileInputStream(str_truststore_path), str_truststore_password.toCharArray());
TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509");
tmf.init(truststore);
ctx_ssl.init(null, tmf.getTrustManagers(), new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(ctx_ssl.getSocketFactory());
cnf_output = new DefaultClientConfig();
cnf_output.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES,
new HTTPSProperties(null, ctx_ssl));
} catch (Exception ex) {
ex.printStackTrace();
}
/* OUTPUT */
// Create client
try {
clt_output = Client.create(cnf_output);
} catch (Exception x) {
log.warn("\tInterrupted while cnf_output created client. Exception: ", x);
}
// Set timeout (read- and connection- receive the same value.)
clt_output.setConnectTimeout(int_timeout * 1000);
clt_output.setReadTimeout(int_timeout * 1000);
// Tireless Connection Establishment to WebService
while (!boo_output_connection) {
// Stitch URL together
try {
log.debug("\tConnection-URL (Output): " + str_server_url);
wre_output = clt_output.resource(str_server_url);
// Set MediaType (XML)
clr_output = wre_output.accept(MediaType.TEXT_XML).get(ClientResponse.class);
log.debug("\toutput status info (Output): " + "\tSTATUS==>" + clr_output.getStatusInfo()
+ "\tStringResponse==>" + clr_output.toString());
// following code is not necessary, because the reponse from server is always empty!
} catch (Exception ex) {
log.warn("\tConnection for GET-Request could not be established! Exception: ", ex);
try {
log.debug("\tTry to reconnect with server in : " + int_timeout + " [s]");
Thread.sleep(int_timeout * 1000);
} catch (Exception ex_sub) {
log.warn("\tInterrupted while sleeping. Exception: ", ex_sub);
}
}
}
}
}