On Sat, 29 Aug 2009 11:45:50 +0200
Oleg Kalnichevski <[email protected]> wrote:
> (1) HttpClient 3.1
>
> HttpClient client = new HttpClient();
>
> ProtocolSocketFactory mysf = new DefaultProtocolSocketFactory();
> Protocol myhttp = new Protocol("http", mysf, 80);
> HostConfiguration hostconf = new HostConfiguration();
> hostconf.setHost("targethost", 80, myhttp);
> HttpState state = new HttpState();
>
> GetMethod httpget = new GetMethod("/use/relative/uris/only");
> try {
> client.executeMethod(hostconf, httpget, state);
>
> if (httpget.getStatusCode() == HttpStatus.SC_OK) {
> System.out.println(httpget.getResponseBodyAsString());
> } else {
> System.out.println("Unexpected failure: " +
> httpget.getStatusLine().toString());
> }
> } finally {
> httpget.releaseConnection();
> }
Thank you, to approach your proposed solution, I thought I gould change
the URI of the HttpMethod and use the setHost function of
HostConfiguration - however, I encountered some trouble when changing
absolute URIs to relative ones (I don't want to change the Methods
initial creation as they happen all over the place, so I've just
written a function to replace the client.execute(method) calls:
private void processHttpMethod(HttpMethod method) throws IOException {
if(protocol != null ) {
try {
/* We set host and our custom protocol */
client.getHostConfiguration().setHost(method.getURI().getHost(),
80, protocol);
System.out.println("Path: "+method.getURI().getPath());
URI relPath = null;
/* create a relative URI */
try {
relPath = new URI(method.getURI().getPath(), false);
} catch (Exception e) { System.out.println("Can't build
URI: "+e); }
if(!relPath.isRelPath())
System.out.println("URI is not relative (it should
be)");
method.setURI(relPath);
System.out.println("URI: "+method.getURI());
} catch (URIException e) {
System.err.println("Failed to parse URI: "+e);
}
} else
System.out.println("Using default protocol");
System.out.println(client.getHost());
client.executeMethod(method); // use state and hostconfig of the
client object
}
This however fails building the relative URI:
Path: /files/131745342/foobar.tar.gz <- should be the URI
URI is not relative (it should be)
URI: http://myhost.example.com/files/131745342/foobar.tar.gz
myhost.example.com <- host is ok, but seems like the absolute method's
URI overwrites it as well as the protocol.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]