麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > Java > 正文

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

2019-11-26 16:04:28
字體:
來源:轉載
供稿:網友
昨天把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
    }
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 亚洲午夜视频 | www.精品一区| 国产色91 | 国产在线中文 | 国产精品免费观在线 | 欧美日韩亚洲国产 | 国产精品a一 | xxxxxx打针视频vk | 男女隐私免费视频 | 91中文在线观看 | 9999视频 | 成人做爽爽爽爽免费国产软件 | 国产理论视频在线观看 | 成人免费网站在线观看视频 | 久久精品在线免费观看 | 成人免费一区二区三区在线观看 | 久久日本 | 性少妇videosexfreexxx片 | 日韩黄网站 | 久久久久久久不卡 | 毛片免费视频网站 | 国产成人午夜高潮毛片 | 成人青青草 | 韩国一级免费视频 | 九九精品在线观看视频 | av大全在线免费观看 | 午夜精品视频在线 | 黑人一区二区 | 免费观看视频在线 | 日韩av成人| 精品国产91久久久久久浪潮蜜月 | 精品在线观看一区 | 欧美一级黄| 欧美性生活xxxxx | 欧美激情精品久久久久久黑人 | 欧美日韩综合视频 | 中文字幕网站在线 | 综合网日日天干夜夜久久 | 嫩草91在线| 99影视在线视频免费观看 | 免费黄色大片在线观看 |