使用Wininet发送短信

来源:互联网 发布:算法初步高考题 编辑:程序博客网 时间:2024/06/04 18:11

转自:http://blog.csdn.net/wei801004/archive/2005/10/22/513670.aspx

 

如果能够使自己程序具有短信发送功能,那将会使您的程序蓬荜生辉,特别是通讯录等程序。通常在程序中发送短信有两种方式,一种是使用GSM Modem通过计算机串口编程发送短信,这种方式需要购置硬件设备,既不方便又不经济!另一种方式是通过网络发送,我们可以先在163.com等网站上注册一个用户,然后通过这些具有短信发送功能的网站发送短信。这种方式比较经济。下面本文讲述第二种的实现方式。

实际上,我们的程序只要具有象浏览器那样向HTTP服务器发送数据的功能,我们就可以模拟浏览器的登陆等操作,通过程序来发送短信。我们打算使用WinInet函数来实现与HTTP服务器的通信。

主要就两个函数,一个就是模拟浏览器的函数,另一个是发送短信的函数,其中使用了网易作为测试样例,您还可以自己添加其他短信网关。

包含的头文件如下:

#pragma comment (lib, "wininet")
#include <wininet.h>

/*****************************************************************
* 函数介绍: 执行HTTP的Post或Get方法
* 输入参数: TCHAR* hdrs - HTTP头
TCHAR* accept - Accept类型
TCHAR* Method - POST 或 GET
TCHAR* frmdata - 要提交的数据
TCHAR* ServerName - 服务器地址
TCHAR* FormAction - 数据提交到的网页名称
* 输出参数: 无
* 返 回 值: int - 返回操作状态(见SendSMS)
*****************************************************************/
int doHTTP(TCHAR* hdrs, TCHAR* accept, TCHAR* Method, TCHAR* frmdata, TCHAR* ServerName, TCHAR* FormAction)
{
// 创建Internet
HINTERNET hSession = InternetOpen("MyAgent", 
INTERNET_OPEN_TYPE_PRECONFIG, 
NULL, 
NULL,
0);
if (!hSession)
{
return 5;
}
// 连接服务器
HINTERNET hConnect = InternetConnect(hSession, 
ServerName, 
INTERNET_DEFAULT_HTTP_PORT, 
NULL, 
NULL, 
INTERNET_SERVICE_HTTP, 
0, 
1);
if (!hConnect)
{
return 2;
}
// 创建一个请求
HINTERNET hRequest = HttpOpenRequest(hConnect,
Method,
FormAction,
HTTP_VERSION,
NULL, 
(const char**)&accept,
0, 
1);
if (!hRequest)
{
return 2;
}
// 发送请求
BOOL bSendRequest = HttpSendRequest(hRequest,
hdrs,
strlen(hdrs),
frmdata,
strlen(frmdata));
if (!bSendRequest)
{
return 2;
}

////////////////////////调试用/////////////////
#ifdef _DEBUG
int bDoLoop = 1;
LPTSTR szReadBuffer;
DWORD lNumberOfBytesRead;
FILE* f1;
szReadBuffer = (LPTSTR) malloc(500);
ZeroMemory(szReadBuffer, 500);
if ((f1=fopen("c://test.htm", "w"))!=NULL)
{
while(bDoLoop)
{
bDoLoop = InternetReadFile(hRequest, szReadBuffer, 500, &lNumberOfBytesRead);
fseek(f1, 0L, SEEK_END);
fwrite(szReadBuffer, sizeof(szReadBuffer), lNumberOfBytesRead, f1); 
if (lNumberOfBytesRead<500)
bDoLoop = 0;
}
}
fclose(f1);
free(szReadBuffer);
#endif
//////////////////////////////////////////////////

// 清除句柄
if (hRequest)
InternetCloseHandle(hRequest);
if (hConnect)
InternetCloseHandle(hConnect);
if (hSession)
InternetCloseHandle(hSession);

return 0;
}

/*****************************************************************
* 函数介绍: 发送短信函数
* 输入参数: char* lpGateway - 发送网关名称
char* lpUserName - 发送者登陆账号
char* lpPassword - 发送者登陆密码
char* lpPhone - 接收者手机号码
char* lpContent - 发送内容
char* lpNickName - 发送者昵称
char* lpExtent - 扩展信息 
* 输出参数: 无
* 返 回 值: int 00 - 操作完成,结果未知
01 - 网关代号不存在
02 - 网络连接超时
03 - 用户中止操作
04 - 网关/账号/手机/短信内容空白或非法
05 - 出现其他错误
*****************************************************************/
SENDSMS_API int CALLAGREEMENT SendSMS(char* lpGateway,
char* lpUserName, 
char* lpPassword,
char* lpPhone, 
char* lpContent,
char* lpNickName,
char* lpExtent
)
{
int Result;
static TCHAR hdrs[] = _T("Content-Type: application/x-www-form-urlencoded");
static TCHAR accept[] = _T("Accept: */*");
static TCHAR frmdata[1024];

// 登陆姓名,密码等不允许为空
if ((strlen(lpGateway)<=0)||(strlen(lpUserName)<=0)||
(strlen(lpPassword)<=0)||(strlen(lpPhone)<=0)||(strlen(lpContent)<=0))
return 4;

// 选择网易网关发送
if (strcmp(lpGateway, "163.com")==0)
{
// 登录短信发送系统
sprintf(frmdata, "username=%s&password=%s", lpUserName, lpPassword);
Result = doHTTP(hdrs, accept, "POST", frmdata, "reg4.163.com", "/in.jsp");

// 发送短信
if (strlen(lpNickName)>0)
sprintf(frmdata, "send=1&phone=%s&message=%s--%s", lpPhone, lpContent, lpNickName);
else
sprintf(frmdata, "send=1&phone=%s&message=%s", lpPhone, lpContent);
Result = doHTTP(hdrs, accept, "POST", frmdata, "sms.163.com", "/service/sendmsg_pop.php");

// 退出短信发送系统
sprintf(frmdata, "username=%s", lpUserName);
Result = doHTTP(hdrs, accept, "GET", frmdata, "reg4.163.com", "/Logout.jsp");

return Result;
}


// 选择搜狐网关发送
if (strcmp(lpGateway, "sohu.com")==0)
{
Result = 1;
return Result;
}

// 网关代号不存在
return 1;