?? sendsms.cpp
字號:
/*
* Copyright (c) 2003,Rainsoft Studio
* All rights reserved.
*
* 文件名稱:SendSMS.cpp
* 文件標識:見配置管理計劃書
* 摘要:發送短信的出口主文件
*
* 當前版本:1.0
* 作者:王正平
* 完成日期:2003年07月03日
*/
#include <windows.h>
#include "SendSMS.h"
#include <wininet.h>
#include <TChar.h>
#include <stdio.h>
// 回車換行字符串
const char CLRF[2] = {0x0D, 0x0A};
// 預定義網關
typedef struct _GateWay
{
char Name[16];
char Comment[30];
char Count[2];
} GateWay, *PGateWay;
GateWay GateWayList[20];
/*****************************************************************
* 函數介紹: Dll文件的主入口函數
* 輸入參數:
* 輸出參數:
* 返 回 值:
*****************************************************************/
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
break;
}
return true;
}
/*****************************************************************
* 函數介紹: 執行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;
}
/*****************************************************************
* 函數介紹: 獲取短信網關
* 輸入參數: 無
* 輸出參數: 無
* 返 回 值: 網關列表: 每個網關以chr(10)+chr(13)分隔
開頭為16字節的網關代號
中間為30字節的網關說明
最后為2字節的短信允許漢字個數
*****************************************************************/
SENDSMS_API char* CALLAGREEMENT GetGatewayList()
{
int i;
// 網易
strcpy(GateWayList[0].Name, "163.com");
strcpy(GateWayList[0].Comment, "網易短信網關");
strcpy(GateWayList[0].Count, "50");
// 搜狐
strcpy(GateWayList[1].Name, "sohu.com");
strcpy(GateWayList[1].Comment, "搜狐短信網關");
strcpy(GateWayList[1].Count, "50");
for (i=0; i<2; i++)
{
strcat(MyGateWay, GateWayList[i].Name);
strcat(MyGateWay, GateWayList[i].Comment);
strcat(MyGateWay, GateWayList[i].Count);
strcat(MyGateWay, CLRF);
}
return MyGateWay;
}
/*****************************************************************
* 函數介紹: 發送短信函數
* 輸入參數: 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;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -