📄 callwebservicesdlg.cpp
字号:
// CallWebservicesDlg.cpp : implementation file
//
#include "stdafx.h"
#include "CallWebservices.h"
#include "CallWebservicesDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CCallWebservicesDlg dialog
CCallWebservicesDlg::CCallWebservicesDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCallWebservicesDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCallWebservicesDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CCallWebservicesDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCallWebservicesDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCallWebservicesDlg, CDialog)
//{{AFX_MSG_MAP(CCallWebservicesDlg)
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCallWebservicesDlg message handlers
BOOL CCallWebservicesDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
CenterWindow(GetDesktopWindow()); // center to the hpc screen
// TODO: Add extra initialization here
HRESULT hr = CoInitializeEx(NULL,COINIT_MULTITHREADED);
if(!SUCCEEDED(hr))
{
MessageBox(_T("CoInitializeEx error!")); //初始化失败
return FALSE;
}
return TRUE; // return TRUE unless you set the focus to a control
}
void CCallWebservicesDlg::FromWeb()
{
MSXML2::IXMLHTTPRequest *iHTTPRequest;
HRESULT hr = CoCreateInstance(MSXML2::CLSID_XMLHTTPRequest,NULL,
CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER,
MSXML2::IID_IXMLHTTPRequest,
(LPVOID *)&iHTTPRequest);
if(hr == S_OK && iHTTPRequest != NULL) //如果创建失败则跳出
{
MessageBox(_T("CoCreateInstance ok!"));
}
else
{
MessageBox(_T("CoCreateInstance error!"));
return;
}
VARIANT vAsync; //这些工作是没办法,COM对象必须使用万能变量
VARIANT vUserID; //所以首先构造万能变量
VARIANT vPassword;
VARIANT vSend;
VariantInit(&vAsync);
vAsync.vt = VT_BOOL;
vAsync.boolVal = VARIANT_FALSE;
VariantInit(&vUserID);
vUserID.vt = VT_BSTR;
vUserID.bstrVal = NULL;
VariantInit(&vPassword);
vPassword.vt = VT_BSTR;
vPassword.bstrVal = NULL;
CString g_lpszSOAPReq = _T("");
g_lpszSOAPReq += _T("<?xml version='1.0' encoding='utf-8'?>");
g_lpszSOAPReq += _T("<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>");
g_lpszSOAPReq += _T("<soap:Body>");
g_lpszSOAPReq += _T("<login>");
g_lpszSOAPReq += _T("<in0>'0095'</in0>");
g_lpszSOAPReq += _T("<in1>'0095'</in1>");
g_lpszSOAPReq += _T("</login>");
g_lpszSOAPReq += _T("</soap:Body>");
g_lpszSOAPReq += _T("</soap:Envelope>");
// CString g_lpszSOAPReq = _T("");
// g_lpszSOAPReq += _T("<?xml version='1.0' encoding='utf-8'?>");
// g_lpszSOAPReq += _T("<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>");
// g_lpszSOAPReq += _T("<soap:Body>");
// g_lpszSOAPReq += _T("<getWeatherbyCityName xmlns='http://WebXml.com.cn/'>");
// g_lpszSOAPReq += _T("<theCityName>'58367'</theCityName>");
// g_lpszSOAPReq += _T("</getWeatherbyCityName>");
// g_lpszSOAPReq += _T("</soap:Body>");
// g_lpszSOAPReq += _T("</soap:Envelope>");
VariantInit(&vSend);
vSend.vt = VT_BSTR;
vSend.bstrVal = g_lpszSOAPReq.AllocSysString();
// hr = iHTTPRequest->open(_T("POST"),_T("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName"),vAsync,vUserID,vPassword);
hr = iHTTPRequest->open(_T("POST"),_T("http://10.81.64.22:8080/HIS-server/service/HISManageInf?wsdl/login"),vAsync,vUserID,vPassword);
long lpstatus;
iHTTPRequest->get_readyState(&lpstatus);
if(lpstatus == 1 && hr == S_OK)
{
iHTTPRequest->setRequestHeader(_T("Content-Type"),_T("text/xml"));
// iHTTPRequest->setRequestHeader(_T("SOAPAction"),_T("http://WebXml.com.cn/getWeatherbyCityName"));
iHTTPRequest->setRequestHeader(_T("SOAPAction"),_T("http://10.81.64.22:8080/HIS-server/service/HISManageInf"));
hr=iHTTPRequest->send(vSend);
}
else
{
MessageBox(_T("ready error!"));
return;
}
if(hr == S_OK)
{
MessageBox(_T("send ok!"));
}
else
{
MessageBox(_T("send error!"));
return;
}
// 在下面就可以接收数据了
BSTR bstrbody;
hr = iHTTPRequest->get_responseText(&bstrbody);
// VARIANT bstrbody;
// hr = iHTTPRequest->get_responseBody(&bstrbody);
if( SUCCEEDED(hr) ) //如果获得指针成功
{
long status;
iHTTPRequest->get_status(&status);
if(status != 200 && status != 207)
{
MessageBox(_T("recv error!"));
return;
}
else
{
MessageBox(bstrbody);
// MSXML2::IXMLDOMDocument *iXMLDoc;
// MSXML2::IXMLDOMElement *iXMLElm = NULL;
// MSXML2::IXMLDOMNodeList *iXMLlist = NULL;
// MSXML2::IXMLDOMNode *iXMLNode = NULL;
// MSXML2::IXMLDOMNode *iXMLItem = NULL;
// short tEmpty;
// BSTR bStr;
//
// hr = CoCreateInstance(MSXML2::CLSID_DOMDocument, NULL,
// CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER,
// MSXML2::IID_IXMLDOMDocument,(LPVOID *)&iXMLDoc);
//
// iXMLDoc->load(bstrbody,&tEmpty);
// MessageBox(_T("recv ok!"));
// if(tEmpty)
// {
// MessageBox(_T("load ok!"));
//
// iXMLDoc->getElementsByTagName(_T("ns1:loginResponse"),&iXMLlist); //查找某个结点
// iXMLlist->get_item(0,&iXMLNode);
// iXMLNode->get_childNodes(&iXMLlist);
//
// long lenth;
// iXMLlist->get_length(&lenth);
//
// for(int i=0;i<lenth;i++)
// {
// iXMLlist->get_item(i,&iXMLItem); //获取结点链中的某一个结点
// iXMLItem->get_text(&bStr); //获取结点的内容
// MessageBox(bStr);
// }
// }
// else
// {
// MessageBox(_T("load error!"));
// return;
// }
}
}
else
{
MessageBox(_T("respones error!"));
return;
}
//最后,释放XMLHTTP对象
iHTTPRequest->Release();
iHTTPRequest=NULL;
}
void CCallWebservicesDlg::OnButton1()
{
// TODO: Add your control notification handler code here
FromWeb();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -