程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA編程入門知識 >> 探討:使用httpClient在客戶端與服務器端傳輸對象參數的詳解

探討:使用httpClient在客戶端與服務器端傳輸對象參數的詳解

編輯:JAVA編程入門知識
昨天把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
    }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved