我需要在我的 Web 应用程序中加载外部 url 的内容。
我用 HttpsUrlConnection 和 HttpCliente 试过了,我忘记了,但是相对 URL 有问题,因为它不起作用。
如果我的 webapp 是http://example1.com
并且我尝试对 的内容收费,例如,http://external.com
的相对 url正在尝试解决。http://external.com
/images/g.jpg
http://example1.com/images/g.jpg
我很绝望,我寻找谷歌,但我什么也没找到。
我很抱歉我的英语不好。
谢谢!!!:-)
PD:有我的代码(在代码中 helios 说关于将相对 url 更改为绝对 url,但它不起作用......)
codigoHtml 具有带有相关链接的 html 代码,它不起作用!
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(ClientPNames.DEFAULT_HOST, new HttpHost("host_to_redirect"));
HttpPost httpPost = new HttpPost("host_to_redirect/action.do");
httpPost.addHeader("Location", "host_to_redirect");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("name", "value"));
nameValuePairs.add(new BasicNameValuePair("name", "value"));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpResponse = httpclient.execute(httpPost);
httpResponse.addHeader("Location", "host_to_redirect");
HttpEntity entity = httpResponse.getEntity();
System.out.println("----------------------------------------");
System.out.println(httpResponse.getStatusLine());
System.out.println("----------------------------------------");
if (entity != null) {
// System.out.println(EntityUtils.toString(entity));
response.setHeader("Location", "https://prepliberty.tirea.es:8065/pliberty");
response.addHeader("Location", "https://prepliberty.tirea.es:8065/pliberty");
String codigoHtml = EntityUtils.toString(entity);
codigoHtml = codigoHtml.replaceAll("plib_script/", "host_to_redirect/plib_script/");
codigoHtml = codigoHtml.replaceAll("plib_css/", "host_to_redirect/plib_css/");
codigoHtml = codigoHtml.replaceAll("plib_images/", "host_to_redirect/plib_images/");
response.getWriter().write(codigoHtml);
}
}