Use Hessian PHP to communicate with Java

Source: Internet
Author: User

A Hessian usage summary written in Baidu space last year on. Today, I transferred it

The company was prepared to open some API interfaces for agents to use due to business needs. After taking some time to understand the technology over the weekend, the company decided to use Hessian of caucho.com (Hessian is convenient and efficient)

Test Environment

  • Window XP
  • JDK 1.6
  • Resin3.1.9
  • Spring2.0.8
  • Hessian-3.0.20.jar (this version should correspond with spring, do not blindly pursue the latest version, I am because of this, I do not know whether it is good or bad problems eat n more bitter)
  • HessianPHP-1.0.5-RC2
  • Apache2.2
  • Php5.3.0


The test on the Java Server and customer service was successful at the beginning, but several problems occurred when php5.3 was used as the client to access Java.

PHP code
  1. Include_once
     
    '../Dist/Hessian. php'
    ;
  2. Include_once
     
    '../Dist/hessianclient. php'
    ;
  3. Hessian: mapremotetype ('com. hisupplier. showroom. WebService. queryparams'
    ,
    'Queryparams'
    );
  4. Hessian: mapremotetype ('com. hisupplier. showroom. WebService. listresult'
    ,
    'Listresult'
    );
  5. Hessian: mapremotetype ('com. hisupplier. showroom. entity. product'
    ,
    'Product'
    );
  6. Hessian: mapremotetype ('com. hisupplier. commons. Page. page'
    ,
    'Page'
    );
  7. Try {
  8. $ Params
    =
    New
    Queryparams (114 );
  9. $ URL
    =
    Http://guiyou.jiaming.com/webService"
    ;
  10. $ Proxy
    =
    New
    Hessianclient (
    $ URL
    );
  11. Echo
     
    "<Br>"
    ;
  12. Print_r ($ proxy
    -> Hello (
    $ Params
    ));
  13. Echo
     
    "<Br>"
    ;
  14. Print_r ($ proxy
    -> Getproduct (
    $ Params
    ));
  15. Echo
     
    "<Br>"
    ;
  16. Print_r ($ proxy
    -> Getlist (
    $ Params
    ));
    // The terrible problem lies here.

  17. } Catch (httperror $ ex
    ){
  18. ...
  19. }
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); // The Problem Is that} catch (httperror $ ex ){...}

 

Java code
  1. Public
     
    Interface
    Showroomapi {
  2. String Hello (queryparams Params );
  3. Listresult <product> getlist (queryparams Params );
  4. Product getproduct (queryparams Params );
  5. }
public interface ShowroomAPI {    String hello(QueryParams params);    ListResult<Product> getList(QueryParams params);    Product getProduct(QueryParams params);}


1st questions

Because php5.2.x comes with the datetime class, it conflicts with hessianphp.

Solution:
Change datetime. php to hessiandatetime. php, and datetime to hessiandatetime.

2nd questions

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
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

Solution:
Change the date () method to date_default_timezone_set ()

3rd questions

PHP code
  1. 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:
  2. #0 E:/workspace/PHP-test/Dist/hessianclient. php (215): hessianparser-> parsereply ()
  3. #1 E:/workspace/PHP-test/Dist/filter. php (159): hessianproxy-> executecall ('getlist'
    , Array)
  4. #2 E:/workspace/PHP-test/Dist/filter. php (73): proxyfilter-> execute (Object (hessianproxy), object (filterchain ))
  5. #3 E:/workspace/PHP-test/Dist/filter. php (191): filterchain-> dofilter (Object (hessianproxy ))
  6. #4 E:/workspace/PHP-test/Dist/filter. php (73): phperrorreportingfilter-> execute (Object (hessianproxy), object (filterchain ))
  7. #5 E:/workspace/PHP-test/Dist/hessianclient. php (182): filterchain-> dofilter (Object (hessianproxy ))
  8. #6 E:/workspace/PHP-test/Dist/hessianclient5.php (94): hessianproxy-> call ('getlist'
    , Array)
  9. #7 [internal function
    ]: Hessianclient->__ call (
    'Getlist'
    , Array)
  10. #8 E:/workspace/PHP-test/tests/test. php (23): hessianclient-> getlist (Object (queryparams ))
  11. #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 and Baidu haven't found any related articles for a long time. After that, they reduced Apache and PHP to 2.0 and 5.1 respectively. Finally, they gave up trying Yahoo! My God Buddha blessed Amen and asked me to find an article

Reference
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.


Solution:
Change 1.1 in HTTP. php to 1.0

In the HTTP. php 200th line: $ out = "post $ this-> url http/1.1/R/N ";

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.