作者共发了2篇帖子。 内容转换:不转换▼
 
点击 回复
505 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)
 

本帖信息

点击数:505 回复数:1
评论数: ?
作者:巨大八爪鱼
最后回复:巨大八爪鱼
最后回复时间:2016-2-18 12:22
 
©2010-2024 Arslanbar Ver2.0
除非另有声明,本站采用知识共享署名-相同方式共享 3.0 Unported许可协议进行许可。