当前位置:  编程技术>java/j2ee

探讨:使用httpClient在客户端与服务器端传输对象参数的详解

    来源: 互联网  发布时间:2014-10-26

    本文导语:  昨天把httpClient的源代码下载来看了一下。 稍微跟踪了一下,最终还是使用java.net包的东西.不过封装的实在是漂亮.写程序方便多了。不过还是建议最好先熟悉net包下的东西.为了测试写了个在客户端和服务器段传对象的代码. 简...

昨天把httpClient的源代码下载来看了一下。 稍微跟踪了一下,最终还是使用java.net包的东西.不过封装的实在是漂亮.写程序方便多了。不过还是建议最好先熟悉net包下的东西.为了测试写了个在客户端和服务器段传对象的代码. 简单的传递了一个字符串. 如果复杂点可以传其他的对象,在参数里给出class name之类的信息.服务器端就可以使用反射来做一些实用的操作了。
客户端:
代码如下:

import java.io.IOException;
import java.io.Serializable;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
public class MyTest
{
    /**
     * @param args
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public static void main(String[] args) throws IOException, ClassNotFoundException
    {
        String url = "http://localhost:8084/system/linantest";
        String queryString = "test=hello";
        String inputObj = " boy!";
        Serializable s = getObjFromServer(url, queryString, inputObj);
        System.out.println(s.toString());
    }
    /**
     * @param url
     * @param queryString 类似a=b&c=d 形式的参数
     *
     * @param inputObj   发送到服务器的对象。
     *    
     * @return 服务器返回到客户端的对象。
     * @throws IOException
     */
    public static Serializable getObjFromServer(String url, String queryString, Serializable inputObj) throws IOException
    {
        HttpClient client = new HttpClient();
        PostMethod post = new PostMethod(url);
        post.setQueryString(queryString);
        post.setRequestHeader("Content-Type", "application/octet-stream");
        java.io.ByteArrayOutputStream bOut = new java.io.ByteArrayOutputStream(1024);
        java.io.ByteArrayInputStream bInput = null;
        java.io.ObjectOutputStream out = null;
        Serializable returnObj = null;
        try
        {
            out = new java.io.ObjectOutputStream(bOut);
            out.writeObject(inputObj);
            out.flush();
            out.close();
            out = null;
            bInput = new java.io.ByteArrayInputStream(bOut.toByteArray());
            RequestEntity re = new InputStreamRequestEntity(bInput);
            post.setRequestEntity(re);
            client.executeMethod(post);
            java.io.InputStream in = post.getResponseBodyAsStream();
            java.io.ObjectInputStream oInput = new java.io.ObjectInputStream(in);
            returnObj = (Serializable) oInput.readObject();
            oInput.close();
            oInput = null;
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (ClassNotFoundException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally
        {
            if (out != null)
            {
                out.close();
                out = null;
            }
            if (bInput != null)
            {
                bInput.close();
                bInput = null;
            }
            //释放连接
            post.releaseConnection();
        }
        return returnObj;
    }
}

服务器端的servlet
代码如下:

package test.li;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.openjweb.eai.adapter.TimerDBAdapter;
public class TestServlet extends HttpServlet
{
    public TestServlet()
    {
        super();
    }
    /**
     * Destruction of the servlet.

     */
    public void destroy()
    {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }
    /**
     * The doGet method of the servlet.

     *
     * This method is called when a form has its tag value method equals to get.
     *
     * @param request
     *            the request send by the client to the server
     * @param response
     *            the response send by the server to the client
     * @throws Exception
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    {
        String test = request.getParameter("test");
        java.io.ObjectInputStream oi = null;
        java.io.ObjectOutputStream ot = null;
        try
        {
            oi = new java.io.ObjectInputStream(request.getInputStream());
            Object o = oi.readObject();
            oi.close();
            oi = null;

            String outObj = test + o.toString();
            ot = new java.io.ObjectOutputStream(response.getOutputStream());
            ot.writeObject(outObj);
            ot.flush();
            ot.close();
            ot = null;
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (ClassNotFoundException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally
        {
            try
            {
                if (oi != null)
                {
                    oi.close();
                    oi = null;
                }
                if (ot != null)
                {
                    ot.close();
                    ot = null;
                }
            }
            catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        }
    /**
     * The doPost method of the servlet.

     *
     * This method is called when a form has its tag value method equals to
     * post.
     *
     * @param request
     *            the request send by the client to the server
     * @param response
     *            the response send by the server to the client
     * @throws ServletException
     *             if an error occurred
     * @throws IOException
     *             if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        doGet(request, response);
    }
    /**
     * Initialization of the servlet.

     *
     * @throws ServletException
     *             if an error occure
     */
    public void init() throws ServletException
    {
        // Put your code here
    }
}


    
 
 
 
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • jsp/servlet开发网站,客户端打印解决方案探讨!
  • 会javabean,用jbuiler的请进,呵呵,大家探讨探讨,参与者都有分!!
  • 厦门LINUX技术探讨群
  • 大家可以探讨一下Servlet的设计模式么?
  • 探讨Oracle中的&号问题
  • 大家来探讨!
  • 探讨如何学习j2ee!
  • 探讨下启动盘的制作原理!
  • 高分探讨小问题--关于jsp
  • [探讨]文件系统的控制问题
  • 欢迎高手指教,菜鸟来探讨!
  • 关于一个系统的探讨~
  • jbuilder6不稳定!请有使用经验的来探讨一下是什么原因?
  • C/C++代码格式工具探讨
  • 请教--父进程监控子进程,欢迎高手进来探讨!!!
  • 探讨下linux下pam验证中的crypt
  • 一个问题与大家探讨,各位请进,来者有分!!!
  • 关于学习Linux的一个问题(非技术的探讨)
  • makefile eval 问题--共同探讨
  • Linux Makefile探讨,产生的.d文件是.c文件的依赖?
  • 要做显示数据库内容的柱状图,有意者可以提意见大家进行探讨


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3