www.gusucode.com > 同城苏州黄页系统php源码程序 > mod.js/http_soap_client.class.js

    HTTP_SOAP_API = '/http-soap-api.php' ;

/*
#########################################################
#
#  http_soap_client		 for js
#
#		alee 2006.4.12
#########################################################
*/


/*
######################################
## 构造函数							##
######################################
*/
function http_soap_client( http_soap_server_api, time_out )
{
	// 服务器 通讯接口
	this.http_soap_server_api = http_soap_server_api
	
	// 对象 有效期
	this.time_out = ( typeof(time_out) == 'undefined' ) ? 600 : time_out ;
	
	// 只读对象 标记
	this.readonly = 0 ;

	// 远程对象ID
	this.ObjID = '' ;

	// 通讯 客户端 类型
	this.client = 'js' ;
	
	this.HttpSend = new ActiveXObject("Microsoft.XMLHttp");
	
	this.except_word = '' ;
	
	this.stream = '' ;
}

/*
######################################
## 公有函数							##
## Public Function					##
######################################
*/
// 创建对象
http_soap_client.prototype.create = function( class_name, parameters )
{
	
	data = {'action':'create','class_name':class_name}
	if( typeof( parameters ) != 'undefined' )
		data['parameters'] = parameters ;
	this.ObjID = this._post_data( data, true );

	return this.ObjID ;
}

// 调用远程对象方法
http_soap_client.prototype.method_invoke = function( func_name, parameters, if_sync, ObjID )
{

	if( typeof(ObjID) == 'undefined' )
		ObjID = this.ObjID ;
	if( typeof(if_sync) == 'undefined' )
		if_sync = true ;
	if( typeof(parameters) == 'undefined' )
		if_sync = {} ;

	result = this._post_data( {'action':'method_invoke','ObjID':ObjID,'func_name':func_name,'parameters':parameters}, if_sync );
	return result ;
}

// 获得远程对象属性
http_soap_client.prototype.get_attribute = function( attribute_name, ObjID )
{
	if( typeof(ObjID) == 'undefined' )
		ObjID = this.ObjID ;

	return this._post_data( {'action':'get_attribute','ObjID':ObjID,'attribute_name':attribute_name}, true );
}

// 设置远程对象属性
http_soap_client.prototype.set_attribute = function( attribute_name, attribute_value, if_sync, ObjID )
{
	if( typeof(ObjID) == 'undefined' )
		ObjID = this.ObjID ;
	if( typeof(if_sync) == 'undefined' )
		if_sync = true ;

	data = {
		'action':'set_attribute',
		'ObjID':ObjID,
		'attribute_name':attribute_name,
		'attribute_value':attribute_value
	} ;

	return this._post_data( data, if_sync );
}

// 销毁对象
http_soap_client.prototype.destroy = function( ObjID )
{
	if( typeof(ObjID) == 'undefined' )
		ObjID = this.ObjID ;

	return this._post_data( {'action':'destroy','ObjID':ObjID}, true );
}

// 消息输出
http_soap_client.prototype.out = function( msg )
{
	outbox = document.createElement('div') ;
	outbox.innerHTML = '<textarea cols="120" rows="15" wrap="off">' + msg + '</textarea>' ;
	window.document.body.insertAdjacentElement( 'BeforeEnd', outbox );
	// document.write( box = '<textarea cols="60" rows="10" wrap="off">' + msg + '</textarea>' );
}
http_soap_client.prototype.out_stream = function()
{
	this.out( 'http_soap_client Stream Body : \n' + this.stream ) ;
}
http_soap_client.prototype.out_exception = function()
{
	if( this.except_word == '' )
		return false ;
	this.out( 'http_soap 遇到意外 : \n' + this.except_word ) ;
	//document.write('http_soap 遇到意外 : \n' + this.except_word) ;
}
/*
######################################
## 私有函数 - 请勿使用!				##
## Private Function					##
######################################
*/
http_soap_client.prototype._post_data = function( data, if_sync )
{
	if( typeof(data) == 'undefined' )
		data = {} ;
	if( typeof(if_sync) == 'undefined' )
		if_sync = true ;

	// 一些必要的参数
	dataStr = 'sync=' + if_sync + '&client=' + this.client + '&readonly=' + this.readonly

	dataStr = dataStr + '&' + serialize_request_data( data ) ;
	// alert(dataStr);

	// 发送
	this.HttpSend.open( "POST", this.http_soap_server_api, !if_sync );
	this.HttpSend.setRequestHeader("CONTENT-TYPE","application/x-www-form-urlencoded");
													//如果需要 get/post 中文參數到服務器,必須出現次行,否則服務器端出現編碼錯誤。
	this.HttpSend.send( dataStr );					//發送查詢信息

	// 接收
	if( !if_sync )
		return ;

	s = ( this.HttpSend.responseText.slice(0,4) == '\r\n\r\n' ) ?
			this.HttpSend.responseText.slice(4) :	// 去处 http 中 head 和 body 之间 的 \r\n\r\n
			this.HttpSend.responseText ;
	this.stream = s ;
	// alert(this.HttpSend.responseText);
	ss = s.split("\r\n--- http soap server exception msg start ---\r\n") ;

	if( ss.length < 2 )
	{
		this.out( s ) ;
		return '' ;
	}

	if( ss[0] != '' )
		this.out( ss[0] ) ;

	sss = ss[1].split("\r\n--- http soap server response start ---\r\n") ;

	except = this._unserialize( sss[0] ) ;
	this.except_word = '' ;
	for( var idx in except )
		this.except_word += except[ idx ] ;

	return_str = this._unserialize( sss[1] ) ;
	return return_str ;
}

http_soap_client.prototype._unserialize = function( str )
{
	if( typeof(str) == 'undefined' || str == 'undefined' || sss[1] == '' )
		return '' ;
	else( sss[1] != '' )
	{
		str = strdecode(str) ;
		if( str.slice(0,1) == '{' && str.slice(-1) == '}' )
			script = 'str = ' + str ;
		else
			script = 'str = "' + str + '"' ;

		// this.out( script ) ;
		eval( script ) ;

	}

	return str ;
}


//////////////////////////////////////////////////////////////////////


function serialize_request_data( request, val_name_pre )
{

	var restr = '';									//pp($request);sleep(10);
	for( var key in request )
	{
		val = request[ key ] ;

		if ( typeof(val_name_pre) != 'undefined' )
			key = val_name_pre + '%5B' + key + '%5D' ;
		
		if( restr != '' )
			restr = restr + '&' ;

		if( typeof(val) == 'object' )
		{
			return_val = serialize_request_data( val, key ) ;

			if( return_val == '' )
				continue ;

			restr = restr + return_val;
			
		}
		else
			restr = restr + key + '=' + val ;
		

	}
	
	return restr;
}



function utf16to8(str) {
    var out, i, len, c;

    out = "";
    len = str.length;
    for(i = 0; i < len; i++) {
        c = str.charCodeAt(i);
        if ((c >= 0x0001) && (c <= 0x007F)) {
            out += str.charAt(i);
        } else if (c > 0x07FF) {
            out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
            out += String.fromCharCode(0x80 | ((c >>  6) & 0x3F));
            out += String.fromCharCode(0x80 | ((c >>  0) & 0x3F));
        } else {
            out += String.fromCharCode(0xC0 | ((c >>  6) & 0x1F));
            out += String.fromCharCode(0x80 | ((c >>  0) & 0x3F));
        }
    }
    return out;
}


/* utf.js - UTF-8 <: UTF-16 convertion
*
* Copyright (C) 1999 Masanao Izumo <iz@onicos.co.jp>
* Version: 1.0
* LastModified: Dec 25 1999
* This library is free.  You can redistribute it and/or modify it.
*/

/*
* Interfaces:
* utf8 = utf16to8(utf16);
* utf16 = utf16to8(utf8);
*/

function utf8to16(str) {
    var out, i, len, c;
    var char2, char3;

    out = "";
    len = str.length;
    i = 0;
    while(i < len) {
        c = str.charCodeAt(i++);
        switch(c >> 4)
        { 
          case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
            // 0xxxxxxx
            out += str.charAt(i-1);
            break;
          case 12: case 13:
            // 110x xxxx   10xx xxxx
            char2 = str.charCodeAt(i++);
            out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
            break;
          case 14:
            // 1110 xxxx  10xx xxxx  10xx xxxx
            char2 = str.charCodeAt(i++);
            char3 = str.charCodeAt(i++);
            out += String.fromCharCode(((c & 0x0F) << 12) |
                                           ((char2 & 0x3F) << 6) |
                                           ((char3 & 0x3F) << 0));
            break;
        }
    }

    return out;
}

/* Copyright (C) 1999 Masanao Izumo <iz@onicos.co.jp>
* Version: 1.0
* LastModified: Dec 25 1999
* This library is free.  You can redistribute it and/or modify it.
*/

/*
* Interfaces:
* b64 = base64encode(data);
* data = base64decode(b64);
*/


var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var base64DecodeChars = new Array(
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
    52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
    -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
    15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
    -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
    41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1);

function base64encode(str) {
    var out, i, len;
    var c1, c2, c3;

    len = str.length;
    i = 0;
    out = "";
    while(i < len) {
        c1 = str.charCodeAt(i++) & 0xff;
        if(i == len)
        {
            out += base64EncodeChars.charAt(c1 >> 2);
            out += base64EncodeChars.charAt((c1 & 0x3) << 4);
            out += "==";
            break;
        }
        c2 = str.charCodeAt(i++);
        if(i == len)
        {
            out += base64EncodeChars.charAt(c1 >> 2);
            out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
            out += base64EncodeChars.charAt((c2 & 0xF) << 2);
            out += "=";
            break;
        }
        c3 = str.charCodeAt(i++);
        out += base64EncodeChars.charAt(c1 >> 2);
        out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
        out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6));
        out += base64EncodeChars.charAt(c3 & 0x3F);
    }
    return out;
}

function base64decode(str) {
    var c1, c2, c3, c4;
    var i, len, out;

    len = str.length;
    i = 0;
    out = "";
    while(i < len) {
        /* c1 */
        do {
            c1 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
        } while(i < len && c1 == -1);
        if(c1 == -1)
            break;

        /* c2 */
        do {
            c2 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
        } while(i < len && c2 == -1);
        if(c2 == -1)
            break;

        out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));

        /* c3 */
        do {
            c3 = str.charCodeAt(i++) & 0xff;
            if(c3 == 61)
                return out;
            c3 = base64DecodeChars[c3];
        } while(i < len && c3 == -1);
        if(c3 == -1)
            break;

        out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2));

        /* c4 */
        do {
            c4 = str.charCodeAt(i++) & 0xff;
            if(c4 == 61)
                return out;
            c4 = base64DecodeChars[c4];
        } while(i < len && c4 == -1);
        if(c4 == -1)
            break;
        out += String.fromCharCode(((c3 & 0x03) << 6) | c4);
    }
    return out;
}
//input base64 encode
function strdecode(str)
{
	if( str == '' )
		return '""' ;
	return utf8to16(base64decode(str));
}



http_soap_client_loaded = true ;