Index: test/org/apache/commons/httpclient/TestURI.java =================================================================== --- test/org/apache/commons/httpclient/TestURI.java (revision 160003) +++ test/org/apache/commons/httpclient/TestURI.java (working copy) @@ -31,6 +31,8 @@ package org.apache.commons.httpclient; +import org.apache.commons.httpclient.methods.GetMethod; + import junit.framework.Test; import junit.framework.TestSuite; @@ -230,4 +232,20 @@ } + public void testURIEscaping() throws Exception { + String escaped = "http://some.host.com/%41.html"; + String unescaped = "http://some.host.com/A.html"; + URI u1 = new URI(escaped, true); + GetMethod method = new GetMethod(); + method.setURI(u1); + URI u2 = method.getURI(); + + assertEquals(escaped, u1.toString()); + assertEquals(escaped, new String(u1.getRawURI())); + assertEquals(unescaped, u1.getURI()); + assertEquals(escaped, u2.toString()); + assertEquals(escaped, new String(u2.getRawURI())); + assertEquals(unescaped, u2.getURI()); + } + } Index: java/org/apache/commons/httpclient/HttpMethodBase.java =================================================================== --- java/org/apache/commons/httpclient/HttpMethodBase.java (revision 160003) +++ java/org/apache/commons/httpclient/HttpMethodBase.java (working copy) @@ -242,32 +242,23 @@ * @see org.apache.commons.httpclient.HttpMethod#getURI() */ public URI getURI() throws URIException { - - if (this.httphost == null) { - // just use a relative URI, the host hasn't been set - URI tmpUri = new URI(null, null, path, null, null); - tmpUri.setEscapedQuery(queryString); - return tmpUri; - } else { - - // we only want to include the port if it's not the default + StringBuffer buffer = new StringBuffer(); + if (this.httphost != null) { + buffer.append(this.httphost.getProtocol().getScheme()); + buffer.append("://"); + buffer.append(this.httphost.getHostName()); int port = this.httphost.getPort(); - if (port == this.httphost.getProtocol().getDefaultPort()) { - port = -1; + if (port != -1 && port != this.httphost.getProtocol().getDefaultPort()) { + buffer.append(":"); + buffer.append(port); } - URI tmpUri = new URI( - this.httphost.getProtocol().getScheme(), - null, - this.httphost.getHostName(), - port, - path, - null // to set an escaped form - ); - tmpUri.setEscapedQuery(queryString); - return tmpUri; - } - + buffer.append(this.path); + if (this.queryString != null) { + buffer.append('?'); + buffer.append(this.queryString); + } + return new URI(buffer.toString(), true); } /**