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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • tkmybatis逆向工程(java数据类型强制转换方法)

    配置文件pom.xml<build><plugins><plugin><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-maven-plugin</artifactId><version>1.3.7</vers

    2022年4月15日
    34
  • 对ajax的理解面试题_javascript面试题大全

    对ajax的理解面试题_javascript面试题大全前两天面试的时候,面试官问我,你掌握的技能是Ajax,那你给我讲一下它的基本原理吧!妈呀,瞬间脑子空白。当时在门口背了好久的网络知识点,一时竟然说不吃话,只记得什么异步通信,同步数据,面试官的笑让我不寒而栗…………今天整体的整理一遍Ajax的知识点吧。…

    2022年8月27日
    2
  • Java的常用开发工具

    Java的常用开发工具Java开发人员的常用工具java常用的开发工具。都说工欲善其事必先利其器,要想学好java这门语言,选择一款好用顺手的开发工具是必不可少的。另外面试java工作时开发工具的使用也是一个重要的考核点。要想全面了解java开发工具,我们首先需要先了解一下java程序的开发过程,通过这个过程我们能够了解到java开发都需要用到那些工具。首先我们先了解完整项目开发过程,如图所示:

    2022年5月11日
    44
  • oracle 9i安装_oracle9i查看字符集

    oracle 9i安装_oracle9i查看字符集Oracle9iDatabaseRelease2Enterprise/Standard/PersonalEditionforWindowsNT/2000/XPhttp://download.oracle.com/otn/nt/oracle9i/9201/92010NT_Disk1.ziphttp://download.oracle.com/otn/nt/oracle9i/9201/…

    2022年10月29日
    0
  • 免费申请国外免费域名超详细教程[通俗易懂]

    免费申请国外免费域名超详细教程[通俗易懂]1.首先申请免费域名网站:https://my.freenom.com/domains.php2.填入域名,这里我们以xcflag为列(尽量选择复杂一点的或者五个字母以上的域名,因为简单的有些域名是需要收费的),点击检查可用性。3.可以看到很多免费的域名(用的谷歌翻译插件,翻译有时候不是很准确,free翻译过来应该是免费而不是自由,之后会写一些关于谷歌插件的笔记,详细讲解)4.我们选择xcflag.tk点击立即获取,稍等一会点击购物车查看绿色按钮5.默认三个月试用,这里下拉框我们选择十二个月

    2022年6月30日
    69
  • Java中利用DatagramPacket与DatagramSocket进行通讯的示例

    Java中利用DatagramPacket与DatagramSocket进行通讯的示例对以下demo进行了扩展,增了消息循环和等待。 Java中的DatagramPacket与DatagramSocket的初步扩展的代码如下:1.接收端工程代码:由于接收端的控制台log会被发送端的log冲掉,所以把log写到文件中。packagecom.ameyume.receiver;importjava.io.File;importjava.io.FileNotFoundExcep

    2022年5月4日
    39

发表回复

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

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