HRESULT hr;
try
{

CComPtr<ISoapConnector> SoapConnector;
//创建SoapConnector类的对象
SoapConnector.CoCreateInstance(CLSID_HttpConnector30);

//指定Web服务的地址
SoapConnector->put_Property(CComBSTR("EndPointURL"),_variant_t("http://172.18.70.32:82/MobileService/"));

//与Web服务连接
hr=SoapConnector->Connect();

//指定Web服务完成的操作
SoapConnector->put_Property(CComBSTR("SoapAction"),_variant_t(_T("http://tempuri.org/IMobileService/GetCXY")));

//准备发送消息给Web服务
SoapConnector->BeginMessage();

// 创建SoapSerializer对象
CComPtr<ISoapSerializer> Serializer;
Serializer.CoCreateInstance(CLSID_SoapSerializer30);

// 将serializer连接到connector的输入字符串
IStreamPtr pStream;
SoapConnector->get_InputStream(&pStream);
Serializer->Init(_variant_t((IUnknown*)pStream));

// 创建SOAP消息
Serializer->StartEnvelope(CComBSTR("Soap"),CComBSTR(""),CComBSTR("")); Serializer->StartBody(CComBSTR(""));

Serializer->StartElement(CComBSTR("GetXY"),CComBSTR("http://tempuri.org/"),CComBSTR(""),CComBSTR("Soap"));   // 消息函数名称

Serializer->StartElement(CComBSTR("phone"),CComBSTR(""),CComBSTR(""),CComBSTR("Soap")); // 函数参数

Serializer->WriteString(CComBSTR("13031040117")); //参数处理      

Serializer->EndElement();

Serializer->EndElement();

Serializer->EndBody(); Serializer->EndEnvelope();

//消息真正地发给Web服务
hr = SoapConnector->EndMessage(); //如果访问不到WebService或网速慢,就会导致程序异常。  if (FAILED(hr))
{
return ;
}
// 读取响应
CComPtr<ISoapReader> Reader;
Reader.CoCreateInstance(CLSID_SoapReader30);

// Connect the reader to the output stream of the connector object.
IStreamPtr pStream2;
SoapConnector->get_OutputStream(&pStream2);
这里报错. 网上查说是访问不到这个webs, 或者网速慢, 可我的webs 部署在本机啊
另外请看下我的wsdl 描述吧
webservicewsdlsoap异常

解决方案 »

  1.   

      <?xml version="1.0" encoding="utf-8" ?> 
    - <wsdl:definitions name="MobileService" targetNamespace="http://tempuri.org/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:tns="http://tempuri.org/" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsap="http://schemas.xmlsoap.org/ws/2004/08/addressing/policy" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msc="http://schemas.microsoft.com/ws/2005/12/wsdl/contract" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:wsa10="http://www.w3.org/2005/08/addressing" xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex">
    - <wsp:Policy wsu:Id="WSHttpBinding_IMobileService_policy">
    - <wsp:ExactlyOne>
    - <wsp:All>
    - <sp:SymmetricBinding xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
    - <wsp:Policy>
    - <sp:ProtectionToken>
    - <wsp:Policy>
    - <sp:SecureConversationToken sp:IncludeToken="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy/IncludeToken/AlwaysToRecipient">
    - <wsp:Policy>
      <sp:RequireDerivedKeys /> 
    - <sp:BootstrapPolicy>
    - <wsp:Policy>
    - <sp:SignedParts>
      <sp:Body /> 
      <sp:Header Name="To" Namespace="http://www.w3.org/2005/08/addressing" /> 
      <sp:Header Name="From" Namespace="http://www.w3.org/2005/08/addressing" /> 
      <sp:Header Name="FaultTo" Namespace="http://www.w3.org/2005/08/addressing" /> 
      <sp:Header Name="ReplyTo" Namespace="http://www.w3.org/2005/08/addressing" /> 
      <sp:Header Name="MessageID" Namespace="http://www.w3.org/2005/08/addressing" /> 
      <sp:Header Name="RelatesTo" Namespace="http://www.w3.org/2005/08/addressing" /> 
      <sp:Header Name="Action" Namespace="http://www.w3.org/2005/08/addressing" /> 
      </sp:SignedParts>
    - <sp:EncryptedParts>
      <sp:Body /> 
      </sp:EncryptedParts>
    - <sp:SymmetricBinding>
    - <wsp:Policy>
    - <sp:ProtectionToken>
    - <wsp:Policy>
    - <sp:SpnegoContextToken sp:IncludeToken="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy/IncludeToken/AlwaysToRecipient">
    - <wsp:Policy>
      <sp:RequireDerivedKeys /> 
      </wsp:Policy>
      </sp:SpnegoContextToken>
      </wsp:Policy>
      </sp:ProtectionToken>
    - <sp:AlgorithmSuite>
    - <wsp:Policy>
      <sp:Basic256 /> 
      </wsp:Policy>
      </sp:AlgorithmSuite>
    - <sp:Layout>
    - <wsp:Policy>
      <sp:Strict /> 
      </wsp:Policy>
      </sp:Layout>
      <sp:IncludeTimestamp /> 
      <sp:EncryptSignature /> 
      <sp:OnlySignEntireHeadersAndBody /> 
      </wsp:Policy>
      </sp:SymmetricBinding>
    - <sp:Wss11>
    - <wsp:Policy>
      <sp:MustSupportRefKeyIdentifier /> 
      <sp:MustSupportRefIssuerSerial /> 
      <sp:MustSupportRefThumbprint /> 
      <sp:MustSupportRefEncryptedKey /> 
      </wsp:Policy>
      </sp:Wss11>
    - <sp:Trust10>
    - <wsp:Policy>
      <sp:MustSupportIssuedTokens /> 
      <sp:RequireClientEntropy /> 
      <sp:RequireServerEntropy /> 
      </wsp:Policy>
      </sp:Trust10>
      </wsp:Policy>
      </sp:BootstrapPolicy>
      </wsp:Policy>
      </sp:SecureConversationToken>
      </wsp:Policy>
      </sp:ProtectionToken>
    - <sp:AlgorithmSuite>
    - <wsp:Policy>
      <sp:Basic256 /> 
      </wsp:Policy>
      </sp:AlgorithmSuite>
    - <sp:Layout>
    - <wsp:Policy>
      <sp:Strict /> 
      </wsp:Policy>
      </sp:Layout>
      <sp:IncludeTimestamp /> 
      <sp:EncryptSignature /> 
      <sp:OnlySignEntireHeadersAndBody /> 
      </wsp:Policy>
      </sp:SymmetricBinding>
    - <sp:Wss11 xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
    - <wsp:Policy>
      <sp:MustSupportRefKeyIdentifier /> 
      <sp:MustSupportRefIssuerSerial /> 
      <sp:MustSupportRefThumbprint /> 
      <sp:MustSupportRefEncryptedKey /> 
      </wsp:Policy>
      </sp:Wss11>
    - <sp:Trust10 xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
    - <wsp:Policy>
      <sp:MustSupportIssuedTokens /> 
      <sp:RequireClientEntropy /> 
      <sp:RequireServerEntropy /> 
      </wsp:Policy>
      </sp:Trust10>
      <wsaw:UsingAddressing /> 
      </wsp:All>
      </wsp:ExactlyOne>
      </wsp:Policy>
    - <wsp:Policy wsu:Id="WSHttpBinding_IMobileService_Test_Input_policy">
    - <wsp:ExactlyOne>
    - <wsp:All>
    - <sp:SignedParts xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
      <sp:Body />  
      </sp:SignedParts>
    - <sp:EncryptedParts xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
      <sp:Body /> 
      </sp:EncryptedParts>
      </wsp:All>
      </wsp:ExactlyOne>
      </wsp:Policy>
    - <wsp:Policy wsu:Id="WSHttpBinding_IMobileService_Test_output_policy">
    - <wsp:ExactlyOne>
    - <wsp:All>
    - <sp:SignedParts xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
      <sp:Body /> 
       </sp:SignedParts>
    - <sp:EncryptedParts xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
      <sp:Body /> 
      </sp:EncryptedParts>
      </wsp:All>
      </wsp:ExactlyOne>
      </wsp:Policy>
    - <wsp:Policy wsu:Id="WSHttpBinding_IMobileService_GetXY_Input_policy">
    - <wsp:ExactlyOne>
    - <wsp:All>
    - <sp:SignedParts xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
      <sp:Body /> 
      <sp:Header Name="To" Namespace="http://www.w3.org/2005/08/addressing" /> 
      <sp:Header Name="From" Namespace="http://www.w3.org/2005/08/addressing" /> 
      <sp:Header Name="FaultTo" Namespace="http://www.w3.org/2005/08/addressing" /> 
      <sp:Header Name="ReplyTo" Namespace="http://www.w3.org/2005/08/addressing" /> 
      <sp:Header Name="MessageID" Namespace="http://www.w3.org/2005/08/addressing" /> 
      <sp:Header Name="RelatesTo" Namespace="http://www.w3.org/2005/08/addressing" /> 
      <sp:Header Name="Action" Namespace="http://www.w3.org/2005/08/addressing" /> 
      </sp:SignedParts>
    - <sp:EncryptedParts xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
      <sp:Body /> 
      </sp:EncryptedParts>
      </wsp:All>
      </wsp:ExactlyOne>
      </wsp:Policy>
    - <wsp:Policy wsu:Id="WSHttpBinding_IMobileService_GetXY_output_policy">
    - <wsp:ExactlyOne>
    - <wsp:All>
    - <sp:SignedParts xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
      <sp:Body /> 
       </sp:SignedParts>
    - <sp:EncryptedParts xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
      <sp:Body /> 
      </sp:EncryptedParts>
      </wsp:All>
      </wsp:ExactlyOne>
      </wsp:Policy>
    - <wsdl:types>
    - <xsd:schema targetNamespace="http://tempuri.org/Imports">
      <xsd:import schemaLocation="http://172.18.70.32:82/MobileService/?xsd=xsd0" namespace="http://tempuri.org/" /> 
      <xsd:import schemaLocation="http://172.18.70.32:82/MobileService/?xsd=xsd1" namespace="http://schemas.microsoft.com/2003/10/Serialization/" /> 
      <xsd:import schemaLocation="http://172.18.70.32:82/MobileService/?xsd=xsd2" namespace="http://schemas.datacontract.org/2004/07/PGis110.Helper" /> 
      </xsd:schema>
      </wsdl:types>
    - <wsdl:message name="IMobileService_Test_InputMessage">
      <wsdl:part name="parameters" element="tns:Test" /> 
      </wsdl:message>
    - <wsdl:message name="IMobileService_Test_OutputMessage">
      <wsdl:part name="parameters" element="tns:TestResponse" /> 
      </wsdl:message>
    - <wsdl:message name="IMobileService_GetXY_InputMessage">
      <wsdl:part name="parameters" element="tns:GetXY" /> 
      </wsdl:message>
    - <wsdl:message name="IMobileService_GetXY_OutputMessage">
      <wsdl:part name="parameters" element="tns:GetXYResponse" /> 
      </wsdl:message>
    - <wsdl:portType name="IMobileService">
    - <wsdl:operation name="Test">
      <wsdl:input wsaw:Action="http://tempuri.org/IMobileService/Test" message="tns:IMobileService_Test_InputMessage" /> 
      <wsdl:output wsaw:Action="http://tempuri.org/IMobileService/TestResponse" message="tns:IMobileService_Test_OutputMessage" /> 
      </wsdl:operation>
    - <wsdl:operation name="GetXY">
      <wsdl:input wsaw:Action="http://tempuri.org/IMobileService/GetXY" message="tns:IMobileService_GetXY_InputMessage" /> 
      <wsdl:output wsaw:Action="http://tempuri.org/IMobileService/GetXYResponse" message="tns:IMobileService_GetXY_OutputMessage" /> 
      </wsdl:operation>
      </wsdl:portType>
    - <wsdl:binding name="WSHttpBinding_IMobileService" type="tns:IMobileService">
      <wsp:PolicyReference URI="#WSHttpBinding_IMobileService_policy" /> 
      <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" /> 
    - <wsdl:operation name="Test">
      <soap12:operation soapAction="http://tempuri.org/IMobileService/Test" style="document" /> 
    - <wsdl:input>
      <wsp:PolicyReference URI="#WSHttpBinding_IMobileService_Test_Input_policy" /> 
      <soap12:body use="literal" /> 
      </wsdl:input>
    - <wsdl:output>
      <wsp:PolicyReference URI="#WSHttpBinding_IMobileService_Test_output_policy" /> 
      <soap12:body use="literal" /> 
      </wsdl:output>
      </wsdl:operation>
    - <wsdl:operation name="GetXY">
      <soap12:operation soapAction="http://tempuri.org/IMobileService/GetXY" style="document" /> 
    - <wsdl:input>
      <wsp:PolicyReference URI="#WSHttpBinding_IMobileService_GetXY_Input_policy" /> 
      <soap12:body use="literal" /> 
      </wsdl:input>
    - <wsdl:output>
      <wsp:PolicyReference URI="#WSHttpBinding_IMobileService_GetXY_output_policy" /> 
      <soap12:body use="literal" /> 
      </wsdl:output>
      </wsdl:operation>
      </wsdl:binding>
    - <wsdl:service name="MobileService">
    - <wsdl:port name="WSHttpBinding_IMobileService" binding="tns:WSHttpBinding_IMobileService">
      <soap12:address location="http://172.18.70.32:82/MobileService/" /> 
    - <wsa10:EndpointReference>
      <wsa10:Address>http://172.18.70.32:82/MobileService/</wsa10:Address> 
    - <Identity xmlns="http://schemas.xmlsoap.org/ws/2006/02/addressingidentity">
      <Dns>localhost</Dns> 
      </Identity>
      </wsa10:EndpointReference>
      </wsdl:port>
      </wsdl:service>
      </wsdl:definitions>
      

  2.   

    本帖最后由 oyljerry 于 2013-08-28 23:25:39 编辑
      

  3.   

    恩 分析过了, 说是 bad requse 啊。 我不知道构建的evn 与body 哪里出错呢