c#创建webService, vc++ 调用

来源:互联网 发布:qq三国79js打架技巧 编辑:程序博客网 时间:2024/05/17 22:51

 1、安装 SoapToolkit

2、CString bBackQuestion;
  bBackQuestion =(const char*)Reader->Body->xml; //可以查看错误原因,

3、不能识别http头,导致返回Soap::Client问题,需要c#创建WebService时,添加申明

[SoapDocumentService(RoutingStyle = SoapServiceRoutingStyle.RequestElement)]

 

代码如下:(代码未经整理,不能直接运行。。。vc++用户可以稍微修改下即可)

c#代码不需要修改,直接创建 ASP WEB复制即可

c# 

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Configuration;
using System.Data.SqlClient;
using System.Collections;
using System.Runtime.InteropServices;


[WebService(Namespace = "http://mywebservice/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[SoapDocumentService(RoutingStyle = SoapServiceRoutingStyle.RequestElement)]
//[System.Web.Services.Protocols.SoapRpcService]


public class Service : System.Web.Services.WebService
{

    public Service () {

        //如果使用设计的组件,请取消注释以下行
        //InitializeComponent();
    }

    [WebMethod]
    //返回提取新号码的一些信息
    public string test()
    {        

        return "succeful~";

    }

}

 

vc++: .h

#import "msxml4.dll"
#import "mssoap30.dll" \
exclude("IStream", "IErrorInfo", "ISequentialStream", "_LARGE_INTEGER", \
"_ULARGE_INTEGER", "tagSTATSTG", "_FILETIME")

using namespace MSXML2;
using namespace MSSOAPLib30;

 

 

.cpp


CString CFD_Soap::strGet_DataSet(LPCTSTR lFunc, LPCTSTR pParam)
{
 CString strFunc(_T("")), strParam(_T(""));
 if(lFunc == NULL)
  strFunc = m_strFunc;
 if (strFunc.IsEmpty())
  return _T("");

 if (pParam)
  strParam = pParam;
 else
  strParam = m_strParam;

 m_strURL = "http://localhost/Queue/Service.asmx";
 m_strNs = "http://mywebservice/"; 
 strFunc = _T("webGetSrv");

 CString strResult(_T(""));
 HRESULT hr = CoInitialize(NULL);//³õʼ»¯com»·¾³
    if(FAILED(hr))
  return _T("");
    ISoapSerializerPtr Serializer;
    ISoapReaderPtr Reader;
    ISoapConnectorPtr Connector;
 
 try
 {
  //Á¬½Óµ½WebService
  hr = Connector.CreateInstance(__uuidof(HttpConnector30));
  if(FAILED(hr))
   return strResult; 

  Connector->Property["EndPointURL"] = (_variant_t)m_strURL;
  hr = Connector->Connect();
  Connector->Property["SoapAction"] = "";//(_variant_t)m_strNs;
  
  //¿ªÊ¼´´½¨webserviceµÄÇëÇóSoap°ü
  hr = Connector->BeginMessage();
  hr = Serializer.CreateInstance(__uuidof(SoapSerializer30));
  if(FAILED(hr))
   return strResult;
  hr = Serializer->Init(_variant_t((IUnknown*)Connector->InputStream));
  hr = Serializer->StartEnvelope("", "", "");
  
  hr = Serializer->StartBody("");  
  hr = Serializer->StartElement((_bstr_t)strFunc, (_bstr_t)m_strNs, "", "");
  
  if(!strParam.IsEmpty())
  {
   Serializer->StartElement("info", (_bstr_t)m_strNs, "", "");
   Serializer->WriteString((_bstr_t)strParam);
   Serializer->EndElement();
  }
  
  
  hr = Serializer->EndElement(); 
  hr = Serializer->EndBody();
  hr = Serializer->EndEnvelope();
  hr = Connector->EndMessage(); //ÉÏ´«ÇëÇó
  //½âÎö·µ»ØµÄsoap°ü
  hr = Reader.CreateInstance(__uuidof(SoapReader30));
  if(FAILED(hr))
   return strResult;
  Reader->Load(_variant_t((IUnknown*)Connector->OutputStream), "");
  strResult = (LPCTSTR)Reader->RpcResult->text;
  CString bBackQuestion;
  bBackQuestion =(const char*)Reader->Body->xml;
 }
 catch (_com_error& e)
 {
  CString bBackQuestion;
  bBackQuestion =(const char*)Reader->Body->xml; //ʧ°ÜÔ­Òò
  TRACE(e.ErrorMessage());
  return _T("") ;
 }
    return strResult;
}

 

原创粉丝点击