之前做了一个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无效。

相关文章

网站地图xml地图