原创作者: 马秉尧   阅读:1494次   评论:0条   更新时间:2011-06-01    
异常处理

Hprose for Hprose的客户端只支持同步调用,因此在调用过程中,如果服务器端发生错误,异常将在客户端被直接抛出,使用try...catch语句块即可捕获异常,通常服务器端调用返回的异常是HproseException类型。但是在调用过程中也可能抛出其它类型的异常。

例如,当调用不存在的方法时:
<?php
include("hprose/hproseHttpClient.php");
$client = new HproseHttpClient("http://www.hprose.com/example/");
echo "<pre>";
try {
  echo $client->unexistMethod();
}
catch (Exception $e) {
  print_r($e);
}
echo "</pre>";
?>

运行结果如下:
引用
HproseException Object
(
    [message:protected] => Can't find this function unexistmethod().
file: D:\phpox\hprose\www\example\hproseHttpServer.php
line: 152
trace: #0 D:\phpox\hprose\www\example\hproseHttpServer.php(374): HproseHttpServer->doInvoke()
#1 D:\phpox\hprose\www\example\hproseHttpServer.php(391): HproseHttpServer->handle()
#2 D:\phpox\hprose\www\example\index.php(58): HproseHttpServer->start()
#3 {main}
    [string:private] =>
    [code:protected] => 0
    [file:protected] => /var/www/hprose/hproseHttpClient.php
    [line:protected] => 165
    [trace:private] => Array
        (
            [0] => Array
                (
                    [file] => /var/www/hprose/hproseHttpClient.php
                    [line] => 191
                    [function] => invoke
                    [class] => HproseHttpClient
                    [type] => ->
                    [args] => Array
                        (
                            [0] => unexistMethod
                            [1] => Array
                                (
                                )

                        )

                )

            [1] => Array
                (
                    [function] => __call
                    [class] => HproseHttpClient
                    [type] => ->
                    [args] => Array
                        (
                            [0] => unexistMethod
                            [1] => Array
                                (
                                )

                        )

                )

            [2] => Array
                (
                    [file] => /var/www/exam4.php
                    [line] => 6
                    [function] => unexistMethod
                    [class] => HproseHttpClient
                    [type] => ->
                    [args] => Array
                        (
                        )

                )

        )

)


HTTP参数设置

目前的版本只提供了http客户端实现,针对于http客户端,有一些特别的设置,例如代理服务器、持久连接、http标头等设置,下面我们来分别介绍。

代理服务器

默认情况下,代理服务器是被禁用的。可以通过setProxy来设置http代理服务器的地址和端口。参数是字符串,例如:"tcp://10.54.1.39:8000",默认值为NULL。

HTTP标头

有时候您可能需要设置特殊的http标头,例如当您的服务器需要Basic认证的时候,您就需要提供一个Authorization标头。设置标头很简单,只需要调用setHeader方法就可以啦,该方法的第一个参数为标头名,第二个参数为标头值,这两个参数都是字符串型。如果将第二个参数设置为NULL,则表示删除这个标头。

标头名不可以为以下值:
  • Context-Type
  • Context-Length

因为这些标头有特别意义,客户端会自动设定这些值。

另外,Cookie这个标头不要轻易去设置它,因为设置它会影响Cookie的自动处理,如果您的通讯中用到了Session,通过setHeader方法来设置Cookie标头,将会影响Session的正常工作。
评论 共 0 条 请登录后发表评论

发表评论

您还没有登录,请您登录后再发表评论

文章信息

  • andot在2010-01-26创建
  • andot在2011-06-01更新
  • 标签: hprose, php
Global site tag (gtag.js) - Google Analytics