最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2013年06月04日 11:31:34   作者:  
本篇文章是對使用httpClient在客戶端與服務(wù)器端傳輸對象參數(shù)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
昨天把httpClient的源代碼下載來看了一下。 稍微跟蹤了一下,最終還是使用java.net包的東西.不過封裝的實在是漂亮.寫程序方便多了。不過還是建議最好先熟悉net包下的東西.為了測試寫了個在客戶端和服務(wù)器段傳對象的代碼. 簡單的傳遞了一個字符串. 如果復(fù)雜點可以傳其他的對象,在參數(shù)里給出class name之類的信息.服務(wù)器端就可以使用反射來做一些實用的操作了。
客戶端:
復(fù)制代碼 代碼如下:

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 形式的參數(shù)
     *
     * @param inputObj   發(fā)送到服務(wù)器的對象。
     *    
     * @return 服務(wù)器返回到客戶端的對象。
     * @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;
    }
}

服務(wù)器端的servlet
復(fù)制代碼 代碼如下:

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
    }
}

相關(guān)文章

  • 詳解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實現(xiàn)一個日志管理詳解

    SpringBoot利用AOP實現(xiàn)一個日志管理詳解

    目前有這么個問題,有兩個系統(tǒng)CSP和OMS,這倆系統(tǒng)共用的是同一套日志操作:Log;目前想?yún)^(qū)分下這倆系統(tǒng)的日志操作,那沒辦法了,只能重寫一份Log的日志操作。本文就將利用AOP實現(xiàn)一個日志管理,需要的可以參考一下
    2022-09-09
  • java(包括springboot)讀取resources下文件方式實現(xiàn)

    java(包括springboot)讀取resources下文件方式實現(xiàn)

    這篇文章主要介紹了java(包括springboot)讀取resources下文件方式實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Spring boot監(jiān)控Actuator-Admin實現(xiàn)過程詳解

    Spring boot監(jiān)控Actuator-Admin實現(xiàn)過程詳解

    這篇文章主要介紹了Spring boot監(jiān)控Actuator-Admin實現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-09-09
  • 使用spring實現(xiàn)郵件的發(fā)送實例(含測試,源碼,注釋)

    使用spring實現(xiàn)郵件的發(fā)送實例(含測試,源碼,注釋)

    本篇文章主要介紹了使用spring實現(xiàn)郵件的發(fā)送實例,詳細(xì)的介紹了使用spring配置實現(xiàn)郵件發(fā)送,含測試,源碼,注釋,有興趣的可以下
    2017-05-05
  • springboot 整合 sa-token簡介及入門教程

    springboot 整合 sa-token簡介及入門教程

    Sa-Token 是一個輕量級 Java 權(quán)限認(rèn)證框架,主要解決:登錄認(rèn)證、權(quán)限認(rèn)證、Session會話、單點登錄、OAuth2.0、微服務(wù)網(wǎng)關(guān)鑒權(quán) 等一系列權(quán)限相關(guān)問題,這篇文章主要介紹了springboot 整合 sa-token簡介及入門教程,需要的朋友可以參考下
    2023-05-05
  • java實現(xiàn)識別二維碼圖片功能

    java實現(xiàn)識別二維碼圖片功能

    這篇文章主要為大家詳細(xì)介紹了java實現(xiàn)識別二維碼圖片功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • SSH框架網(wǎng)上商城項目第15戰(zhàn)之線程、定時器同步首頁數(shù)據(jù)

    SSH框架網(wǎng)上商城項目第15戰(zhàn)之線程、定時器同步首頁數(shù)據(jù)

    這篇文章主要為大家詳細(xì)介紹了SSH框架網(wǎng)上商城項目第15戰(zhàn)之線程、定時器同步首頁數(shù)據(jù),感興趣的小伙伴們可以參考一下
    2016-06-06
  • Eclipse項目有紅感嘆號的解決方法

    Eclipse項目有紅感嘆號的解決方法

    這篇文章主要為大家詳細(xì)介紹了Eclipse項目有紅感嘆號的解決方法,給出了Eclipse項目有紅感嘆號的原因,以及如何解決?,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-04-04

最新評論

天津市| 报价| 谷城县| 芜湖县| 房山区| 东兰县| 铜山县| 阿图什市| 庆城县| 泗阳县| 华蓥市| 南京市| 中西区| 呼伦贝尔市| 比如县| 军事| 定远县| 团风县| 鹿邑县| 兰州市| 忻州市| 柏乡县| 金平| 鲜城| 山阴县| 临江市| 昆明市| 玉溪市| 昌黎县| 仲巴县| 贵州省| 罗山县| 尉氏县| 涟水县| 德惠市| 芜湖市| 卓尼县| 博罗县| 东港市| 屯留县| 太白县|