Using WinInet to call a Web service
Using WinInet to call a Web service
I am working on a MFC/C++ application, and need to call a Web service. I would like to avoid adding any dependency or use any other API (MSXML or SOAP Toolkit), and continue with my application's minimum requirements (which includes Internet Explorer 5.5 or above), and still write the Web service client code. In other words, can you please show me an example of using WinInet to call a Web service?
Answer:
Click here to download a sample console application that uses MFC WinInet classes to call a Web service.

MFC provides wrapper classes around WinInet API. These classes simplify the task of writing HTTP/FTP client applications. Following are the eight steps required to send a HTTP request using MFC WinInet classes:
  1. Create an instance of CInternetSession class. This begins an HTTP session.

  2. Call CInternetSession::GetHttpConnection to get an instance of CHttpConnection. Pass the server and HTTP port to this method, and it establishes a connection to an HTTP server.

  3. Open an HTTP request using CHttpConnection::OpenRequest. Pass the rest of the URL (except server name), the HTTP method (GET/POST/...) to this method, and it returns a CHttpFile object.

  4. Optionally, call CHttpFile::AddRequestHeaders to supply any request headers.

  5. Call CHttpFile::SendRequest to actually send the request and get the response back.

  6. Use CHttpFile::QueryInfoStatusCode to find out if the HTTP request succeeded.

  7. On success, use pHttpFile->Read to read the response bytes.

  8. Finally, call CHttpFile::Close and CHttpConnection::Close.
Here is a Web service client code that uses MFC WinInet classes to call Weather - Temperature Web service (written using Apache SOAP) on XMethods.com. This Web service, given a zip code (US only), returns the current temperature.
...
...
#include <afxinet.h>
...
...

static const TCHAR* g_lpszSOAPRequest =    
_T("<soap:Envelope "
    "xmlns:n='urn:xmethods-Temperature' "
    "xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' "
    "xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/' "
    "xmlns:xs='http://www.w3.org/2001/XMLSchema' "
    "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'> "
    "<soap:Body soap:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'> "
    "  <n:getTemp> "
    "     <zipcode xsi:type='xs:string'>98007</zipcode> "
    "  </n:getTemp> "
   "</soap:Body> "
"</soap:Envelope>");

#define CHUNK_SIZE      2048
   
void CallWebService()
{
   try
   {
      
      CInternetSession httpSession(_T("Sample Web Service Client"), 
                           1,
                           INTERNET_OPEN_TYPE_PRECONFIG,
                           NULL,
                           NULL,
                           INTERNET_FLAG_DONT_CACHE);
      
      
      CHttpConnection* pHttpConnection = 
                              httpSession.GetHttpConnection(_T("services.xmethods.net"), 
                                 INTERNET_FLAG_NO_AUTO_REDIRECT,
                                 80, NULL, NULL);

      
      CHttpFile* pHttpFile             = 
                              pHttpConnection->OpenRequest
                              (_T("POST"), 
                                 _T("soap/servlet/rpcrouter"), 
                                 NULL, 1, NULL, NULL, 
                                 INTERNET_FLAG_KEEP_CONNECTION |
                                 INTERNET_FLAG_EXISTING_CONNECT |
                                 INTERNET_FLAG_DONT_CACHE |
                                 INTERNET_FLAG_RELOAD);

      
      CString strSOAPReq(g_lpszSOAPRequest);
      DWORD dwRewLen = strSOAPReq.GetLength();
      CString strHeaders;
      strHeaders.Format(_T("Content-Type: text/xml; charset=utf-8/nContent-Length:%d"), 
         dwRewLen);
      pHttpFile->AddRequestHeaders(strHeaders);

      
      
      pHttpFile->SendRequest(NULL, 0, (LPVOID)(LPCTSTR)strSOAPReq, dwRewLen);

      
      DWORD dwStatucCode = HTTP_STATUS_OK;

      pHttpFile->QueryInfoStatusCode(dwStatucCode);

      if(dwStatucCode == HTTP_STATUS_OK)
      {
         CString strResponse;
         TCHAR szBuf[CHUNK_SIZE] = {0};
         UINT nBytesRead;

         
         do
         {
            nBytesRead = pHttpFile->Read((void*) szBuf, CHUNK_SIZE);
            strResponse += szBuf;
            if(nBytesRead < CHUNK_SIZE)
               break;
         }while(nBytesRead == CHUNK_SIZE);

         AfxMessageBox(strResponse);
         
      }
      else
      {
         
      }

      
      if(pHttpFile)
      {
         pHttpFile->Close();
         delete pHttpFile;
         pHttpFile = NULL;
      }

      if(pHttpConnection)
      {
         pHttpConnection->Close();
         delete pHttpConnection;
         pHttpConnection = NULL;
      }

   }
   catch(CInternetException* exp)
   {
      TCHAR lpszErrorMsg[MAX_PATH+2];
      exp->GetErrorMessage(lpszErrorMsg, MAX_PATH);
      AfxMessageBox(lpszErrorMsg);
   }
}
...
...
Click here to download the above sample console application.

阅读更多
个人分类: others
想对作者说点什么? 我来说一句

web service example

2013年08月07日 183KB 下载

flex call web Service

2009年04月16日 3KB 下载

python调用WEBSERVICE接口

2017年07月12日 28KB 下载

mgcp呼叫流程文档

2010年08月07日 166KB 下载

没有更多推荐了,返回首页

不良信息举报

Using WinInet to call a Web service

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭