HTTP方式发送SOAP请求(VC)

 
 
2010-11-17 17:17

在VC中调用Web Servers接口常用有两种方法:

一种是利用Microsoft SOAP Toolkit,但这个必须要另外按装.

第二种可以利用VC的winnet.h中的相关类,自已组装SOAP请求.

第一种方法网上较多,可以百度"SOAP HTTP VC",但第一种方法对于理解web servers接口更好所以选用了第一种.

主要用到的类 : CInternetSession,CHttpConnection CHttpFile

代码:

请求Web servers接口需要有请求的URL(m_strServer)地址及接口所在的名空间(m_strNameSpace).

//step1:构造HTTP请求头部.

   strHeader.Format("Content-Type: text/xml; charset=\"UTF-8\"\r\n"
         "SOAPAction: \"%s%s\"\r\n",  m_strNameSpace, strMethod );

   这两个字段都是必须的,按HTTP协议头部的各段之间用换行隔开,POST 字段会在后面创建连接的时候加上去.

//step2:构造HTTP请求消息体:

这里面每个节点的形式都是XXX:YYY其中XXX是名字空间变量(跟C++的域运算符作用相当了),其值在后面的属性中给出,YYY是真正的节点名.例如:一开始的<SOAP:Envelope ..>SOAP是名字空间变量,其值在后面的属性给出: "xmlns:SOAP=\"http://schemas.xmlsoap.org/soap/envelope/\" ",按这个说法“SOAP”可以随便换,但Envelope不能因为SOAP协议规定必段以这个节点开始.好多名字空间都必须是固定的.

其中在接口方法名中引入了自已的名字空间: strMethodBegin.Format( "<RECENTCONTACT:%s xmlns:RECENTCONTACT=\"%s\" >", strMethodName , m_strNameSpace);
strMethodEnd.Format(   "</RECENTCONTACT:%s>", strMethodName );同理"RECENTcONTACT"可随便替换.

    strSoapXML= "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>"
          "<SOAP:Envelope SOAP:encodingStyle=\"http://schemas.xmlsoap.org/soap/envelope/\" "
                                "xmlns:SOAP=\"http://schemas.xmlsoap.org/soap/envelope/\" "
                                "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
                                "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">";


strSoapXML += "<SOAP:Body>";

CString strMethodBegin, strMethodEnd;
CString strParam;

strMethodBegin.Format( "<RECENTCONTACT:%s xmlns:RECENTCONTACT=\"%s\" >", strMethodName , m_strNameSpace);
strMethodEnd.Format(   "</RECENTCONTACT:%s>", strMethodName );
  

strSoapXML += strMethodBegin;
for ( int i=0; i<nCount; i++ )
{
   strParam.Format( "<RECENTCONTACT:in%d xsi:type=\"xsd:%s\">%s</RECENTCONTACT:in%d>",
                  i, pParam[i].strType, pParam[i].strValue, i );

      strSoapXML += strParam;
}
strSoapXML += strMethodEnd;

   strSoapXML += "</SOAP:Body>"
                 "</SOAP:Envelope>";

//step3:发送请求

CInternetSession m_internetSession;
CHttpConnection *m_pHttpConnect=NULL;
CHttpFile        *m_pHttpFile=NULL;

try
{
   m_pHttpConnect = m_internetSession.GetHttpConnection( m_strServer, m_uPort );
  
   m_pHttpFile= m_pHttpConnect->OpenRequest( CHttpConnection::HTTP_VERB_POST, m_strObj ); //这里会在头部加入POST字段.
  
   if( !m_pHttpFile->AddRequestHeaders( strHeaders , HTTP_ADDREQ_FLAG_COALESCE | HTTP_ADDREQ_FLAG_ADD_IF_NEW) )
   {
    DWORD dw;
    dw=GetLastError();
    dw=GetLastError();
   }
  
     m_pHttpFile->SendRequestEx( strBody.GetLength(), HSR_SYNC | HSR_INITIATE );
  
   m_pHttpFile->Write( strBody, strBody.GetLength() );
  
   m_pHttpFile->EndRequest( HSR_SYNC );
  
   //读取返回值
   DWORD dwHttpStatus;
   if (!m_pHttpFile->QueryInfoStatusCode(dwHttpStatus))
   {
    dwHttpStatus = 200;
   }
  
   DWORD dwResponseLength = m_pHttpFile->GetLength();
   while (0 != dwResponseLength )
   {
    char *szResponse = new char[dwResponseLength + 1];
    szResponse[dwResponseLength] = '\0';
    m_pHttpFile->Read(szResponse, dwResponseLength);
    strRt += szResponse;
    delete[] szResponse;
            dwResponseLength = m_pHttpFile->GetLength();
   }
  
  
}
catch( CInternetException )
{

.....
     return strRt;
}
catch (CException* e)
{
    
...

   return strRt;
}


m_pHttpFile->Close();
m_pHttpConnect->Close();
   m_internetSession.Close();

return strRt;

代码均从函数中获取,不能直接使用.

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值