去年2009-07-13在百度空间写的一篇hessian的使用总结,今天把它转过来
公司因业务需求准备开放一些API接口让代理商使用,周末抽了些时间了解了一下这方面的技术后,决定采用caucho.com的Hessian实现(hessian使用方便又高效)
测试环境
- Window XP
- JDK 1.6
- Resin3.1.9
- Spring2.0.8
- hessian-3.0.20.jar(这个版本要与spring的对应,不要一味的追求最新版,我因为这个,不知是好还是坏的毛病吃了N多苦头)
- HessianPHP-1.0.5-RC2
- Apache2.2
- PHP5.3.0
刚开始跑Java服务器端和客服端的测试都很顺利,但是当使用php5.3做为客户端访问Java时出现了好几个问题
- include_once '../dist/Hessian.php' ;
- include_once '../dist/HessianClient.php' ;
- Hessian :: mapRemoteType('com.hisupplier.showroom.webservice.QueryParams' , 'QueryParams' );
- Hessian :: mapRemoteType('com.hisupplier.showroom.webservice.ListResult' , 'ListResult' );
- Hessian :: mapRemoteType('com.hisupplier.showroom.entity.Product' , 'Product' );
- Hessian :: mapRemoteType('com.hisupplier.commons.page.Page' , 'Page' );
- try {
- $params = new QueryParams(114);
- $url = "http://guiyou.jiaming.com/webService" ;
- $proxy = new HessianClient( $url );
- echo "<br>" ;
- print_r($proxy ->hello( $params ));
- echo "<br>" ;
- print_r($proxy ->getProduct( $params ));
- echo "<br>" ;
- print_r($proxy ->getList( $params )); //要命的问题出在这里
- } catch (HttpError $ex ) {
- ...
- }
include_once '../dist/Hessian.php';
include_once '../dist/HessianClient.php';
Hessian :: mapRemoteType('com.hisupplier.showroom.webservice.QueryParams', 'QueryParams');
Hessian :: mapRemoteType('com.hisupplier.showroom.webservice.ListResult', 'ListResult');
Hessian :: mapRemoteType('com.hisupplier.showroom.entity.Product', 'Product');
Hessian :: mapRemoteType('com.hisupplier.commons.page.Page', 'Page');
try {
$params = new QueryParams(114);
$url = "http://guiyou.jiaming.com/webService";
$proxy = new HessianClient($url);
echo "<br>";
print_r($proxy->hello($params));
echo "<br>";
print_r($proxy->getProduct($params));
echo "<br>";
print_r($proxy->getList($params)); //要命的问题出在这里
} catch (HttpError $ex) {
...
}
- public interface ShowroomAPI {
- String hello(QueryParams params);
- ListResult<Product> getList(QueryParams params);
- Product getProduct(QueryParams params);
- }
public interface ShowroomAPI {
String hello(QueryParams params);
ListResult<Product> getList(QueryParams params);
Product getProduct(QueryParams params);
}
第1个问题
因为php5.2.x版本后自带了DateTime类,和 HessianPHP 中的发生冲突
解决: 改文件DateTime.php 为 HessianDateTime.php,类DateTime 为 HessianDateTime
第2个问题
PHP Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'UTC' for '8.0/no DST' instead in G://php//HessianPHP//dist//Hessian.php on line 74
解决: 将date() 方法都改为 date_default_timezone_set()
第3个问题
- Exception: Hessian Parser, Malformed reply: expected r Code: 2 exception 'HessianError' with message 'Hessian Parser, Malformed reply: expected r' in E:/workspace/php-test/dist/Protocol.php:339 Stack trace:
- #0 E:/workspace/php-test/dist/HessianClient.php(215): HessianParser->parseReply()
- #1 E:/workspace/php-test/dist/Filter.php(159): HessianProxy->executeCall('getList' , Array)
- #2 E:/workspace/php-test/dist/Filter.php(73): ProxyFilter->execute(Object(HessianProxy), Object(FilterChain))
- #3 E:/workspace/php-test/dist/Filter.php(191): FilterChain->doFilter(Object(HessianProxy))
- #4 E:/workspace/php-test/dist/Filter.php(73): PHPErrorReportingFilter->execute(Object(HessianProxy), Object(FilterChain))
- #5 E:/workspace/php-test/dist/HessianClient.php(182): FilterChain->doFilter(Object(HessianProxy))
- #6 E:/workspace/php-test/dist/HessianClient5.php(94): HessianProxy->call('getList' , Array)
- #7 [internal function ]: HessianClient->__call( 'getList' , Array)
- #8 E:/workspace/php-test/tests/test.php(23): HessianClient->getList(Object(QueryParams))
- #9 {main}
Exception: Hessian Parser, Malformed reply: expected r Code: 2 exception 'HessianError' with message 'Hessian Parser, Malformed reply: expected r' in E:/workspace/php-test/dist/Protocol.php:339 Stack trace:
#0 E:/workspace/php-test/dist/HessianClient.php(215): HessianParser->parseReply()
#1 E:/workspace/php-test/dist/Filter.php(159): HessianProxy->executeCall('getList', Array)
#2 E:/workspace/php-test/dist/Filter.php(73): ProxyFilter->execute(Object(HessianProxy), Object(FilterChain))
#3 E:/workspace/php-test/dist/Filter.php(191): FilterChain->doFilter(Object(HessianProxy))
#4 E:/workspace/php-test/dist/Filter.php(73): PHPErrorReportingFilter->execute(Object(HessianProxy), Object(FilterChain))
#5 E:/workspace/php-test/dist/HessianClient.php(182): FilterChain->doFilter(Object(HessianProxy))
#6 E:/workspace/php-test/dist/HessianClient5.php(94): HessianProxy->call('getList', Array)
#7 [internal function]: HessianClient->__call('getList', Array)
#8 E:/workspace/php-test/tests/test.php(23): HessianClient->getList(Object(QueryParams))
#9 {main}
google, baidu了半天也没找到相关的文章,后来把apache和php分别降到2.0和5.1还是不行,最后快放弃了试了一下yahoo,哦!my god佛祖保佑阿门,让我找了一了篇文章
Chunked http responses cause a protocol parse error
Http.php is written to perform an HTTP POST using HTTP/1.1 which means that
the Hessian client must support a HTTP header of "Transfer-Encoding:
chunked".
Protocol::parseReply() is written as follows:
if($this->read(1) != 'r') {
return new HessianError('Hessian Parser, Malformed reply: expected
r',HESSIAN_PARSER_ERROR,0,$this->stream);
}
which will fail because the first line of a chunked response will contain
the chunk length. Protocol::parseReply() needs to be written to support
chunking.
At the moment the workaround I have is to set HTTP/1.0 in Http::POST.
解决: 把Http.php中的1.1改为1.0
在Http.php第200行: $out = "POST $this->url HTTP/1.1/r/n";