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.

Create and Pulish web service, call in Asp.net(Demo).

Can debug local Now try to publish it to local IIS. http://msdn.microsoft.com/en-us/library/dd46...

GPS and Web Service using C++ ATL/WTL

  • 2011年10月31日 13:29
  • 74KB
  • 下载

VB call Web Service

This post will show you how to call web service in VB code.

jQuery Autocomplete using Web Service in ASP.Net

地址1:http://www.aspsnippets.com/Articles/Implement-jQuery-Autocomplete-using-Web-Service-in-ASP.Net.a...

How to invoke Java web service in ASP

  • 2009年11月19日 08:51
  • 1.96MB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Using WinInet to call a Web service
举报原因:
原因补充:

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