httpclient:通过 https 将 HttpResponse 复制到 HttpServletResponse
httpclient: Copy HttpResponse to HttpServletResponse over https
我有以下代码将 httpresponse 转换为 httpservletresponse,如果通过 http URL 访问服务器,它工作得很好,但它不适用于 https。
问题是如果 URL 是 HTTPs,我从 httpservletresponse 得到空字符串。
public void extractResponse(HttpResponse httpResponse, HttpServletResponse response)
{
InputStream inputStream = null;
try {
inputStream = httpResponse.getEntity().getContent();
String responseStr = EntityUtils.toString(httpResponse.getEntity()); //Get the contect from httpresponse, it has the value I want
copyStream(inputStream, response.getOutputStream());
} catch (IllegalStateException | IOException e) {
}
finally{
if(inputStream != null)
{
try {
inputStream.close();
} catch (IOException e) {
}
}
}
}
/**
* Wrapper for IOUtils.copy
* @param input
* @param output
* @throws IOException
*/
public void copyStream(InputStream input, OutputStream output) throws IOException
{
IOUtils.copy(input, output);
}
仅供参考,我在两台服务器上都使用 tomcat,httpclient 的版本是 4.13。
我自己弄明白了,这个问题是由于两个服务器之间的 HTTP Schema 不一致造成的。
例如,如果您在服务器 A 中使用 HTTPClient 创建 HTTP 请求,并且请求将发送到服务器 B,如果您使用的是 HTTP 模式(URL 将是 'http://example.com') 要访问服务器 A,您必须在创建 HTTP 请求时使用相同的 HTTP 模式,另一方面,如果您在服务器 A 中使用 HTTPS,则必须使用 HTTPS 访问服务器 B,否则,它永远不会起作用。
一旦您确认了一致的 HTTP 模式,我在问题中发布的代码就会起作用。
我有以下代码将 httpresponse 转换为 httpservletresponse,如果通过 http URL 访问服务器,它工作得很好,但它不适用于 https。
问题是如果 URL 是 HTTPs,我从 httpservletresponse 得到空字符串。
public void extractResponse(HttpResponse httpResponse, HttpServletResponse response)
{
InputStream inputStream = null;
try {
inputStream = httpResponse.getEntity().getContent();
String responseStr = EntityUtils.toString(httpResponse.getEntity()); //Get the contect from httpresponse, it has the value I want
copyStream(inputStream, response.getOutputStream());
} catch (IllegalStateException | IOException e) {
}
finally{
if(inputStream != null)
{
try {
inputStream.close();
} catch (IOException e) {
}
}
}
}
/**
* Wrapper for IOUtils.copy
* @param input
* @param output
* @throws IOException
*/
public void copyStream(InputStream input, OutputStream output) throws IOException
{
IOUtils.copy(input, output);
}
仅供参考,我在两台服务器上都使用 tomcat,httpclient 的版本是 4.13。
我自己弄明白了,这个问题是由于两个服务器之间的 HTTP Schema 不一致造成的。
例如,如果您在服务器 A 中使用 HTTPClient 创建 HTTP 请求,并且请求将发送到服务器 B,如果您使用的是 HTTP 模式(URL 将是 'http://example.com') 要访问服务器 A,您必须在创建 HTTP 请求时使用相同的 HTTP 模式,另一方面,如果您在服务器 A 中使用 HTTPS,则必须使用 HTTPS 访问服务器 B,否则,它永远不会起作用。
一旦您确认了一致的 HTTP 模式,我在问题中发布的代码就会起作用。