【小记备忘】之Winnet调用webservice【2013.12.23】
欢迎加入我们的QQ群,无论你是否工作,学生,只要有c / vc / c++ 编程经验,就来吧!158427611
#include "stdafx.h" #include <windows.h> #include <wininet.h> //#import "Wininet.lib" static TCHAR* g_lpszSOAPRequest = _T("<soap:Envelope ") _T("xmlns:n='urn:xmethods-Temperature' ") _T("xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' ") _T("xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/' ") _T("xmlns:xs='http://www.w3.org/2001/XMLSchema' ") _T("xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'> ") _T("<soap:Body soap:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'> ") _T(" <n:getTemp> ") _T(" <zipcode xsi:type='xs:string'>98007</zipcode> ") _T(" </n:getTemp> ") _T("</soap:Body> ") _T("</soap:Envelope>"); #define CHUNK_SIZE 2048 int CallWebService(); int _tmain(int argc, _TCHAR* argv[]) { CallWebService(); return 0; } int CallWebService() { HINTERNET hSession = ::InternetOpen(_T("SoapTrans"), INTERNET_OPEN_TYPE_PRECONFIG, _T(""), INTERNET_INVALID_PORT_NUMBER, 0); if(hSession == NULL) { return 0; } HINTERNET hConnect = ::InternetConnect(hSession, _T("localhost"), INTERNET_INVALID_PORT_NUMBER, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0); if(hConnect == NULL) { ::InternetCloseHandle(hSession); return 0; } HINTERNET hHttpFile = ::HttpOpenRequest(hConnect, _T("POST"), _T("/WebService....."), HTTP_VERSION, NULL, 0, INTERNET_FLAG_DONT_CACHE, 0); if(hHttpFile == NULL) { ::InternetCloseHandle(hConnect); ::InternetCloseHandle(hSession); return 0; } BOOL bSendRequest = ::HttpSendRequest(hHttpFile, NULL, -1, g_lpszSOAPRequest, strlen(g_lpszSOAPRequest)); if(bSendRequest) { { char achQueryBuf[16]; DWORD dwFileSize; DWORD dwQueryBufLen = sizeof(achQueryBuf); BOOL bQuery = ::HttpQueryInfo(hHttpFile, HTTP_QUERY_CONTENT_LENGTH, achQueryBuf, &dwQueryBufLen, NULL); if(bQuery) { dwFileSize = (DWORD)atol(achQueryBuf); char* response = new char[dwFileSize]; DWORD dwLength; BOOL bGet = ::InternetReadFile(hHttpFile, response, dwFileSize, &dwLength); printf(response); } } ::InternetCloseHandle(hHttpFile); ::InternetCloseHandle(hConnect); ::InternetCloseHandle(hSession); } }
欢迎加入我们的QQ群,无论你是否工作,学生,只要有c / vc / c++ 编程经验,就来吧!158427611