vc++ 访问php webService

   之前做了一个VC++访问c#制作的WebService,没有问题,接着我又做了一个VC++访问php制作的WebService ,
结果老是出现Client错误。这个php WebService是用ZendStudio制作的,后来采用NUSoap做php的WebService就没问题。
以下为整个过程:
NUSoap WebService自作.
参考网址: http://blog.csdn.net/raining_peidx/archive/2009/07/27/4384600.aspx
1.php WebService 文件: nusoap_server3.php,以下为代码:


<? php
require_once ( " lib/nusoap.php " );
function  AddNumbers( $NumberOne , $NumberTwo )
{
    
return   $NumberOne + $NumberTwo ;
}
$soap   =   new  soap_server;
 
//  初始化对 WSDL 的支持
$soap -> configureWSDL( ' AddNumbers ' );
//  注册服务
$soap -> register( ' AddNumbers ' ,
array ( " NumberOne " => " xsd:int " , " NumberTwo " => " xsd:int " ) ,
array ( " retrun " => " xsd:int " )           
);
$HTTP_RAW_POST_DATA   =   isset ( $HTTP_RAW_POST_DATA ?   $HTTP_RAW_POST_DATA   :   '' ;
$soap -> service( $HTTP_RAW_POST_DATA );
?>  
2.VC客户端制作,以下是控制台应用程序制作。
#include  " stdafx.h "
#include 
< stdio.h >
#include 
< stdlib.h >

// 先导入msxml4.dll,再导入mssoap30.dll
#import  " msxml4.dll "  
#import 
" C:\Program Files\Common Files\MSSoap\Binaries\mssoap30.dll "  \
    exclude(
" IStream " " IErrorInfo " " ISequentialStream " " _LARGE_INTEGER " , \
    
" _ULARGE_INTEGER " " tagSTATSTG " " _FILETIME " )
using   namespace  MSXML2;
using   namespace  MSSOAPLib30; 

void  Add( int  No1, int  No2)

{
    
// char *_itoa( int value, char *string, int radix );

    
char  str1[ 10 ],str2[ 10 ];

    itoa(No1,str1,
10 ); itoa(No2,str2, 10 );

    ISoapSerializerPtr Serializer;

    ISoapReaderPtr Reader;

    ISoapConnectorPtr Connector;    

    
//  Connect to the service.

    Connector.CreateInstance(__uuidof(HttpConnector30));   


    Connector
-> Property[ " EndPointURL " =   " http://localhost/test/nusoap/nusoap_server3.php " ;  
    Connector
-> Connect();



    
    
// Connector->Property["SoapAction"] = " http://Fantasy/WebServiceDemo/AddNumbers ";
    Connector -> Property[ " SoapAction " =   " http://localhost/test/nusoap/nusoap_server3.php/AddNumbers " ;
    Connector
-> BeginMessage();      

    
//  Create the SoapSerializer object.

    Serializer.CreateInstance(__uuidof(SoapSerializer30));

    
//  Connect the serializer object to the input stream of the connector object.

    Serializer
-> Init(_variant_t((IUnknown * )Connector -> InputStream));

    
//  Build the SOAP Message.

    Serializer
-> StartEnvelope( "" , "" , "" );

    
// 这里可以添加SoapHeader验证代码:

    
/*

    Serializer->StartHeader("");

    Serializer->StartHeaderElement("MyHeader","
http://Fantasy/WebServiceDemo/ ",0,"","NONE","");   

    Serializer->StartElement("UserName","","","");//填充参数2则出错。

    Serializer->WriteString(_bstr_t(userName));

    
*/

    Serializer
-> StartBody( "" );   

    
// Serializer->StartElement("AddNumbers","  http://localhost/test/soap/Server.php/ ","","");
    
// Serializer->StartElement("AddNumbers"," http://Fantasy/WebServiceDemo/ ","","");
    Serializer -> StartElement( " AddNumbers " , "" , "" , "" );

    
// (第三个参数可有可无,若加上第二个参数出错!?)

    Serializer
-> StartElement( " NumberOne " , "" , "" , "" ); //  NumberOne=

    Serializer
-> WriteString((_bstr_t)str1);

    Serializer
-> EndElement();      

    Serializer
-> StartElement( " NumberTwo " , "" , "" , "" ); //  NumberOne= 

    Serializer
-> WriteString((_bstr_t)str2);

    Serializer
-> EndElement();

    Serializer
-> EndElement();  

    
//

    Serializer
-> EndBody();

    Serializer
-> EndEnvelope();

    
//  Send the message to the XML Web service.   

    Connector
-> EndMessage();   

    
//  Read the response. 

    Reader.CreateInstance(__uuidof(SoapReader30));    

    
//  Connect the reader to the output stream of the connector object.  

    Reader
-> Load(_variant_t((IUnknown * )Connector -> OutputStream),  "" );    

    
//  Display the result.

    printf(
" Answer = %s\n " , ( const   char * )Reader -> RpcResult -> text);  

}

int  main()

{   

    CoInitialize(NULL);   

    Add(
1211 , 21114 );  

    CoUninitialize();

    
return   0 ;

}

注意:此方法只对NUSOAP制作的WebService有效,对zendStudio制作的WebService无效。

你可能感兴趣的:(webservice)