commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Sanjeev Tripathi" <sanjeev.tripa...@parago.com>
Subject RE: [httpclient] How to send and recieve SerializedObjectusingHttpClient
Date Mon, 14 Feb 2005 02:54:37 GMT
Wendy,

Here is complete code:


http client side code


package com.parago.communication;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.lang.SerializationUtils;

import java.util.Vector;

public class FormLogin {
//    static final String LOGON_SITE = "developer.java.sun.com";
    static final String LOGON_SITE = "localhost";
    static final int LOGON_PORT = 8080;

    public FormLogin() {
        super();
    }

    public static void main(String[] args) throws Exception {

        PostMethod authpost = null;
        try {

            HttpClient client = new HttpClient();
            client.getHostConfiguration().setHost(LOGON_SITE,
LOGON_PORT, "http");


            com.parago.communication.CommunicationVO commVO1 = new
com.parago.communication.CommunicationVO(145, "Object1");
            com.parago.communication.CommunicationVO commVO2 = new
com.parago.communication.CommunicationVO(175, "Object2");
            Vector commVOVector = new Vector();
            commVOVector.add(commVO1);
            commVOVector.add(commVO2);

            byte[] encodedData =
Base64.encodeBase64(SerializationUtils.serialize(commVOVector));
            String encodedString = new String(encodedData);
            System.out.println("Encoded Legnth: " + encodedData.length +
"Encoded String Legnth: " + encodedString.length());

            encodedData = encodedString.getBytes();
            byte[] decodeData = Base64.decodeBase64(encodedData);
            Vector processedVOVector = (Vector)
SerializationUtils.deserialize(decodeData);
            for (int i = 0; i < processedVOVector.size(); i++) {
                CommunicationVO vo = (CommunicationVO)
processedVOVector.get(i);
                System.out.println("id :" + vo.getSubmissionId() + ":
desc:" + vo.getDescription());
            }

            authpost = new PostMethod("/argo/ArgoControl");

            // Prepare login parameters
            NameValuePair action = new NameValuePair("action", "login");
            NameValuePair url = new NameValuePair("url",
"argo/index.html");
            NameValuePair userid = new NameValuePair("LOGIN_NAME",
"smith");
            NameValuePair password = new NameValuePair("LOGIN_PASSWORD",
"irving");
            NameValuePair thickClient = new NameValuePair("ThickClient",
"ThickClient");
            NameValuePair commVOParm = new NameValuePair("CommVO",
encodedString);

            // Set parameters
            authpost.setRequestBody(new NameValuePair[]{action, url,
userid, password, thickClient, commVOParm});
            //Execute method
            client.executeMethod(authpost);

            System.out.println("Login form post: " +
authpost.getStatusLine());
            System.out.println("Response content-type: " +
authpost.getResponseCharSet());

            byte[] decodeSevletData =
org.apache.commons.codec.binary.Base64.decodeBase64(authpost.getResponse
BodyAsString().getBytes());

            Vector servletVecoter = (Vector)
SerializationUtils.deserialize(decodeSevletData);
            System.out.println("processedVOVector :" + servletVecoter);
            for (int i = 0; i < servletVecoter.size(); i++) {
                CommunicationVO vo = (CommunicationVO)
servletVecoter.get(i);
                System.out.println("id :" + vo.getSubmissionId() + ":
desc:" + vo.getDescription());
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // Release the connection.
            System.out.println("Releasing connection: ");
            authpost.releaseConnection();
        }
    }
}

servlet code in service method

if
(ThickClient.equalsIgnoreCase(request.getParameter("THICK_CLIENT_FLAG"))
)
                {
                    ServletOutputStream oos =
response.getOutputStream();
                    com.parago.communication.CommunicationVO commVO1 =
new com.parago.communication.CommunicationVO(45, "Object1");
                    com.parago.communication.CommunicationVO commVO2 =
new com.parago.communication.CommunicationVO(75, "Object2");
                    Vector commVOVector = new Vector();
                    commVOVector.add(commVO1);
                    commVOVector.add(commVO2);

                    byte[] encodedData =
Base64.encodeBase64(SerializationUtils.serialize(commVOVector));
                    oos.print(new String(encodedData));
                    oos.flush();
                    oos.close();
                    return;


                }




value object that I am passing

package com.parago.communication;

/**
 * Created by IntelliJ IDEA.
 * User: stripath
 * Date: Feb 5, 2005
 * Time: 4:08:30 PM
 * To change this template use File | Settings | File Templates.
 */
public class CommunicationVO implements java.io.Serializable{
    public CommunicationVO(int id,String desc) {
        this.submissionId = id;
        this.description = desc;
    }
    private int submissionId;
    private String description;

    public int getSubmissionId () {
        return submissionId;
    }
    public String getDescription() {
        return description;
    }
}

I am getting following exception

java.io.StreamCorruptedException: invalid stream header

I tried sending byte[] also instead of string but I am getting same
error.


thanks.
Sanjeev Tripathi



-----Original Message-----
From: Wendy Smoak [mailto:java@wendysmoak.com] 
Sent: Friday, February 11, 2005 6:24 PM
To: Jakarta Commons Users List
Subject: Re: [httpclient] How to send and recieve
SerializedObjectusingHttpClient

From: "Sanjeev Tripathi" <sanjeev.tripathi@parago.com>

> Please tell me where I can improve this code as its not working.

Have you been able to fix the problem yet?  I *might* have time to look
at
it this weekend.  A more complete example would help, along with a
description of exactly what you wanted, and what your code is doing
instead.
( http://www.catb.org/~esr/faqs/smart-questions.html )  Maybe try it
with a
Vector of String or Date objects, so that anyone who wants to try your
code
can do it without needing additional classes.

-- 
Wendy Smoak


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message