VS2008 C++ 利用WinHttp API获取任意Http网址的源码

时间:2023-03-09 16:50:08
VS2008 C++ 利用WinHttp API获取任意Http网址的源码

最近一直在看有关Http的知识,对其基本的理论知识已经有所掌握,想通过一个C++具体的例子进行实际操作。。于是上网查找了很多资料,发现在Windows系统上,可以通过WinHttp API接口开啊Http,于是仿照网上例子编写一个获取网页源码的C++程序。其中的代码基本是copy网友,主要是自己对代码的理解,并以此作为入门。

例子代码如下:

 // WinHttpTest.cpp : 定义控制台应用程序的入口点。
//
//#include <stdafx.h>
#include <vector>
#include <winsock2.h>
#include <Winhttp.h>
//#include <urlmon.h>
#include <windows.h>
#include <iostream>
#include <fstream>
#include <string>
#include "AtlBase.h"
#include "AtlConv.h"
using namespace std;
#pragma comment(lib, "winhttp")//这一句不能省略
string GetHost(string strUrl)
{
int indexHttp = strUrl.find("http://");
if(indexHttp != -)
{
strUrl = strUrl.substr();
}
else
return "";
int indexSlash = strUrl.find("/");
if(indexSlash != -)
{
return strUrl.substr(, indexSlash);
}
else
return strUrl;
return "";
}
string GetRequestStr(string strUrl)
{
int indexHttp = strUrl.find("http://");
if(indexHttp != -)
{
strUrl = strUrl.substr();
}
else
return "";
int indexSlash = strUrl.find("/");
if(indexSlash == -)
{
return "";
}
else
return strUrl.substr(indexSlash);
}
string GetHtml(string strUrl)
{
string strHost = GetHost(strUrl);//获取Host
string strRequestStr = GetRequestStr(strUrl);//获取请求路径
USES_CONVERSION;
//2014年7月9日10:02:29
//LPCWSTR的定义 typedef const wchar_t* LPCWSTR;
//LPSTR的定义 typedef char* LPCWSTR;
//LPWSTR的定义 typedef wchar_t* LPWSTR;
LPCWSTR host = A2CW(strHost.c_str());//string转换为常量指针类型
LPCWSTR requestStr = A2CW(strRequestStr.c_str());
//Variables
DWORD dwSize = ;
DWORD dwDownloaded = ;
LPSTR pszOutBuffer;
vector <string> vFileContent;
BOOL bResults = FALSE; //Note the definition of HINTERNET
HINTERNET hSession = NULL,
hConnect = NULL,
hRequest = NULL;
string strHtml = "";// store the html code
string str;//temporary variables
ofstream out("test.html",ios::binary);//output the html code to a html text;
//2014年7月9日10:39:33
//Search the WinHttp API
//what to do when call the function WinHttpOpen?
// Use WinHttpOpen to obtain a session handle.
hSession = WinHttpOpen(L"WinHTTP Example/1.0",
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
WINHTTP_NO_PROXY_NAME,
WINHTTP_NO_PROXY_BYPASS, );
// Specify an HTTP server.
if (hSession)
hConnect = WinHttpConnect(hSession, host,
INTERNET_DEFAULT_HTTP_PORT, );
// Create an HTTP request handle.
if (hConnect)
hRequest = WinHttpOpenRequest(hConnect, L"GET", requestStr,
NULL, WINHTTP_NO_REFERER,
NULL,
NULL);
// Send a request.
if (hRequest)
bResults = WinHttpSendRequest(hRequest,
WINHTTP_NO_ADDITIONAL_HEADERS,
, WINHTTP_NO_REQUEST_DATA, ,
, );
// End the request.
if (bResults)
bResults = WinHttpReceiveResponse(hRequest, NULL); //obtain the html source code
if (bResults)
do
{
// Check for available data.
dwSize = ;
if (!WinHttpQueryDataAvailable( hRequest, &dwSize))
printf( "Error %u in WinHttpQueryDataAvailable.\n",
GetLastError());
// Allocate space for the buffer.
pszOutBuffer = new char[dwSize+];
if (!pszOutBuffer)
{
printf("Out of memory\n");
dwSize=;
}
else
{
// Read the Data.
ZeroMemory(pszOutBuffer, dwSize+);
if (!WinHttpReadData( hRequest, (LPVOID)pszOutBuffer,
dwSize, &dwDownloaded))
{
printf( "Error %u in WinHttpReadData.\n",
GetLastError());
}
else
{
//printf("%s", pszOutBuffer);
// Data in vFileContent
vFileContent.push_back(pszOutBuffer); }
// Free the memory allocated to the buffer.
delete [] pszOutBuffer;
}
} while (dwSize>);
// Keep checking for data until there is nothing left.
// Report any errors.
if (!bResults)
printf("Error %d has occurred.\n",GetLastError());
// Close any open handles.
if (hRequest) WinHttpCloseHandle(hRequest);
if (hConnect) WinHttpCloseHandle(hConnect);
if (hSession) WinHttpCloseHandle(hSession); for(int i=;i<(int)vFileContent.size();i++)
{
str=vFileContent[i];
out<<str;
strHtml += vFileContent[i];
}
out.close();
return strHtml;
}
int _tmain(int argc, _TCHAR* argv[])
{ string str = GetHtml("http://bbs.bccn.net/thread-294526-1-1.html");
//output the html code
//cout << str << endl;
system("pause");
return ;
}

执行后,可以双击tes.html文件运行,也可打开这个文件与通过浏览器打开的网页源码就行对比。。