You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hc.apache.org by "Yan, Charlene" <Ch...@thomsonlearning.com> on 2003/05/21 15:30:16 UTC

FormLoginDemo.java

Hello all,

I am a newbie to httpclient.  I am trying to pass xml string from my struts web application to cocoon web application.  Please note that they are two separate applications.  I am using httpclient to connect them.  I created a httpclient in my struts app action, set the host address of cocoon web application and try to connect.  The result is that it will connect and the status code is OK.  However, it will not start a browser with the destination URL.  I am wondering whether httpclient does have the functionality of opening up a new browser window. I read more docs on httpclient and find that there is no mentioning of opeinging up a new browser window.  My question is whether it will open a new browser window after I connect to the new url.  Please help!!


String LOGON_SITE = "localhost";
        int    LOGON_PORT = 8080;

        HttpClient client = new HttpClient();
        client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT, "http");
        client.getState().setCookiePolicy(CookiePolicy.COMPATIBILITY);
     	// 'developer.java.sun.com' has cookie compliance problems
        // Their session cookie's domain attribute is in violation of the RFC2109
        // We have to resort to using compatibility cookie policy

        //GetMethod authget = new GetMethod("/cocoon/request1");             

        if ((reportsForm.getEnteredSwbId() != null) &&
                (reportsForm.getEnteredSwbId().length() > 0)) {
            criteria.addEqualTo("swbId", reportsForm.getEnteredSwbId());

            try {
                Query query = QueryFactory.newQuery(TlnSolution.class, criteria);
                reportsForm.setSolution((TlnSolution) broker.getObjectByQuery(
                        query));

                if (reportsForm.getSolution() != null) { // Found a Solution
                  TlnUtils tlnUtils = new TlnUtils();
                  xmlString = tlnUtils.outputXML(reportsForm.getSolution());
                }

            //client.executeMethod(authget);
            //System.out.println("Login form get: " + authget.getStatusLine().toString()); 
            // release any connection resources used by the method
            //authget.releaseConnection();
            // See if we got any cookies
            Cookie[] initcookies = 
              client.getState().getCookies(LOGON_SITE, LOGON_PORT, "/", false);
            System.out.println("Initial set of cookies:");    
            if (initcookies.length == 0) {
                System.out.println("None");    
            } else {
                for (int i = 0; i < initcookies.length; i++) {
                    System.out.println("- " + initcookies[i].toString());    
                }
            }   
            PostMethod authpost = new PostMethod("/cocoon/request1");
            System.out.println("We are doing a post here.");
            NameValuePair Foo   = new NameValuePair("Foo", xmlString);
            authpost.setRequestBody(new NameValuePair[] {Foo});
            client.executeMethod(authpost);
            System.out.println("uri is " + authpost.getURI());
            System.out.println("host is " + client.getHostConfiguration().getHost());
        System.out.println("Login form post: " + authpost.getStatusLine().toString()); 
        // release any connection resources used by the method
        authpost.releaseConnection();
        // See if we got any cookies
        // The only way of telling whether logon succeeded is 
        // by finding a session cookie
        Cookie[] logoncookies = 
          client.getState().getCookies(LOGON_SITE, LOGON_PORT, "/", false);
        System.out.println("Logon cookies:");    
        if (logoncookies.length == 0) {
            System.out.println("None");    
        } else {
            for (int i = 0; i < logoncookies.length; i++) {
                System.out.println("- " + logoncookies[i].toString());    
            }
        }
        // Usually a successful form-based login results in a redicrect to 
        // another url
        int statuscode = authpost.getStatusCode();
        System.out.println("statuscode is " + statuscode);
        if (statuscode == HttpStatus.SC_OK ) {
            Header header = authpost.getResponseHeader("location");
            System.out.println("header is " + header);
            if (header != null) {
                String newuri = header.getValue();
                if ((newuri == null) || (newuri.equals(""))) {
                    newuri = "/";
                }
                System.out.println("Redirect target: " + newuri); 
                GetMethod redirect = new GetMethod(newuri);

                client.executeMethod(redirect);
                System.out.println("Redirect: " + redirect.getStatusLine().toString()); 
                // release any connection resources used by the method
                redirect.releaseConnection();
            } else {
                System.out.println("Invalid redirect");
                System.exit(1);
            }
        }

Thanks.

Charlene

Re: FormLoginDemo.java

Posted by Michael Becke <be...@u.washington.edu>.
As Ortwin said HttpClient does not provide any GUI functionality.  It is 
just a library for communicating over HTTP.  If you want the end user's 
browser to redirect to the URL returned from Cocoon you will need to 
return a redirect from your Struts Action.execute() method.  I'm 
guessing this might have problems though as the client's browser will 
not have the cookies used by HttpClient.

Mike

Yan, Charlene wrote:
> Hello all,
> 
> I am a newbie to httpclient.  I am trying to pass xml string from my struts web application to cocoon web application.  Please note that they are two separate applications.  I am using httpclient to connect them.  I created a httpclient in my struts app action, set the host address of cocoon web application and try to connect.  The result is that it will connect and the status code is OK.  However, it will not start a browser with the destination URL.  I am wondering whether httpclient does have the functionality of opening up a new browser window. I read more docs on httpclient and find that there is no mentioning of opeinging up a new browser window.  My question is whether it will open a new browser window after I connect to the new url.  Please help!!
> 
> 
> String LOGON_SITE = "localhost";
>         int    LOGON_PORT = 8080;
> 
>         HttpClient client = new HttpClient();
>         client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT, "http");
>         client.getState().setCookiePolicy(CookiePolicy.COMPATIBILITY);
>      	// 'developer.java.sun.com' has cookie compliance problems
>         // Their session cookie's domain attribute is in violation of the RFC2109
>         // We have to resort to using compatibility cookie policy
> 
>         //GetMethod authget = new GetMethod("/cocoon/request1");             
> 
>         if ((reportsForm.getEnteredSwbId() != null) &&
>                 (reportsForm.getEnteredSwbId().length() > 0)) {
>             criteria.addEqualTo("swbId", reportsForm.getEnteredSwbId());
> 
>             try {
>                 Query query = QueryFactory.newQuery(TlnSolution.class, criteria);
>                 reportsForm.setSolution((TlnSolution) broker.getObjectByQuery(
>                         query));
> 
>                 if (reportsForm.getSolution() != null) { // Found a Solution
>                   TlnUtils tlnUtils = new TlnUtils();
>                   xmlString = tlnUtils.outputXML(reportsForm.getSolution());
>                 }
> 
>             //client.executeMethod(authget);
>             //System.out.println("Login form get: " + authget.getStatusLine().toString()); 
>             // release any connection resources used by the method
>             //authget.releaseConnection();
>             // See if we got any cookies
>             Cookie[] initcookies = 
>               client.getState().getCookies(LOGON_SITE, LOGON_PORT, "/", false);
>             System.out.println("Initial set of cookies:");    
>             if (initcookies.length == 0) {
>                 System.out.println("None");    
>             } else {
>                 for (int i = 0; i < initcookies.length; i++) {
>                     System.out.println("- " + initcookies[i].toString());    
>                 }
>             }   
>             PostMethod authpost = new PostMethod("/cocoon/request1");
>             System.out.println("We are doing a post here.");
>             NameValuePair Foo   = new NameValuePair("Foo", xmlString);
>             authpost.setRequestBody(new NameValuePair[] {Foo});
>             client.executeMethod(authpost);
>             System.out.println("uri is " + authpost.getURI());
>             System.out.println("host is " + client.getHostConfiguration().getHost());
>         System.out.println("Login form post: " + authpost.getStatusLine().toString()); 
>         // release any connection resources used by the method
>         authpost.releaseConnection();
>         // See if we got any cookies
>         // The only way of telling whether logon succeeded is 
>         // by finding a session cookie
>         Cookie[] logoncookies = 
>           client.getState().getCookies(LOGON_SITE, LOGON_PORT, "/", false);
>         System.out.println("Logon cookies:");    
>         if (logoncookies.length == 0) {
>             System.out.println("None");    
>         } else {
>             for (int i = 0; i < logoncookies.length; i++) {
>                 System.out.println("- " + logoncookies[i].toString());    
>             }
>         }
>         // Usually a successful form-based login results in a redicrect to 
>         // another url
>         int statuscode = authpost.getStatusCode();
>         System.out.println("statuscode is " + statuscode);
>         if (statuscode == HttpStatus.SC_OK ) {
>             Header header = authpost.getResponseHeader("location");
>             System.out.println("header is " + header);
>             if (header != null) {
>                 String newuri = header.getValue();
>                 if ((newuri == null) || (newuri.equals(""))) {
>                     newuri = "/";
>                 }
>                 System.out.println("Redirect target: " + newuri); 
>                 GetMethod redirect = new GetMethod(newuri);
> 
>                 client.executeMethod(redirect);
>                 System.out.println("Redirect: " + redirect.getStatusLine().toString()); 
>                 // release any connection resources used by the method
>                 redirect.releaseConnection();
>             } else {
>                 System.out.println("Invalid redirect");
>                 System.exit(1);
>             }
>         }
> 
> Thanks.
> 
> Charlene
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-httpclient-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-httpclient-dev-help@jakarta.apache.org
> 


Re: FormLoginDemo.java

Posted by Ortwin Glück <or...@nose.ch>.
Yan,

HttpClient is a server side application without any graphical user 
interface. It is supposed to transfer data to and from any Http Server. 
The dealing with the data is considered the responsibility of the user 
(you).

So your servlet will probably need to pass the output from cocoon 
through to the ServletOutputStream.

Odi


Yan, Charlene wrote:
> Hello all,
> 
> I am a newbie to httpclient.  I am trying to pass xml string from my
> struts web application to cocoon web application.  Please note that
> they are two separate applications.  I am using httpclient to connect
> them.  I created a httpclient in my struts app action, set the host
> address of cocoon web application and try to connect.  The result is
> that it will connect and the status code is OK.  However, it will not
> start a browser with the destination URL.  I am wondering whether
> httpclient does have the functionality of opening up a new browser
> window. I read more docs on httpclient and find that there is no
> mentioning of opeinging up a new browser window.  My question is
> whether it will open a new browser window after I connect to the new
> url.  Please help!!