简体   繁体   中英

trying to charge content of a external url with relative url in java

I need to load content of a external url in my web application.

I tried it with HttpsUrlConnection and HttpCliente, I forget it but I have problems with relative URL because it doesn´t work.

if my webapp is http://example1.com and I try to charge content of http://external.com , the relative url of http://external.com , /images/g.jpg for example, is trying to solve at http://example1.com/images/g.jpg .

I´m desperate, I look for google but I find nothing.

I´m sorry for my bad English.

Thank you!!! :-)

PD: There is my code (in the code is that helios said about change relative url for absolute url, but it doesn´t work...)

codigoHtml has the html code with relative links, it doesn´t work!!

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);
        }   

    }

What you're trying to do is similar to what mod_rewrite module for Apache does.

It basically has to rewrite the URLs provided. There's no magic bullet. So what I should advise -if the content is not very complex- is to grab the content as an String and make a replacement (or several).

Something like:

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 could be http://external.com/ and NEW_PREFIX could be http://example1.com/

You can take into account that URLs are always preceded by a double-quote " so prefixes could include that starting " . Of course... there can be misreplacements...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM