Using WinInet to call a Web service

原创 2005年04月27日 14:53:00
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.

Cousera-Using python to access web data(第2~4周笔记)

第二周: 正则表达式 http://www.cnblogs.com/moonache/p/5110322.html 第三周: Networks and Sockets http://www....
  • u014518506
  • u014518506
  • 2016年10月31日 20:28
  • 2591

LoadRunner测试webservice接口-方法一web_service_call()

方法一:webservice协议的web_service_call()     使用LoadRunner测试webservice接口,最常用的方式就是使用webservice协议的web_ser...
  • FloraCHY
  • FloraCHY
  • 2017年05月24日 11:17
  • 937

VB call Web Service

This post will show you how to call web service in VB code.
  • Burgess_Liu
  • Burgess_Liu
  • 2011年03月22日 09:18
  • 1671

利用winInet实现文件上传到HTTP服务器

利用winInet实现文件上传到HTTP服务器 最近两个星期主要搞这个东东,到今天总算比较圆满的搞定了. 用http协议上传主要有两种形式: 第一是用http的put协议,第二是用http的post协...
  • cyforce
  • cyforce
  • 2011年02月19日 23:27
  • 5540

Call web service using HttpWebRequest and serialization

Desrible how  to call web service using httpwebreqeustshow how to create request data by serializati...
  • flyspirit
  • flyspirit
  • 2006年11月24日 07:53
  • 570

jQuery Call web services(SOAP) in SharePoint 2013

Step by Step Calling web services(SOAP) in SharePoint 2013 As we know sharepoint already provide so...
  • Tristan_Dong
  • Tristan_Dong
  • 2014年03月28日 13:32
  • 2038

Silverlight: serializing the contents of a web service call

Today Ive seen an interesting question on the Silverlight forum. The question was: how to change the...
  • cuijie
  • cuijie
  • 2008年01月27日 12:26
  • 346

Call JMS Web Service

The content type application/json of the response message does not match the content type of the bin...
  • sui84
  • sui84
  • 2014年04月29日 13:25
  • 370

call web service failed

SR 3-5609695891: call web service failed 信息 请求号 3-560...
  • lixiaohuiok111
  • lixiaohuiok111
  • 2012年04月24日 10:19
  • 5425

java调用WebService接口方法

package cn.cehb; import java.net.MalformedURLException;   import java.rmi.RemoteException;   impo...
  • quwei7515
  • quwei7515
  • 2014年01月07日 09:05
  • 11114
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Using WinInet to call a Web service
举报原因:
原因补充:

(最多只允许输入30个字)