winnet winhttp

winnet winhttp//HttpPost.cppwrittenbyl_zhaohui@163.com//2007/11/30#include<windows.h>#include<stdio.h>#include<stdlib.h>#define_ATL_CSTRING_EXPLICIT_CONSTRUCTORS#includ…

大家好,又见面了,我是你们的朋友全栈君。

 // HttpPost.cpp written by l_zhaohui@163.com
// 2007/11/30

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>

#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS
#include <atlbase.h>
#include <atlstr.h>

#define USE_WINHTTP    //Comment this line to user wininet.
#ifdef USE_WINHTTP
#include <winhttp.h>
#pragma comment(lib, "winhttp.lib")
#else
#include <wininet.h>
#pragma comment(lib, "wininet.lib")
#endif
#define BUF_SIZE    (1024)

// CrackedUrl
class CrackedUrl
{
    int m_scheme;
    CStringW m_host;
    int m_port;
    CStringW m_path;
public:
    CrackedUrl(LPCWSTR url)
    {
        URL_COMPONENTS uc = { 0};
        uc.dwStructSize = sizeof(uc);

        const DWORD BUF_LEN = 256;

        WCHAR host[BUF_LEN];
        uc.lpszHostName = host;
        uc.dwHostNameLength = BUF_LEN;

        WCHAR path[BUF_LEN];
        uc.lpszUrlPath = path;
        uc.dwUrlPathLength = BUF_LEN;

        WCHAR extra[BUF_LEN];
        uc.lpszExtraInfo = extra;
        uc.dwExtraInfoLength = BUF_LEN;

#ifdef USE_WINHTTP
        if (!WinHttpCrackUrl(url, 0, ICU_ESCAPE, &uc))
        {
            printf("Error:WinHttpCrackUrl failed!/n");
        }

#else
        if (!InternetCrackUrl(url, 0, ICU_ESCAPE, &uc))
        {
            printf("Error:InternetCrackUrl failed!/n");
        }
#endif
        m_scheme = uc.nScheme;
        m_host = host;
        m_port = uc.nPort;
        m_path = path;
    }

    int GetScheme() const
    {
        return m_scheme;
    }

    LPCWSTR GetHostName() const
    {
        return m_host;
    }

    int GetPort() const
    {
        return m_port;
    }

    LPCWSTR GetPath() const
    {
        return m_path;
    }

    static CStringA UrlEncode(const char *p)
    {
        if (p == 0)
        {
            return CStringA();
        }

        CStringA buf;

        for (;;)
        {
            int ch = (BYTE) (*(p++));
            if (ch == '/0')
            {
                break;
            }

            if (isalnum(ch) || ch == '_' || ch == '-' || ch == '.')
            {
                buf += (char)ch;
            }
            else if (ch == ' ')
            {
                buf += '+';
            }
            else
            {
                char c[16];
                wsprintfA(c, "%%%02X", ch);
                buf += c;
            }
        }

        return buf;
    }
};

// CrackedUrl










HINTERNET OpenSession(LPCWSTR userAgent = 0)
{
#ifdef USE_WINHTTP
    return WinHttpOpen(userAgent, NULL, NULL, NULL, NULL);;
#else
    return InternetOpen(userAgent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
#endif
}

HINTERNET Connect(HINTERNET hSession, LPCWSTR serverAddr, int portNo)
{
#ifdef USE_WINHTTP
    return WinHttpConnect(hSession, serverAddr, (INTERNET_PORT) portNo, 0);
#else
    return InternetConnect(hSession, serverAddr, portNo, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
#endif
}

HINTERNET OpenRequest(HINTERNET hConnect, LPCWSTR verb, LPCWSTR objectName, int scheme)
{
    DWORD flags = 0;
#ifdef USE_WINHTTP
    if (scheme == INTERNET_SCHEME_HTTPS)
    {
        flags |= WINHTTP_FLAG_SECURE;
    }

    return WinHttpOpenRequest(hConnect, verb, objectName, NULL, NULL, NULL, flags);

#else
    if (scheme == INTERNET_SCHEME_HTTPS)
    {
        flags |= INTERNET_FLAG_SECURE;
    }

    return HttpOpenRequest(hConnect, verb, objectName, NULL, NULL, NULL, flags, 0);
#endif
}

BOOL AddRequestHeaders(HINTERNET hRequest, LPCWSTR header)
{
    SIZE_T len = lstrlenW(header);
#ifdef USE_WINHTTP
    return WinHttpAddRequestHeaders(hRequest, header, DWORD(len), WINHTTP_ADDREQ_FLAG_ADD);
#else
    return HttpAddRequestHeaders(hRequest, header, DWORD(len), HTTP_ADDREQ_FLAG_ADD);
#endif
}

BOOL SendRequest(HINTERNET hRequest, const void *body, DWORD size)
{
#ifdef USE_WINHTTP
    return WinHttpSendRequest(hRequest, 0, 0,const_cast<void *>(body), size, size, 0);
#else
    return HttpSendRequest(hRequest, 0, 0, const_cast<void *>(body), size);
#endif
}
BOOL EndRequest(HINTERNET hRequest)
{
#ifdef USE_WINHTTP
    return WinHttpReceiveResponse(hRequest, 0);
#else
    // if you use HttpSendRequestEx to send request then use HttpEndRequest in here!
    return TRUE;
#endif
}

BOOL QueryInfo(HINTERNET hRequest, int queryId, char *szBuf, DWORD *pdwSize)
{
#ifdef USE_WINHTTP
    return WinHttpQueryHeaders(hRequest, (DWORD) queryId, 0, szBuf, pdwSize, 0);
#else
    return HttpQueryInfo(hRequest, queryId, szBuf, pdwSize, 0);
#endif
}

BOOL ReadData(HINTERNET hRequest, void *buffer, DWORD length, DWORD *cbRead)
{
#ifdef USE_WINHTTP
    return WinHttpReadData(hRequest, buffer, length, cbRead);
#else
    return InternetReadFile(hRequest, buffer, length, cbRead);
#endif
}

void CloseInternetHandle(HINTERNET hInternet)
{
    if (hInternet)
    {
#ifdef USE_WINHTTP
        WinHttpCloseHandle(hInternet);
#else
        InternetCloseHandle(hInternet);
#endif
    }
}

int _tmain(int argc, _TCHAR *argv[])
{
    HINTERNET hSession = 0;
    HINTERNET hConnect = 0;
    HINTERNET hRequest = 0;
    CStringW strHeader(L"Content-type: application/x-www-form-urlencoded/r/n");

    // Test data
    CrackedUrl crackedUrl(L"http://www.baidu.com");
    CStringA strPostData("a=1");

    // Open session.
    hSession = OpenSession(L"HttpPost");
    if (hSession == NULL)
    {
        printf("Error:Open session!/n");
        return -1;
    }

    // Connect.
    hConnect = Connect(hSession, crackedUrl.GetHostName(), crackedUrl.GetPort());
       // hConnect = Connect(hSession, L"192.168.0.8",80);
    if (hConnect == NULL)
    {
        printf("Error:Connect failed!/n");
        return -1;
    }

    // Open request.
    //hRequest = OpenRequest(hConnect, L"POST", L"login.html", crackedUrl.GetScheme());
    hRequest = OpenRequest(hConnect, L"POST", crackedUrl.GetPath(), crackedUrl.GetScheme());
    if (hRequest == NULL)
    {
        printf("Error:OpenRequest failed!/n");
        return -1;
    }

    // Add request header.
    if (!AddRequestHeaders(hRequest, strHeader))
    {
        printf("Error:AddRequestHeaders failed!/n");
        return -1;
    }

    // Send post data.
    if (!SendRequest(hRequest, (const char *)strPostData, strPostData.GetLength()))
    {
        printf("Error:SendRequest failed!/n");
        return -1;
    }

    // End request
    if (!EndRequest(hRequest))
    {
        printf("Error:EndRequest failed!/n");
        return -1;
    }
    char szBuf[BUF_SIZE];
    DWORD dwSize = 0;
    szBuf[0] = 0;

    // Query header info.
#ifdef USE_WINHTTP
    int contextLengthId = WINHTTP_QUERY_CONTENT_LENGTH;
    int statusCodeId = WINHTTP_QUERY_STATUS_CODE;
    int statusTextId = WINHTTP_QUERY_STATUS_TEXT;
#else
    int contextLengthId = HTTP_QUERY_CONTENT_LENGTH;
    int statusCodeId = HTTP_QUERY_STATUS_CODE;
    int statusTextId = HTTP_QUERY_STATUS_TEXT;
#endif
    dwSize = BUF_SIZE;
    if (QueryInfo(hRequest, contextLengthId, szBuf, &dwSize))
    {
        szBuf[dwSize] = 0;
        printf("Content length:[%s]/n", szBuf);
    }
    dwSize = BUF_SIZE;
    if (QueryInfo(hRequest, statusCodeId, szBuf, &dwSize))
    {
        szBuf[dwSize] = 0;
        printf("Status code:[%s]/n", szBuf);
    }

    dwSize = BUF_SIZE;
    if (QueryInfo(hRequest, statusTextId, szBuf, &dwSize))
    {
        szBuf[dwSize] = 0;
        printf("Status text:[%s]/n", szBuf);
    }

    // read data.
    for (;;)
    {
        dwSize = BUF_SIZE;
        if (ReadData(hRequest, szBuf, dwSize, &dwSize) == FALSE)
        {
            break;
        }

        if (dwSize <= 0)
        {
            break;
        }

        szBuf[dwSize] = 0;
        printf("%s/n", szBuf);    //Output value = value1 + value2
    }

    CloseInternetHandle(hRequest);
    CloseInternetHandle(hConnect);
    CloseInternetHandle(hSession);

         system("pause");

    return 0;
}

转载于:https://www.cnblogs.com/ytjjyy/archive/2012/05/18/2507994.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/157566.html原文链接:https://javaforall.net

(0)
上一篇 2022年7月11日 下午9:00
下一篇 2022年7月11日 下午9:00


相关推荐

  • GLM-ASR-Nano-2512部署教程:CPU模式下16GB内存运行中文基础识别服务

    GLM-ASR-Nano-2512部署教程:CPU模式下16GB内存运行中文基础识别服务

    2026年3月12日
    2
  • 遥感生态指数(RSEI)——水体掩膜及主成分分析

    遥感生态指数(RSEI)——水体掩膜及主成分分析1 水体掩膜 利用水体指数来增强水体信息 弱化其他地物的信息 便于提取研究区的水体范围 NDWI Green NIR Green NIR MNDWI Green SWIR1 Green SWIR1 INDVI Red NIR Red NIR 以上三个水体指数都能在一定程度上突出水体 推荐使用 MNDWI 指数进行水体范围的提取 在 Bandmath 中输入水体指数法的计算公式 float b2 b5 b2 b5 其中 b2 b

    2026年3月17日
    1
  • 通俗易懂的LHS和RHS

    通俗易懂的LHS和RHS在一段代码执行之前 会经过编译阶段 在对程序的处理过程中 不可或缺的人物就是 引擎 编译器 作用域 JavScript 在预编译后执行代码时 引擎就会对其进行查询 查询分为 LHS Left Hand SideRHS Right Hand Side 即赋值的左侧和右侧 当出现在赋值操作的左侧时进行 LHS 查询 出现在右侧时进行 RHS 查询 例 functionfoo a varb

    2026年3月16日
    2
  • box-sizing:border-box的理解和作用

    box-sizing:border-box的理解和作用要想清楚这个属性的作用,首先要理解盒子模型盒子模型是指:外边距(margin)+border(边框)+内边距(padding)+content(内容)可以把每一个容器,比如div,都看做是一个盒子模型比如你给一个div设置宽高为500px,但实际你设置的只是content,之后你又设置了padding:10px;border:1pxsolidred;这时div的宽高就会变为544px(content500px+padding40px+border4px)相当于一个元素的实际宽高是由

    2025年7月17日
    7
  • Java的throws Exception

    Java的throws Exception转 https www cnblogs com feichengwula articles 3793261 html1 终极解释 throwsExcept 放在方法后边 是 throwsExcept 表示的是本方法不处理异常 交给被调用处处理 如果你不希望异常层层往上抛 你就要用 throwsExcept 而且被调用处必须处理 2 thrownewExce

    2025年8月19日
    6
  • 数据结构之排序算法建议收藏

    排序(Sorting),特别是高效的排序一直是计算机工作学习和研究的重要课题之一,排序有内部排序和外部排序之分,若整个排序过程不需要访问外存便能完成,则称此类排序为内部排序,反之则为外部排序。本篇将对

    2021年12月19日
    57

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号