探討:使用httpClient在客戶端與服務(wù)器端傳輸對象參數(shù)的詳解
更新時間:2013年06月04日 11:31:34 作者:
本篇文章是對使用httpClient在客戶端與服務(wù)器端傳輸對象參數(shù)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
昨天把httpClient的源代碼下載來看了一下。 稍微跟蹤了一下,最終還是使用java.net包的東西.不過封裝的實(shí)在是漂亮.寫程序方便多了。不過還是建議最好先熟悉net包下的東西.為了測試寫了個在客戶端和服務(wù)器段傳對象的代碼. 簡單的傳遞了一個字符串. 如果復(fù)雜點(diǎn)可以傳其他的對象,在參數(shù)里給出class name之類的信息.服務(wù)器端就可以使用反射來做一些實(shí)用的操作了。
客戶端:
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
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
}
}
客戶端:
復(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
}
}
您可能感興趣的文章:
- java使用httpclient模擬post請求和get請求示例
- java使用httpclient發(fā)送post請求示例
- 使用httpclient無需證書調(diào)用https的示例(java調(diào)用https)
- JAVA利用HttpClient進(jìn)行POST請求(HTTPS)實(shí)例
- java實(shí)現(xiàn)HttpClient異步請求資源的方法
- java發(fā)送HttpClient請求及接收請求結(jié)果過程的簡單實(shí)例
- HttpClient 在Java項(xiàng)目中的使用詳解
- 使用java的HttpClient實(shí)現(xiàn)多線程并發(fā)
- 關(guān)于Http持久連接和HttpClient連接池的深入理解
相關(guān)文章
SpringBoot通過redisTemplate調(diào)用lua腳本并打印調(diào)試信息到redis log(方法步驟詳解)
這篇文章主要介紹了SpringBoot通過redisTemplate調(diào)用lua腳本 并打印調(diào)試信息到redis log,本文分步驟給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2020-02-02Intellij IDEA 如何通過數(shù)據(jù)庫表生成帶注解的實(shí)體類(圖文詳細(xì)教程)
這篇文章主要介紹了Intellij IDEA 如何通過數(shù)據(jù)庫表生成帶注解的實(shí)體類(圖文詳細(xì)教程),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11基于Java的Socket多客戶端Client-Server聊天程序的實(shí)現(xiàn)
這篇文章主要介紹了基于Java的Socket多客戶端Client-Server聊天程序的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03Java實(shí)現(xiàn)的DES加密解密工具類實(shí)例
這篇文章主要介紹了Java實(shí)現(xiàn)的DES加密解密工具類,結(jié)合具體實(shí)例形式分析了Java實(shí)現(xiàn)的DES加密解密工具類定義與使用方法,需要的朋友可以參考下2017-09-09Spring Boot 2.7.6整合redis與低版本的區(qū)別
這篇文章主要介紹了Spring Boot 2.7.6整合redis與低版本的區(qū)別,文中補(bǔ)充介紹了SpringBoot各個版本使用Redis之間的區(qū)別實(shí)例講解,需要的朋友可以參考下2023-02-02mybatis-plus添加數(shù)據(jù)時id自增問題及解決
這篇文章主要介紹了mybatis-plus添加數(shù)據(jù)時id自增問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01