Hessian测试报告
一、说明
主要是测试客户端和服务端的通信,客户端使用C++(hessiancpp),服务端使用Java(Java版Hessian)。
Hessian官方网站:http://hessian.caucho.com/ 可在官方网站下载Hessian的源代码
Hessiancpp的官方文档中已经说明了,要使用Hessian要用到其它第三方的库,首先考虑到本测试是为了测试在Windows
Mobile和Symbian上使用Hessian对服务器访问,文档中提到的第三方库可能无法直接在这两个平台上直接运行,所以决定抽取Hessian中的一部份出来,去掉了可能我们在应用中不会使用到的功能。
hessiancpp was
developed on a Fedora Core 2 x86_64 system, using
* GCC 3.3.3
* OpenSSL 0.9.7a
* BOOST 1.31.0
* SSLPP as a shared
library
* ZLIB 1.2.1.1
release 2.1
and ported to
Windows XP and 2000 Server, using
* Microsoft C++
13.10 (VS.NET 2003)
* OpenSSL 0.9.7d
* BOOST 1.31.0
* SSLPP as a static
library
* ZLIB 1.2.1
二、精简版Hessian
客户端(hessiancpp)方面去掉了hessian_proxy和用到了zlib一些代码和类,然后稍微的修改了部分代码。
服务端(java版Hessain)方面比较简单,只是把com.caucho.hessian.micro这个包里的两个文件(MicroHessianInput.java和MicroHessianOutput.java)提取出来了,没有做任何的修改。
提取后的精简版Hessian可以在后面的附件得到。
三、测试Hessian
本测试只是一个简单的测试,由客户端发送两个字符串(用户名和密码)到服务器,服务器接收到数据后验证用户名和密码是否正确,然后返回一个布尔值(验证结果)和一个字符串(欢迎信息或错误信息)到客户端。
1、服务端主要代码
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
InputStream is = request.getInputStream();
OutputStream os = response.getOutputStream();
//读取从客户端传过来的数据
MicroHessianInput hessianInput = new MicroHessianInput(is);
String user = hessianInput.readString();
String password = hessianInput.readString();
//返回数据至客户端
MicroHessianOutput hessianOutput = new MicroHessianOutput(os);
if(user.compareTo("admin") == 0 && password.compareTo("123456789") == 0)
{
hessianOutput.writeBoolean(true);
hessianOutput.writeString("suc");
}
else
{
hessianOutput.writeBoolean(false);
hessianOutput.writeString("fail");
}
}
2、客户端主要代码
string sendvalue;
hessian_output hes_out;
hes_out.write_string(sendvalue,"admin");
hes_out.write_string(sendvalue,"123456789");
string databuff = sendvalue;
int datalength = sendvalue.size();
CString strHeaders = _T("Content-Type: application/x-www-form-urlencoded");
CInternetSession session;
CHttpConnection* pConnection = session.GetHttpConnection(_T("127.0.0.1"),INTERNET_FLAG_NO_AUTO_REDIRECT,8080);
CHttpFile* fileGet = pConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST,_T("/myapp/TestHessianServlet"));
DWORD dwStatus;
DWORD dwBuffLen=sizeof(dwStatus);
CString strHtml,strSentence;
BOOL result = fileGet->SendRequest(strHeaders,(LPVOID)databuff.c_str(),datalength);
DWORD retcode;
fileGet->QueryInfoStatusCode(retcode);
if(retcode==HTTP_STATUS_OK)
{
char szBuff[4096];
memset(szBuff,0,4096);
UINT nRead;
while ((nRead = (fileGet)->Read(szBuff,4096)))
{
if(nRead)
{
strHtml += CString(szBuff,nRead);
}
if(nRead<4096)
continue;
}
string hesStremStr;
hesStremStr.resize(strHtml.GetLength()+1,0);
const char *c = hesStremStr.c_str();
char *ch=const_cast<char *>(c);
int hesLength = hesStremStr.length();
for(int i = 0;i < hesLength;i++)
{
if(i != hesLength - 1)
ch[i] = szBuff[i];
else
ch[i] = '\0';
}
auto_ptr<input_stream> i_stream(new string_input_stream(hesStremStr));
hessian_input hes_in(i_stream);
bool ret = hes_in.read_boolean();
string desc = hes_in.read_string();
if(ret)
{
cout << "登录成功!" << endl;
}
else
{
cout << "登录失败!" << endl;
}
cout << "服务器返回信息:" << desc << endl;
}
3、测试
客户端发送用户名:admin,密码:123456789到服务器,服务器返回true,suc。
客户端发送用户名:admin0,密码:1234567890到服务器,服务器返回false,fail。
四、测试结果
经测试在客户端和服务器之间使用精简版的Hessian的通信成功。