目前共有2篇帖子。 內容轉換:不轉換▼
 
點擊 回復
501 1
【程序】C++使用WinHttp发送HTTP请求
一派護法 十九級
1樓 發表于:2016-2-18 11:11
本例以访问https://zh.arslanbar.net/API/为例。
【C++代码】
// HTTP.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#include <Winhttp.h>

#pragma comment(lib, "Winhttp.lib")

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    HINTERNET hSession = WinHttpOpen(L"A WinHTTP Example Program/1.0", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, NULL);
    if (hSession)
    {
        HINTERNET hConnect = WinHttpConnect(hSession, L"zh.arslanbar.net", INTERNET_DEFAULT_HTTP_PORT, NULL);
        HINTERNET hRequest = WinHttpOpenRequest(hConnect, NULL, L"API", NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_REFRESH);
        BOOL bResult = WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, NULL, WINHTTP_NO_REQUEST_DATA, NULL, NULL, NULL);
        if (!bResult)
            cout << "Error " << GetLastError() << " has occurred." << endl;
        WinHttpReceiveResponse(hRequest, NULL);
       
        DWORD dwSize;
        bResult = WinHttpQueryDataAvailable(hRequest, &dwSize);
        if (bResult)
        {
            DWORD dwDownloaded = 0;
            char *data = new char[dwSize + 1];
            WinHttpReadData(hRequest, data, dwSize, &dwDownloaded);
            data[dwSize] = '\0';
            cout << "dwSize = " << dwSize << endl;
            cout << data << endl;
            delete[] data;
        }
        else
        {
            DWORD dwError = GetLastError();
            cout << "Error " << dwError << " in WinHttpQueryDataAvailable." << endl;
            if (dwError == ERROR_WINHTTP_INCORRECT_HANDLE_STATE)
                cout << "The requested operation cannot complete because the handle supplied is not in the correct state." << endl;
        }

        WinHttpCloseHandle(hRequest);
        WinHttpCloseHandle(hConnect);
    }
    else
        cout << "Error " << GetLastError() << " in WinHttpOpen." << endl;

    WinHttpCloseHandle(hSession);
    system("pause");
    return 0;
}

【运行结果】
dwSize = 12
No Operation
一派護法 十九級
2樓 發表于:2016-2-18 12:22
【读取较长网页的方法】
// HTTP.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <Windows.h>
#include <Winhttp.h>

#pragma comment(lib, "Winhttp.lib")

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    HINTERNET hSession = WinHttpOpen(L"A WinHTTP Example Program/1.0", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, NULL);
    if (hSession)
    {
        HINTERNET hConnect = WinHttpConnect(hSession, L"zh.arslanbar.net", INTERNET_DEFAULT_HTTP_PORT, NULL);
        HINTERNET hRequest = WinHttpOpenRequest(hConnect, NULL, L"API?query=posts", NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_REFRESH);
        WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, NULL, WINHTTP_NO_REQUEST_DATA, NULL, NULL, NULL);

        BOOL bResult = WinHttpReceiveResponse(hRequest, NULL);
        if (bResult)
        {
            DWORD dwRead = 0;
            DWORD dwSize;
            stringstream data;
            while (WinHttpQueryDataAvailable(hRequest, &dwSize), dwSize > 0)
            {
                char *buffer = new char[dwSize];
                WinHttpReadData(hRequest, buffer, dwSize, &dwRead);
                data.write(buffer, dwSize);
                delete[] buffer;
            }

            // 写入文件
            FILE *fp;
            fopen_s(&fp, "test2.txt", "w");
            fwrite(data.str().c_str(), data.str().length(), 1, fp);
            fclose(fp);
        }

        WinHttpCloseHandle(hRequest);
        WinHttpCloseHandle(hConnect);
    }
    else
        cout << "Error " << GetLastError() << " in WinHttpOpen." << endl;

    WinHttpCloseHandle(hSession);
    system("pause");
    return 0;
}

回復帖子

內容:
用戶名: 您目前是匿名發表
驗證碼:
(快捷鍵:Ctrl+Enter)
 

本帖信息

點擊數:501 回複數:1
評論數: ?
作者:巨大八爪鱼
最後回復:巨大八爪鱼
最後回復時間:2016-2-18 12:22
 
©2010-2024 Arslanbar Ver2.0
除非另有聲明,本站採用創用CC姓名標示-相同方式分享 3.0 Unported許可協議進行許可。