代码示例>VC++示例

VC++调用短信接口发送示例

// SendDemo.cpp : Defines the entry point for the console application.
// qiruiyun vc++ demo
 
#include "stdafx.h"
#include <Windows.h>
#include "winhttp.h"
#pragma comment(lib,"winhttp.lib")
#include <string>
#include <tchar.h>
using namespace std;
 
string httpClient(wchar_t* host, int port, wchar_t* url);
 
int main(int argc, char *argv[])
{
    //un APIKey(接口账号)
    //pw APISecret(接口密钥)
    string ret = httpClient(L"api.qirui.com", 7891, L"/mt?dc=8&sm=【启瑞云】您的验证码是:548977&da=15100000000&un=2288**0010&pw=ae2600e9456f5b0b3ab8&tf=3&rf=1&rd=0");
    printf("%s\n", ret.c_str());
    return 0;
}
 
string httpClient(wchar_t* host, int port, wchar_t* url)
{
    string respStr;
    DWORD dwSize = 0;
    DWORD dwDownloaded = 0;
    LPSTR pszOutBuffer;
    BOOL bResults = FALSE;
    HINTERNET hSession = NULL, 
              hConnect = NULL, 
              hRequest = NULL;
 
    // Use WinHttpOpen to obtain a session handle.
    hSession = WinHttpOpen(L"A WinHTTP Example Program/1.1"
                           WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, 
                           WINHTTP_NO_PROXY_NAME, 
                           WINHTTP_NO_PROXY_BYPASS, 0);
 
    // Specify an HTTP server  INTERNET_DEFAULT_HTTP_PORT
    if (hSession)
        hConnect = WinHttpConnect(hSession, host, 
                                  port, 0);
 
    // Create an HTTP Request handle.
    if (hConnect)
        hRequest = WinHttpOpenRequest(hConnect, L"GET"
                                      url, 
                                      NULL, WINHTTP_NO_REFERER, 
                                      WINHTTP_DEFAULT_ACCEPT_TYPES, 
                                      0);
 
    // Send a Request.
    if (hRequest)
        bResults = WinHttpSendRequest(hRequest, 
                                      WINHTTP_NO_ADDITIONAL_HEADERS, 
                                      0, WINHTTP_NO_REQUEST_DATA, 0, 
                                      0, 0);
 
    // End the request.
    if (bResults)
        bResults = WinHttpReceiveResponse(hRequest, NULL);
 
    // Continue to verify data until there is nothing left.
    if (bResults)
    {
        do
        {
            // Verify available data.
            dwSize = 0;
            if (!WinHttpQueryDataAvailable(hRequest, &dwSize))
                printf("Error %u in WinHttpQueryDataAvailable.\n"
                       GetLastError());
             
            // Allocate space for the buffer.
            pszOutBuffer = new char[dwSize+1];
             
            // Read the Data.
            ZeroMemory(pszOutBuffer, dwSize+1);
 
            if (!WinHttpReadData(hRequest, (LPVOID)pszOutBuffer, 
                                 dwSize, &dwDownloaded))
                printf("Error %u in WinHttpReadData.\n", GetLastError());
            else
                //printf("%s\n", pszOutBuffer);
                respStr += _T(pszOutBuffer);
 
            // Free the memory allocated to the buffer.
            delete[] pszOutBuffer;
        while (dwSize > 0);
    
 
    // Report errors.
    if (!bResults)
        printf("Error %d has occurred.\n", GetLastError());
 
    // Close open handles.
    if (hRequest) WinHttpCloseHandle(hRequest);
    if (hConnect) WinHttpCloseHandle(hConnect);
    if (hSession) WinHttpCloseHandle(hSession);
 
    return respStr;
}

注意事项

√ 短信接口
    1)短信内容一定要带签名,签名放在短信内容的最前面;
    2)签名格式:【***】,签名内容为三个汉字以上(包括三个);
    3)短信内容不允许双签名,即短信内容里只有一个"【】";
    4)短信签名先申请报备后使用,控制台>接口短信>签名报备;