美文网首页
HttpClient 4.5 中转请求到HttpServletR

HttpClient 4.5 中转请求到HttpServletR

作者: else05 | 来源:发表于2017-03-27 12:58 被阅读154次

通过httpclient发送请求到其它资源,把请求响应的内容转给httpServletResponse

public void testData(HttpServletResponse response) {
        HttpResponse httpResponse = null;
        try {
            // 请求外部资源
            httpResponse = Request.Get(url).connectTimeout(30000).execute().returnResponse();
            HttpEntity entity = httpResponse.getEntity();
            // 这里还可以设置响应头
            //  把外部资源响应的内容传到HttpServletResponse中去
            entity.writeTo(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

相关文章

网友评论

      本文标题:HttpClient 4.5 中转请求到HttpServletR

      本文链接:https://www.haomeiwen.com/subject/cxcrottx.html