HttpRequest

// HttpRequest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "HttpRequest.h"

#include <afxwin.h>
#include <stdio.h>
#include <windows.h>
#include "Wininet.h"

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



#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// The one and only application object

CWinApp theApp;

using namespace std;


//模拟浏览器发送HTTP请求函数
CString HttpRequest(char * lpHostName,short sPort,char * lpUrl,char * lpMethod,char * lpPostData,int nPostDataLen)
{
    HINTERNET hInternet,hConnect,hRequest;
    BOOL bRet;CString strResponse;

    hInternet = InternetOpenA("User-Agent",INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
    if(!hInternet)
        goto Ret0;

    hConnect = InternetConnectA(hInternet,lpHostName,sPort,NULL,"HTTP/1.1",INTERNET_SERVICE_HTTP,0,0);
    if(!hConnect)
        goto Ret0;

    hRequest = HttpOpenRequestA(hConnect,lpMethod,lpUrl,"HTTP/1.1",NULL,NULL,INTERNET_FLAG_RELOAD,0);
    if(!hRequest)
        goto Ret0;

    //bRet = HttpAddRequestHeadersA(hRequest,"Content-Type: text/plain;charset=UTF-8",strlen("Content-Type: text/plain;charset=UTF-8"),HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDREQ_FLAG_ADD);
    //if(!bRet)
    //goto Ret0;

    bRet = HttpSendRequestA(hRequest,NULL,0,lpPostData,nPostDataLen);

    //while (TRUE)
    //{
    //    char cReadBuffer[1024] = {0};
    //    DWORD dwSize = 1024;
    //    bRet = ::HttpQueryInfoA(hRequest, HTTP_QUERY_CONTENT_BASE, cReadBuffer, &dwSize, 0);
    //    strResponse = cReadBuffer;
    //}

    while(TRUE)
    {
        char cReadBuffer[4096];
        unsigned long lNumberOfBytesRead;
        bRet = InternetReadFile(hRequest,cReadBuffer,sizeof(cReadBuffer) - 1,&lNumberOfBytesRead);
        if(!bRet || !lNumberOfBytesRead)
            break;
        cReadBuffer[lNumberOfBytesRead] = 0;
        strResponse = strResponse + cReadBuffer;
    }

Ret0:
    if(hRequest)
        InternetCloseHandle(hRequest);
    if(hConnect)
        InternetCloseHandle(hConnect);
    if(hInternet)
        InternetCloseHandle(hInternet);

    return strResponse;
}


int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
    int nRetCode = 0;

    // initialize MFC and print and error on failure
    if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
    {
        // TODO: change error code to suit your needs
        _tprintf(_T("Fatal Error: MFC initialization failed\n"));
        nRetCode = 1;
    }
    else
    {
        // TODO: code your application's behavior here.

        //CString strResponse = HttpRequest("translate.google.com",80,"/translate_t?langpair=en|zh-CN","POST","hl=zh-CN&ie=UTF-8&text=this is me&langpair=en|zh-CN",strlen("hl=zh-CN&ie=UTF-8&text=this is me&langpair=en|zh-CN"));
        CHAR bufData[MAX_PATH] = {0};
        sprintf_s(bufData, MAX_PATH, "VER=1.1&CMD=Login&SEQ=1234567&UIN=474557378&PS=6376c32aa4664c4a4404099e73ad5e7a&M5=1&LC=9326B87B234E7235");

        CString strResponse = HttpRequest("tqq.tencent.com",8000,NULL,"POST",bufData,strlen(bufData));
        //CString strResponse = HttpRequest("www.hao123.com",80,NULL, "GET", NULL, 0);
        FILE * fp = fopen("D:\\123.htm","wb");
        fwrite(strResponse,strResponse.GetLength(),1,fp);
        fclose(fp);
        ::MessageBoxA(NULL,strResponse,"123",0);
    }

    return nRetCode;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值