探討:使用httpClient在客戶端與服務(wù)器端傳輸對(duì)象參數(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 類(lèi)似a=b&c=d 形式的參數(shù)
*
* @param inputObj 發(fā)送到服務(wù)器的對(duì)象。
*
* @return 服務(wù)器返回到客戶端的對(duì)象。
* @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
}
}
- java使用httpclient模擬post請(qǐng)求和get請(qǐng)求示例
- java使用httpclient發(fā)送post請(qǐng)求示例
- 使用httpclient無(wú)需證書(shū)調(diào)用https的示例(java調(diào)用https)
- JAVA利用HttpClient進(jìn)行POST請(qǐng)求(HTTPS)實(shí)例
- java實(shí)現(xiàn)HttpClient異步請(qǐng)求資源的方法
- java發(fā)送HttpClient請(qǐng)求及接收請(qǐng)求結(jié)果過(guò)程的簡(jiǎn)單實(shí)例
- HttpClient 在Java項(xiàng)目中的使用詳解
- 使用java的HttpClient實(shí)現(xiàn)多線程并發(fā)
- 關(guān)于Http持久連接和HttpClient連接池的深入理解
相關(guān)文章
SpringBoot通過(guò)redisTemplate調(diào)用lua腳本并打印調(diào)試信息到redis log(方法步驟詳解)
這篇文章主要介紹了SpringBoot通過(guò)redisTemplate調(diào)用lua腳本 并打印調(diào)試信息到redis log,本文分步驟給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02Intellij IDEA 如何通過(guò)數(shù)據(jù)庫(kù)表生成帶注解的實(shí)體類(lèi)(圖文詳細(xì)教程)
這篇文章主要介紹了Intellij IDEA 如何通過(guò)數(shù)據(jù)庫(kù)表生成帶注解的實(shí)體類(lèi)(圖文詳細(xì)教程),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11基于Java的Socket多客戶端Client-Server聊天程序的實(shí)現(xiàn)
這篇文章主要介紹了基于Java的Socket多客戶端Client-Server聊天程序的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03Java實(shí)現(xiàn)的DES加密解密工具類(lèi)實(shí)例
這篇文章主要介紹了Java實(shí)現(xiàn)的DES加密解密工具類(lèi),結(jié)合具體實(shí)例形式分析了Java實(shí)現(xiàn)的DES加密解密工具類(lèi)定義與使用方法,需要的朋友可以參考下2017-09-09Spring Boot 2.7.6整合redis與低版本的區(qū)別
這篇文章主要介紹了Spring Boot 2.7.6整合redis與低版本的區(qū)別,文中補(bǔ)充介紹了SpringBoot各個(gè)版本使用Redis之間的區(qū)別實(shí)例講解,需要的朋友可以參考下2023-02-02java中的類(lèi)型擦除type?erasure示例詳解
泛型是java從JDK?5開(kāi)始引入的新特性,泛型的引入可以讓我們?cè)诖a編譯的時(shí)候就強(qiáng)制檢查傳入的類(lèi)型,從而提升了程序的健壯度,泛型可以用在類(lèi)和接口上,在集合類(lèi)中非常常見(jiàn),本文將會(huì)講解泛型導(dǎo)致的類(lèi)型擦除2023-09-09mybatis-plus添加數(shù)據(jù)時(shí)id自增問(wèn)題及解決
這篇文章主要介紹了mybatis-plus添加數(shù)據(jù)時(shí)id自增問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01