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 mars_man <sw...@yahoo.com> on 2008/10/01 17:26:09 UTC

Getting at the Location URI header value in the ClientParamStacks

Basically, my java client will be sending an HTTPS request to an ASP site;
that site gets the request, does what it needs to do and then redirects back
using a returnURL that is part of the initial HTTPS request. They then tack
on the result message to the return URL. For example: if the https request
is https://xyz.com/add.asp?name=sav&returnURL=http://mysite.com.   The
return URL from the xyz.com site is http://mysite.com?resultCode=success.

All I want to do is follow the redirection and get at that resultCode value
and I had this working with httpclients 3.0 but since i could not get the
SSL handshale working, i decided to look in to the HttpClients 4.0. I have
the SSL handshake issue resolved but can't figure out how to get at that
resultCode namevalue pair. I see the returnURI in the Location header field
in the ClientParamStacks in the Wire logs but I can't figure out how to get
at that field. I have tried looking at the request, response and the
httpentity objects (header values and other objects) but can't find it
there.....

My code is:
DefaultHttpClient httpclient = new DefaultHttpClient();      
HttpHost proxy =
            new HttpHost(config.getProperty("proxy"),
                new Integer(config.getProperty("port")).intValue(),
                "http");
        httpclient.getParams().setParameter(
            ConnRoutePNames.DEFAULT_PROXY, proxy);

        httpclient.getParams().setBooleanParameter(
           ClientPNames.HANDLE_REDIRECTS, true);

        //read the emisare keystore
        KeyStore trustStore =
KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(ReadFileUtil.getStream("/myKeystore.jks"),
            "changeit".toCharArray());
      
        //set up an SSLSicketFactory
        SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);
        Scheme sch = new Scheme("https", socketFactory, 443);
        httpclient.getConnectionManager().getSchemeRegistry().register(sch);

        HttpGet httpget = new HttpGet(emisareURL);
        System.out.println("executing request" + httpget.getRequestLine());

        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();

        httpclient.getParams();
      
        Header[] headers = response.getAllHeaders();
        for (int i = 0; i < headers.length; i++)
        {
            System.out.println(headers[i].getName() + " : " +
                headers[i].getValue());
        }

---------------------------
the older code that i had working with http clients 3.01 is

       HttpClient client = new HttpClient();
        GetMethod method = new GetMethod();
        try
        {
            method.setURI(new URI(myURL, false));

            method.setFollowRedirects(true);

            //make the call
            HostConfiguration hostConfig = new HostConfiguration();
            hostConfig.setProxy(config.getProperty("proxy"),
                new Integer(config.getProperty("port")).intValue());

            hostConfig.setHost(targethost, sslPort, myhttps);

            client.setHostConfiguration(hostConfig);

            int statusCode = client.executeMethod(method);

            if
 ((statusCode != HttpStatus.SC_OK))
            {
                throw new MyServiceException(
                    config.getProperty("SERVICE_ERROR_MSG"));
            }
            else
            {
                String returnedQueryString =
                    URLDecoder.decode(
                                method.getQueryString(),
                                "UTF-8").replaceAll("%20", " ");

                logger.logDebug("returnedQueryString = " +
returnedQueryString);

                int totalLen = returnedQueryString.length();
                int resultIndex = returnedQueryString.indexOf("result");
                returnMsg = returnedQueryString.substring(
                            resultIndex + RESULT_LENGTH, totalLen).
                            replaceAll(" ", "_");
                logger.logDebug("emisareMsg = " + returnMsg);

              
 logger.logDebug(config.getProperty(returnMsg.trim()));
                returnMsg = config.getProperty(returnMsg.trim());
            }
---------------
The return code from the external site ended up in the querystring attribute
of the method object and i could parse it to get what i needed.

Like I mentioned I could not get the SSL working using 3.0 (with my
implementation of SecureSocketProtocolFactory) till I switched to 4.0 but in
4.0, I can't figure out how to get at the return URI.

I am porting over an ASP app to Java and in the previous world, this https
communication was ASP page to ASP page but in the
new enrvironment it is going to be java client to ASP page. Whatever the ASP
returns back to us, we are not going to display it anywhere; we just need to
get
at the name-value parameter of the return URL.

Any help in getting this going using httpclient 4.0 will be greatly
appreaciated.

Thanks in advance.
-- 
View this message in context: http://www.nabble.com/Getting-at-the-Location-URI-header-value-in-the-ClientParamStacks-tp19762891p19762891.html
Sent from the HttpClient-User mailing list archive at Nabble.com.


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


Re: Getting at the Location URI header value in the ClientParamStacks

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Wed, 2008-10-01 at 08:26 -0700, mars_man wrote:
> Basically, my java client will be sending an HTTPS request to an ASP site;
> that site gets the request, does what it needs to do and then redirects back
> using a returnURL that is part of the initial HTTPS request. They then tack
> on the result message to the return URL. For example: if the https request
> is https://xyz.com/add.asp?name=sav&returnURL=http://mysite.com.   The
> return URL from the xyz.com site is http://mysite.com?resultCode=success.
> 
> All I want to do is follow the redirection and get at that resultCode value
> and I had this working with httpclients 3.0 but since i could not get the
> SSL handshale working, i decided to look in to the HttpClients 4.0. I have
> the SSL handshake issue resolved but can't figure out how to get at that
> resultCode namevalue pair. I see the returnURI in the Location header field
> in the ClientParamStacks in the Wire logs but I can't figure out how to get
> at that field. I have tried looking at the request, response and the
> httpentity objects (header values and other objects) but can't find it
> there.....
> 
> My code is:
> DefaultHttpClient httpclient = new DefaultHttpClient();      
> HttpHost proxy =
>             new HttpHost(config.getProperty("proxy"),
>                 new Integer(config.getProperty("port")).intValue(),
>                 "http");
>         httpclient.getParams().setParameter(
>             ConnRoutePNames.DEFAULT_PROXY, proxy);
> 
>         httpclient.getParams().setBooleanParameter(
>            ClientPNames.HANDLE_REDIRECTS, true);
> 
>         //read the emisare keystore
>         KeyStore trustStore =
> KeyStore.getInstance(KeyStore.getDefaultType());
>         trustStore.load(ReadFileUtil.getStream("/myKeystore.jks"),
>             "changeit".toCharArray());
>       
>         //set up an SSLSicketFactory
>         SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);
>         Scheme sch = new Scheme("https", socketFactory, 443);
>         httpclient.getConnectionManager().getSchemeRegistry().register(sch);
> 
>         HttpGet httpget = new HttpGet(emisareURL);
>         System.out.println("executing request" + httpget.getRequestLine());
> 
>         HttpResponse response = httpclient.execute(httpget);
>         HttpEntity entity = response.getEntity();
> 
>         httpclient.getParams();
>       
>         Header[] headers = response.getAllHeaders();
>         for (int i = 0; i < headers.length; i++)
>         {
>             System.out.println(headers[i].getName() + " : " +
>                 headers[i].getValue());
>         }
> 
> ---------------------------
> the older code that i had working with http clients 3.01 is
> 
>        HttpClient client = new HttpClient();
>         GetMethod method = new GetMethod();
>         try
>         {
>             method.setURI(new URI(myURL, false));
> 
>             method.setFollowRedirects(true);
> 
>             //make the call
>             HostConfiguration hostConfig = new HostConfiguration();
>             hostConfig.setProxy(config.getProperty("proxy"),
>                 new Integer(config.getProperty("port")).intValue());
> 
>             hostConfig.setHost(targethost, sslPort, myhttps);
> 
>             client.setHostConfiguration(hostConfig);
> 
>             int statusCode = client.executeMethod(method);
> 
>             if
>  ((statusCode != HttpStatus.SC_OK))
>             {
>                 throw new MyServiceException(
>                     config.getProperty("SERVICE_ERROR_MSG"));
>             }
>             else
>             {
>                 String returnedQueryString =
>                     URLDecoder.decode(
>                                 method.getQueryString(),
>                                 "UTF-8").replaceAll("%20", " ");
> 
>                 logger.logDebug("returnedQueryString = " +
> returnedQueryString);
> 
>                 int totalLen = returnedQueryString.length();
>                 int resultIndex = returnedQueryString.indexOf("result");
>                 returnMsg = returnedQueryString.substring(
>                             resultIndex + RESULT_LENGTH, totalLen).
>                             replaceAll(" ", "_");
>                 logger.logDebug("emisareMsg = " + returnMsg);
> 
>               
>  logger.logDebug(config.getProperty(returnMsg.trim()));
>                 returnMsg = config.getProperty(returnMsg.trim());
>             }
> ---------------
> The return code from the external site ended up in the querystring attribute
> of the method object and i could parse it to get what i needed.
> 
> Like I mentioned I could not get the SSL working using 3.0 (with my
> implementation of SecureSocketProtocolFactory) till I switched to 4.0 but in
> 4.0, I can't figure out how to get at the return URI.
> 
> I am porting over an ASP app to Java and in the previous world, this https
> communication was ASP page to ASP page but in the
> new enrvironment it is going to be java client to ASP page. Whatever the ASP
> returns back to us, we are not going to display it anywhere; we just need to
> get
> at the name-value parameter of the return URL.
> 
> Any help in getting this going using httpclient 4.0 will be greatly
> appreaciated.
> 
> Thanks in advance.

Sorry for responding so late. I just came back from vacation. 

There are two ways of obtaining the redirect location with HttpClient
4.0

(1) by providing a custom redirect handler

(2) by getting hold of the executed request object from the execution
context  

----
DefaultHttpClient  httpclient = new DefaultHttpClient(params);

httpclient.setRedirectHandler(new DefaultRedirectHandler() {

    @Override
    public URI getLocationURI(
            HttpResponse response, 
            HttpContext context) throws ProtocolException {
        URI location = super.getLocationURI(response, context);
        System.out.println(location); // method 1
        return location;
    }
    
});

HttpContext context = new BasicHttpContext();
HttpGet httpget = new HttpGet("http://localhost:8080");

HttpResponse response = httpclient.execute(httpget, context);

HttpUriRequest request = (HttpUriRequest)
context.getAttribute(ExecutionContext.HTTP_REQUEST);
System.out.println(request.getURI()); // method 2

---

Hope this helps

Oleg


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