You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hc.apache.org by "Denis Carniel (JIRA)" <ji...@apache.org> on 2013/01/24 16:55:13 UTC
[jira] [Updated] (HTTPCLIENT-1304) SystemDefaultHttpClient discards
Headers set on the request
[ https://issues.apache.org/jira/browse/HTTPCLIENT-1304?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Denis Carniel updated HTTPCLIENT-1304:
--------------------------------------
Description:
If a HttpUriRequest has headers defined using the "setHeader" / "addHeader" methods, those headers are properly sent when the request is executed through a DefaultHttpClient but are discarded when using a SystemDefaultHttpClient.
As this does not seem to be an expected behaviour it is likely a bug, and a nasty one as the link between the HttpClient implementation and headers processing isn't obvious.
Folowing the first comment, here is the piece of code I used for my tests (see the point is to add the "Proxy-Authentication" but I tried changing the name and the same happened):
{noformat}
public String getThroughHTTPClient( String urlToRead ) {
String result = null;
HttpUriRequest request = new HttpGet( urlToRead );
if( request != null ) {
DefaultHttpClient client = new SystemDefaultHttpClient(); // DefaultHttpClient();
String proxyUser = System.getProperty( "http.proxyUser" );
if ( StringUtils.isNotEmpty( proxyUser ) ) {
// This part should work when SystemDefaultHttpClient handles headers properly (see https://issues.apache.org/jira/browse/HTTPCLIENT-1304 )
String proxyAuthCredentials = proxyUser + ":" + System.getProperty( "http.proxyPassword" );
String base64Credentials = Base64.encodeBase64String( proxyAuthCredentials.getBytes( ) );
request.setHeader( "Proxy-Authentication", "Basic " + base64Credentials );
System.out.println( "Basic " + Base64.encodeBase64String( proxyAuthCredentials.getBytes() ) );
}
try {
HttpResponse response = client.execute(request);
result = IOUtils.toString( response.getEntity().getContent() );
} catch ( ClientProtocolException e ) {
System.err.println( "Unable to process HTTP request." );
e.printStackTrace();
} catch ( IOException e ) {
System.err.println( "Unable to process HTTP request." );
e.printStackTrace();
} finally {
client.getConnectionManager().shutdown();
}
}
return result;
}
{noformat}
I could see that the sent headers were different when changing the HttpClient implementation by turning on header logging.
was:
If a HttpUriRequest has headers defined using the "setHeader" / "addHeader" methods, those headers are properly sent when the request is executed through a DefaultHttpClient but are discarded when using a SystemDefaultHttpClient.
As this does not seem to be an expected behaviour it is likely a bug, and a nasty one as the link between the HttpClient implementation and headers processing isn't obvious.
> SystemDefaultHttpClient discards Headers set on the request
> -----------------------------------------------------------
>
> Key: HTTPCLIENT-1304
> URL: https://issues.apache.org/jira/browse/HTTPCLIENT-1304
> Project: HttpComponents HttpClient
> Issue Type: Bug
> Components: HttpClient
> Affects Versions: 4.2.3
> Reporter: Denis Carniel
>
> If a HttpUriRequest has headers defined using the "setHeader" / "addHeader" methods, those headers are properly sent when the request is executed through a DefaultHttpClient but are discarded when using a SystemDefaultHttpClient.
> As this does not seem to be an expected behaviour it is likely a bug, and a nasty one as the link between the HttpClient implementation and headers processing isn't obvious.
> Folowing the first comment, here is the piece of code I used for my tests (see the point is to add the "Proxy-Authentication" but I tried changing the name and the same happened):
> {noformat}
> public String getThroughHTTPClient( String urlToRead ) {
> String result = null;
>
> HttpUriRequest request = new HttpGet( urlToRead );
> if( request != null ) {
> DefaultHttpClient client = new SystemDefaultHttpClient(); // DefaultHttpClient();
> String proxyUser = System.getProperty( "http.proxyUser" );
> if ( StringUtils.isNotEmpty( proxyUser ) ) {
> // This part should work when SystemDefaultHttpClient handles headers properly (see https://issues.apache.org/jira/browse/HTTPCLIENT-1304 )
> String proxyAuthCredentials = proxyUser + ":" + System.getProperty( "http.proxyPassword" );
> String base64Credentials = Base64.encodeBase64String( proxyAuthCredentials.getBytes( ) );
> request.setHeader( "Proxy-Authentication", "Basic " + base64Credentials );
> System.out.println( "Basic " + Base64.encodeBase64String( proxyAuthCredentials.getBytes() ) );
> }
> try {
> HttpResponse response = client.execute(request);
> result = IOUtils.toString( response.getEntity().getContent() );
> } catch ( ClientProtocolException e ) {
> System.err.println( "Unable to process HTTP request." );
> e.printStackTrace();
> } catch ( IOException e ) {
> System.err.println( "Unable to process HTTP request." );
> e.printStackTrace();
> } finally {
> client.getConnectionManager().shutdown();
> }
> }
>
> return result;
> }
> {noformat}
> I could see that the sent headers were different when changing the HttpClient implementation by turning on header logging.
--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org