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

 更新时间:2013年06月04日 11:31:34   作者:  
本篇文章是对使用httpClient在客户端与服务器端传输对象参数进行了详细的分析介绍,需要的朋友参考下
昨天把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. <br>
     */
    public void destroy()
    {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }
    /**
     * The doGet method of the servlet. <br>
     *
     * 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. <br>
     *
     * 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. <br>
     *
     * @throws ServletException
     *             if an error occure
     */
    public void init() throws ServletException
    {
        // Put your code here
    }
}

相关文章

  • 详解Java程序读取properties配置文件的方法

    详解Java程序读取properties配置文件的方法

    这篇文章主要介绍了Java读取properties配置文件的方法讲解,properties可以被看作是Java世界的ini,Java中有Properties可以操作它,需要的朋友可以参考下
    2016-04-04
  • 在启动后台 jar包时,使用指定的 application.yml操作

    在启动后台 jar包时,使用指定的 application.yml操作

    这篇文章主要介绍了在启动后台 jar包时,使用指定的 application.yml操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-10-10
  • SpringBoot利用AOP实现一个日志管理详解

    SpringBoot利用AOP实现一个日志管理详解

    目前有这么个问题,有两个系统CSP和OMS,这俩系统共用的是同一套日志操作:Log;目前想区分下这俩系统的日志操作,那没办法了,只能重写一份Log的日志操作。本文就将利用AOP实现一个日志管理,需要的可以参考一下
    2022-09-09
  • java(包括springboot)读取resources下文件方式实现

    java(包括springboot)读取resources下文件方式实现

    这篇文章主要介绍了java(包括springboot)读取resources下文件方式实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-09-09
  • Spring boot监控Actuator-Admin实现过程详解

    Spring boot监控Actuator-Admin实现过程详解

    这篇文章主要介绍了Spring boot监控Actuator-Admin实现过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-09-09
  • 使用spring实现邮件的发送实例(含测试,源码,注释)

    使用spring实现邮件的发送实例(含测试,源码,注释)

    本篇文章主要介绍了使用spring实现邮件的发送实例,详细的介绍了使用spring配置实现邮件发送,含测试,源码,注释,有兴趣的可以下
    2017-05-05
  • springboot 整合 sa-token简介及入门教程

    springboot 整合 sa-token简介及入门教程

    Sa-Token 是一个轻量级 Java 权限认证框架,主要解决:登录认证、权限认证、Session会话、单点登录、OAuth2.0、微服务网关鉴权 等一系列权限相关问题,这篇文章主要介绍了springboot 整合 sa-token简介及入门教程,需要的朋友可以参考下
    2023-05-05
  • java实现识别二维码图片功能

    java实现识别二维码图片功能

    这篇文章主要为大家详细介绍了java实现识别二维码图片功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-04-04
  • SSH框架网上商城项目第15战之线程、定时器同步首页数据

    SSH框架网上商城项目第15战之线程、定时器同步首页数据

    这篇文章主要为大家详细介绍了SSH框架网上商城项目第15战之线程、定时器同步首页数据,感兴趣的小伙伴们可以参考一下
    2016-06-06
  • Eclipse项目有红感叹号的解决方法

    Eclipse项目有红感叹号的解决方法

    这篇文章主要为大家详细介绍了Eclipse项目有红感叹号的解决方法,给出了Eclipse项目有红感叹号的原因,以及如何解决?,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-04-04

最新评论