mfc call c# webservice method
#include "stdafx.h"
#include "HttpMethod.h"
#import "msxml.dll"
#include "msxml.h"
using namespace MSXML;
using namespace std;
void HttpA()
{
LPCWSTR agentName = _TEXT("CppBSHttp");
HINTERNET hInternetAgent = InternetOpen(agentName, LOCAL_INTERNET_ACCESS, NULL, 0, 0);
if(!hInternetAgent)
{
return;
}
HINTERNET hInternetConnection = InternetConnect(hInternetAgent, _TEXT("localhost"), 12259, NULL, NULL, INTERNET_SERVICE_HTTP, NULL, NULL);
if(!hInternetConnection)
{
return;
}
char* szAccept[] = {"*/*", NULL};
HINTERNET hInternetRequest = HttpOpenRequest(hInternetConnection, _TEXT("POST"), _TEXT("CBSLogin.ashx"),
NULL, NULL, (LPCWSTR*)szAccept, 0, 0);
LPCWSTR headerCon = _TEXT("Content-Type: application/x-www-form-urlencoded");
LPCWSTR formCon = _TEXT("userName=你好&userPsw=1");
LPSTR sendFormCon = NULL;
sendFormCon = (LPSTR)malloc(wcslen(formCon) * 6);
memset(sendFormCon, 0, wcslen(formCon) * 6);
WideCharToMultiByte(CP_UTF8, 0, formCon, -1, sendFormCon, wcslen(formCon) * 6, NULL, NULL );
//BOOL bRsl = HttpSendRequest(hInternetRequest, headerCon, wcslen(headerCon), (LPVOID)formCon, wcslen(formCon));
int lenth = strlen(sendFormCon);
BOOL bRsl = HttpSendRequest(hInternetRequest, headerCon, wcslen(headerCon), (LPVOID)sendFormCon , strlen(sendFormCon));
free(sendFormCon);
char rslBuffer[4096];
DWORD readSize = 0;
bRsl = InternetReadFile(hInternetRequest, rslBuffer, 4000, &readSize);
LPWSTR lp = NULL;
lp = (LPWSTR)malloc(readSize * 2);
memset(lp, 0, readSize * 2);
MultiByteToWideChar(CP_UTF8, 0, rslBuffer, readSize, lp, readSize * 2);
ExplainXml(lp);
free(lp);
InternetCloseHandle(hInternetRequest);
InternetCloseHandle(hInternetConnection);
InternetCloseHandle(hInternetAgent);
}
void ExplainXml(LPCTSTR xmlCon)
{
MSXML::IXMLDOMDocumentPtr pXmlDoc;
MSXML::IXMLDOMElementPtr pXmlRoot;
CoInitialize(NULL);
HRESULT hr = pXmlDoc.CreateInstance(__uuidof(MSXML::DOMDocument));
if(!SUCCEEDED(hr))
{
return;
}
pXmlDoc->loadXML((_bstr_t)xmlCon);
pXmlRoot = pXmlDoc->documentElement;
BSTR aSessionID = pXmlRoot->getAttribute((_bstr_t)"SessionID").bstrVal;
_bstr_t strTemp;
strTemp.Attach(aSessionID);
char* sessionID = (char *)strTemp;
BSTR eResult;
pXmlRoot->get_text(&eResult);
_bstr_t strTemp2;
strTemp2.Attach(eResult);
char* boolResult = (char *)strTemp2;
}