Search code examples
javaapache-httpclient-4.x

Getting query String using HttpClient 4.1


I am migrating to Httpclient 4.1 from HttpClient 3.0 How can I get the query String from the URL.

client = new DefaultHttpClient();

client.getHostConfiguration().setHost( pro.getProperty( "host" ),
                Integer.parseInt( pro.getProperty( "port" ).trim() ),
                    pro.getProperty( "protocol" ) );
//client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);

client.getCredentialsProvider().setCredentials(
        new AuthScope( pro.getProperty( "host" ),
            Integer.parseInt( pro.getProperty( "port" ).trim() ),
                pro.getProperty( "protocol" ) ),
        new UsernamePasswordCredentials( user, userpassword ) );

client.getParams().setBooleanParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true );
authget = new HttpGet( pro.getProperty( "getparam" ) );
HttpResponse response = client.execute( authget );
HttpEntity entity = response.getEntity();

What should I insert here for getting the query string to match from "&".

StringTokenizer qryStrToken = new StringTokenizer(***Insert code for getting query string***,"&");

while (qryStrToken.hasMoreTokens()){
    String temp = qryStrToken.nextToken();
    if(temp.startsWith("SMAGENTNAME")){
        smAgentName = temp.substring(temp.indexOf("=")+1);
    }
}

Solution

  • Use the getUri method of HttpGet.

    String uri = authGet.getURI();