【发布时间】:2012-09-14 10:59:17
【问题描述】:
我需要在我的 Web 应用程序中加载外部 url 的内容。
我用 HttpsUrlConnection 和 HttpCliente 试过了,我忘记了,但是相对 URL 有问题,因为它不起作用。
如果我的webapp是http://example1.com,我尝试对http://external.com的内容收费,则http://external.com的相对url,例如/images/g.jpg,试图在http://example1.com/images/g.jpg解决。
我很绝望,我在寻找谷歌,但我一无所获。
对不起,我的英语不好。
谢谢!!! :-)
P.D:有我的代码(在代码中,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);
}
}
【问题讨论】:
-
您能否在问题中添加示例代码以了解您在尝试什么?
-
我编辑我的问题g添加代码,谢谢!!
-
在上面使用格式化按钮。代码按钮在您的文本前放置 4 个空格。这意味着一个代码块。
-
一件事:是原始链接,还是相对链接? (如果链接是绝对的,那么您的结果将是
oldhost.com/newhost.com/dir:) 如果是,您需要转换它们吗?经验法则是尝试在main方法中替换字符串,这是一种非正式测试。硬编码您从外部服务器收到的文本并进行您想要的替换。输出结果。只有当它有效时,才能尝试进入你的真实应用。