-
1.使用VS.NET2003 新建一个“Visual C++项目”,选择“Win32 控制台项目”,您也可建立“MFC应用程序”或“Win32 项目”,随个人喜好吧!
2.“添加 Web 引用”,输入URL地址,例如:http://www.xxx.com/WebService/Service1.asmx?wsdl,这里我们需要的是WSDL文件的申明,需要注意的是,在文件中如果Request和Response的namespace不相同时,VC++引用时会失败,大家可以观察一下。
3.下来进行编码操作,需要调用头文件comutil.h,事例代码(敏感数据被隐去):
// MYTEST.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "ZXT.h"
#include "CBase64.h"
#include "comutil.h"int _tmain(int argc, _TCHAR* argv[])
{
CoInitialize(NULL);BSTR p_szUserLoginID = _com_util::ConvertStringToBSTR("xxxxxxxxxxxx");
BSTR p_szPasswd = _com_util::ConvertStringToBSTR("*********");
CString cstr_szContent = "VC ++的Web Service>发送短信测试";
BSTR p_szSendTime = _com_util::ConvertStringToBSTR("");
BSTR p_szTargTel = _com_util::ConvertStringToBSTR("xxxxxxxxxxxx");
int p_iDispTel = 1;
int GZMCC_SendMsgServiceResult;
int ret;
BSTR desc;
BSTR zxtid;
BSTR feecode;
BSTR feetype;
BSTR feeltel;
BSTR content;
BSTR sendtime;
BSTR targtel;
ZxtSendMsgService::CZxtSendMsgService *service1 = new ZxtSendMsgService::CZxtSendMsgService();
CBase64* base64 = new CBase64();
cstr_szContent = base64->Encode((LPCTSTR)cstr_szContent,cstr_szContent.GetLength());
BSTR p_szContent = _com_util::ConvertStringToBSTR(cstr_szContent);/*//Base64编解码的操作
CString retS = base64->Encode((LPCTSTR)p_szContent,p_szContent.GetLength());
printf("%s/n",retS);
CString szOutput;
base64->Decode((LPCTSTR)retS,szOutput.GetBuffer(retS.GetLength()+1));
printf("%s/n",szOutput);
delete(base64);
*/HRESULT hr = service1->GZMCC_SendMsgService(p_szUserLoginID,
p_szPasswd,
p_szContent,
p_szSendTime,
p_szTargTel,
p_iDispTel,
&GZMCC_SendMsgServiceResult,
&ret,
&desc,
&zxtid,
&feecode,
&feetype,
&feeltel,
&content,
&sendtime,
&targtel);if(FAILED(hr))
{
printf("Error!");
}
else
{
printf("[ret]%d/n",ret);
printf("[desc]%s/n",_com_util::ConvertBSTRToString(desc));
}
service1->Release();
delete(service1);
delete(base64);
CoUninitialize();int* a;
scanf("%d",&a);
return 0;
}运行结果:
0
发送成功
4. 对于包含中文信息的短信内容,我采用了Base64的编码,具体代码见:
//CBase64.h: Base64编解码类的头文件
#pragma once
class CBase64
{
public:
CBase64(void);
~CBase64(void);
CString m_sBase64Alphabet;
private:
int m_nMask[9];
LPCTSTR m_szInput;
int m_nInputSize;
int m_nBitsRemaining;
UINT m_lBitStorage;public:
UINT read_bits(int nNumBits, int * pBitsRead, int& lp);
void write_bits(UINT nBits,int nNumBits,LPTSTR szOutput,int& i);
CString Encode(LPCTSTR szEncoding, int nSize);
int Decode(LPCTSTR szDecoding, LPTSTR szOutput);
};//CBase64.cpp: Base64源文件
#include "StdAfx.h"
#include "./cbase64.h"#if !defined(ASSERT)
# if defined(_DEBUG)
# include <assert.h>
# define ASSERT(x) assert(x)
# else
# define ASSERT(x) ((void)0)
# endif
#endifCBase64::CBase64(void)
{
m_sBase64Alphabet = _T( "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" );
m_nMask[0] = 0;
m_nMask[1] = 1;
m_nMask[2] = 3;
m_nMask[3] = 7;
m_nMask[4] = 15;
m_nMask[5] = 31;
m_nMask[6] = 63;
m_nMask[7] = 127;
m_nMask[8] = 255;}
CBase64::~CBase64(void)
{
}UINT CBase64::read_bits(int nNumBits, int * pBitsRead, int& lp)
{
ULONG lScratch;
while( ( m_nBitsRemaining < nNumBits ) && ( lp < m_nInputSize ) )
{
int c = m_szInput[ lp++ ];
m_lBitStorage <<= 8;
m_lBitStorage |= (c & 0xff);
m_nBitsRemaining += 8;
}
if( m_nBitsRemaining < nNumBits )
{
lScratch = m_lBitStorage << ( nNumBits - m_nBitsRemaining );
*pBitsRead = m_nBitsRemaining;
m_nBitsRemaining = 0;
}
else
{
lScratch = m_lBitStorage >> ( m_nBitsRemaining - nNumBits );
*pBitsRead = nNumBits;
m_nBitsRemaining -= nNumBits;
}
return (UINT)lScratch & m_nMask[nNumBits];
}void CBase64::write_bits(UINT nBits,int nNumBits,LPTSTR szOutput,int& i)
{
UINT nScratch;
m_lBitStorage = (m_lBitStorage << nNumBits) | nBits;
m_nBitsRemaining += nNumBits;
while( m_nBitsRemaining > 7 )
{
nScratch = m_lBitStorage >> (m_nBitsRemaining - 8);
szOutput[ i++ ] = nScratch & 0xFF;
m_nBitsRemaining -= 8;
}
}CString CBase64::Encode(LPCTSTR szEncoding, int nSize)
{
CString sOutput = _T("");
int nNumBits = 6;
UINT nDigit;
int lp = 0;ASSERT( szEncoding != NULL );
if( szEncoding == NULL )
return sOutput;
m_szInput = szEncoding;
m_nInputSize = nSize;m_nBitsRemaining = 0;
nDigit = read_bits( nNumBits, &nNumBits, lp);
while( nNumBits > 0 )
{
sOutput += m_sBase64Alphabet[(int)nDigit];
nDigit = read_bits(nNumBits, &nNumBits,lp);
}while( sOutput.GetLength() % 4 != 0 )
{
sOutput += '=';
}
return sOutput;
}int CBase64::Decode(LPCTSTR szDecoding, LPTSTR szOutput)
{
CString sInput;
int c, lp =0;
int nDigit;
int nDecode[ 256 ];ASSERT( szDecoding != NULL );
ASSERT( szOutput != NULL );
if( szOutput == NULL )
return 0;
if( szDecoding == NULL )
return 0;
sInput = szDecoding;
if( sInput.GetLength() == 0 )
return 0;for( int i = 0; i < 256; i++ )
nDecode[i] = -2;for( i=0; i < 64; i++ )
{
nDecode[ m_sBase64Alphabet[ i ] ] = i;
nDecode[ m_sBase64Alphabet[ i ] | 0x80 ] = i; // Ignore 8th bit
nDecode[ '=' ] = -1;
nDecode[ '=' | 0x80 ] = -1; // Ignore MIME padding char
}memset(szOutput, 0, sInput.GetLength() + 1);
for( lp = 0, i = 0; lp < sInput.GetLength(); lp++ )
{
c = sInput[ lp ];
nDigit = nDecode[ c & 0x7F ];
if( nDigit < -1 )
{
return 0;
}
else if( nDigit >= 0 )
write_bits( nDigit & 0x3F, 6, szOutput, i );
}
return i;
}就先讲到这里,关于AXIS的SOAP HTTP调用方法类似。
VC++调用Web Service的方法及注意事项
最新推荐文章于 2017-06-15 09:03:16 发布