探讨:使用httpClient在客户端与服务器端传输对象参数的详解
程序员文章站
2023-12-18 08:32:40
昨天把httpclient的源代码下载来看了一下。 稍微跟踪了一下,最终还是使用java.net包的东西.不过封装的实在是漂亮.写程序方便多了。不过还是建议最好先熟悉net...
昨天把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
}
}
客户端:
复制代码 代码如下:
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
}
}