【问题标题】:trying to charge content of a external url with relative url in java试图用java中的相对url对外部url的内容收费
【发布时间】: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 方法中替换字符串,这是一种非正式测试。硬编码您从外部服务器收到的文本并进行您想要的替换。输出结果。只有当它有效时,才能尝试进入你的真实应用。

标签: java http url


【解决方案1】:

您尝试执行的操作类似于 mod_rewrite module for Apache 所做的操作。

它基本上必须重写提供的 URL。没有灵丹妙药。所以我应该建议 - 如果内容不是很复杂 - 将内容作为字符串抓取并进行替换(或多个)。

类似:

String html = ...content from URL... //beware of encoding!!! a lot of programmers neglect this!
html = html.replace(OLD_PREFIX, NEW_PREFIX);
// now you can use html

OLD_PREFIX 可以是http://external.com/,NEW_PREFIX 可以是http://example1.com/

您可以考虑到 URL 总是以双引号 " 开头,因此前缀可以包括以 " 开头的前缀。当然……可能会有错位……

【讨论】:

  • 而且你的英语还不错。至少对于写这个问题:)
  • 感谢您的回答赫利俄斯!!我试过了(更改网址)但它仍然不起作用,当我看到源代码时,网址很好,但它不起作用,我想也许我必须在发送之前更改一些标题请求,我试图寻找这个,但我什么也没找到! P.D:谢谢你的英语:-)
  • 当您(作为浏览器客户端)收到页面时...它写得好吗?我的意思是:它是否包含&lt;a href="http://thegoodserver.com/page"&gt;...
猜你喜欢
  • 1970-01-01
  • 2011-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-03
相关资源
最近更新 更多