目标:从虚拟机申请网络服务
麻烦: httpClient.execute(request)
环境:
因此,Web服务来自M-Files,一个文档管理系统,并在IIS上运行。显然,我的Web应用程序在tomcat上运行。有两个虚拟机
我如何初始化HttpClient
:
httpClient = new DefaultHttpClient();
httpClient.getParams().setParameter("http.socket.timeout", new Integer(1000));
我已经设置了timeout参数,以确保在execute
冻结时应用程序不会冻结。
我如何创建请求:
private HttpGet createHttpGet(String url, boolean isAuthenticate) throws IOException {
HttpGet get = new HttpGet(url);
if (isAuthenticate)
get.addHeader("X-Authentication", getAuthenticationToken());
return get;
}
我如何执行请求:
private String executeAndReturn(HttpRequestBase request) throws IOException {
logger.info("[MFiles: request] " + request.getURI());
HttpResponse response = httpClient.execute(request);
logger.info("[MFiles] request sent, waiting response");
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String tmp;
StringBuilder builder = new StringBuilder();
while ((tmp = reader.readLine()) != null) {
logger.info("[MFiles] reading response");
builder.append(tmp);
}
tmp = builder.toString();
reader.close();
logger.info("[MFiles: response] " + tmp);
return tmp;
}
我可以通过ping 10.0.2.2
或ping 192.168.56.102
从虚拟机访问主机并在http://10.0.2.2/REST
或http://192.168.56.102/REST
上请求网络服务,但有些请求已冻结,我不明白为什么。此外,如果我在主机上部署我的网络应用程序,一切都没问题!
我怀疑我的VM网络配置有误,但在这种情况下它应该永远不会成功运行,不应该吗?
任何帮助表示赞赏:)