You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Suneet Shah <su...@gmail.com> on 2009/02/23 07:00:25 UTC

Problem loading HTML Page with relative URLs

Hello:

I am new to HttpClient and am trying to develop some proxy functionality 
for my application.  I am using the code below to load a url and then 
render it in the browser.
However, all if a page has relative urls, they all point to by proxy 
application, and not the application where they actually live. The 
result is that all the links are broken and none of the images, 
stylesheets, etc are loaded. 

How can I get around this? Any help will be appreciated.

Thanks
Suneet


        // get the request url
        // this url goes through the proxy
        // ie. http//localhost/myapp
        String uri = getURI((HttpServletRequest)request);
       
        // example 1 - load the contents of the
        HttpMethod method = new GetMethod("http://myhost.com/myapp");

        method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                new DefaultHttpMethodRetryHandler(3, false));
        method.setFollowRedirects(true);

            // Execute the method.
            int statusCode = httpClient.executeMethod(method);
           
            System.out.println("status - " + statusCode);

            if (statusCode != HttpStatus.SC_OK) {
              System.err.println("Method failed: " + 
method.getStatusLine());
            }

           
            InputStream streamFromServer = method.getResponseBodyAsStream();
            PrintWriter pw = response.getWriter();
            DataInputStream inStream = new 
DataInputStream(streamFromServer);
            String inputLine;

            while ((inputLine = inStream.readLine()) != null) {
                pw.println(inputLine);
            }
            inStream.close();
            pw.flush();


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: Problem loading HTML Page with relative URLs

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Mon, 2009-02-23 at 01:00 -0500, Suneet Shah wrote:
> Hello:
> 
> I am new to HttpClient and am trying to develop some proxy functionality 
> for my application.  I am using the code below to load a url and then 
> render it in the browser.
> However, all if a page has relative urls, they all point to by proxy 
> application, and not the application where they actually live. The 
> result is that all the links are broken and none of the images, 
> stylesheets, etc are loaded. 
> 
> How can I get around this? Any help will be appreciated.
> 

Your proxy should either handle those requests by retrieving content
from the origin server or by rewriting relative urls in the HTML content
to point at the origin server

Oleg


> Thanks
> Suneet
> 
> 
>         // get the request url
>         // this url goes through the proxy
>         // ie. http//localhost/myapp
>         String uri = getURI((HttpServletRequest)request);
>        
>         // example 1 - load the contents of the
>         HttpMethod method = new GetMethod("http://myhost.com/myapp");
> 
>         method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
>                 new DefaultHttpMethodRetryHandler(3, false));
>         method.setFollowRedirects(true);
> 
>             // Execute the method.
>             int statusCode = httpClient.executeMethod(method);
>            
>             System.out.println("status - " + statusCode);
> 
>             if (statusCode != HttpStatus.SC_OK) {
>               System.err.println("Method failed: " + 
> method.getStatusLine());
>             }
> 
>            
>             InputStream streamFromServer = method.getResponseBodyAsStream();
>             PrintWriter pw = response.getWriter();
>             DataInputStream inStream = new 
> DataInputStream(streamFromServer);
>             String inputLine;
> 
>             while ((inputLine = inStream.readLine()) != null) {
>                 pw.println(inputLine);
>             }
>             inStream.close();
>             pw.flush();
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org