最近一直在看有关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文件运行,也可打开这个文件与通过浏览器打开的网页源码就行对比。。

VS2008 C++ 利用WinHttp API获取任意Http网址的源码的更多相关文章

  1. VS2008 C++ 利用WinHttp API获取Http请求/响应头部Header

    http://www.cnblogs.com/LCCRNblog/p/3833472.html 这一篇博客中,实现了获取http请求/响应后的html源码,现在需要获取http请求/响应的头部Head ...

  2. 【转】百度API获取城市名地名(附源码)

    在做一个软件时,用到了定位功能.网上有很多关于google 的GPS定位,但网上关于google定位都没有用, 搜索下原因:(这里建议大家在中国就尽量不使用系统自带的定位) 因为Google的服务器不 ...

  3. 微信公众平台开发-access_token获取及应用(含源码)

    微信公众平台开发-access_token获取及应用(含源码)作者: 孟祥磊-<微信公众平台开发实例教程> 很多系统中都有access_token参数,对于微信公众平台的access_to ...

  4. leaflet结合geoserver利用WFS服务实现图层删除功能(附源码下载)

    前言 leaflet 入门开发系列环境知识点了解: leaflet api文档介绍,详细介绍 leaflet 每个类的函数以及属性等等 leaflet 在线例子 leaflet 插件,leaflet ...

  5. 微信公众平台开发2-access_token获取及应用(含源码)

    微信公众平台开发-access_token获取及应用(含源码) 很多系统中都有access_token参数,对于微信公众平台的access_token参数,微信服务器判断该公众平台所拥有的权限,允许或 ...

  6. 转:微信开发之使用java获取签名signature(贴源码,附工程)

    微信开发之使用java获取签名signature(贴源码,附工程) 标签: 微信signature获取签名 2015-12-29 22:15 6954人阅读 评论(3) 收藏 举报  分类: 微信开发 ...

  7. Java关于ReentrantLock获取锁和释放锁源码跟踪

    通过对ReentrantLock获取锁和释放锁源码跟踪主要想进一步深入学习AQS. 备注:AQS中的waitStatus状态码含义:

  8. 【转】反编译获取任何微信小程序源码(完)

    一.前言最近在学习微信小程序开发,半个月学习下来,很想实战一下踩踩坑,于是就仿写了一个阿里妈妈淘宝客小程序的前端实现,过程一言难尽,差不多两周时间过去了,发现小程序的坑远比想象的要多的多!!在实际练手 ...

  9. 劳动节脑洞大开!利用Debug API 获取 加壳客户端的MD5值

    系统 : Windows xp 程序 : 某游戏客户端 程序下载地址 :不提供 要求 : 远程注入 & 获取MD5值 使用工具 : vc++6.0 & OD 案例说明: 该游戏客户端对 ...

随机推荐

  1. 获取sd卡的总大小和可用大小

  2. Linux下利用expect,不用交互模式,直接登陆远程主机

    Linux环境下只有在机器20.200.254.18上ssh dataconv@20.200.31.23才能连接到23的机器,而且还需要输入密码(每次都需要输入地址,密码很烦),所以利用expect写 ...

  3. vue源码学习-vnode的挂载和更新流程

    概述 本文主要介绍在视图的渲染过程中,Vue 是如何把 vnode 解析并挂载到页面中的.我们通过一个最简单的例子来分析主要流程: <div id="app"> {{s ...

  4. SSM之框架整合

    前言 SSM框架,即Spring + Spring MVC + MyBatis的整合框架集,是继SSH后比较主流的Java EE企业级框架,采用标准的MVC模式,项目结构与微软的ASP.NET MVC ...

  5. PyTorch教程之Tensors

    Tensors类似于numpy的ndarrays,但是可以在GPU上使用来加速计算. 一.Tensors的构建 from __future__ import print_function import ...

  6. 在JavaScript中使用json.js:Ajax项目之POST请求(异步)

    经常在百度搜索框输入一部分关键词后,弹出候选关键热词.现在我们就用Ajax技术来实现这一功能. 一.下载json.js文件 百度搜一下,最好到json官网下载,安全起见. 并与新建的两个文件部署如图 ...

  7. HCatalog

    HCatalog HCatalog是Hadoop中的表和存储管理层,能够支持用户用不同的工具(Pig.MapReduce)更容易地表格化读写数据. HCatalog从Apache孵化器毕业,并于201 ...

  8. Linux入门之常用命令(12)用户管理

    [用户管理] linux如何查看所有的用户和组信息的方法: 1.cat /etc/passwd: 2.cat /etc/group 1. useradd useradd 命令可以创建一个新的用户帐号, ...

  9. spring jar包

    org.springframework.aop- 3.0.0.RELEASE--------------------Spring的面向切面编程,提供AOP(面向切面编程)实现 org.springfr ...

  10. 51nod 1451 合法三角形 判斜率去重,时间复杂度O(n^2)

    题目: 这题我WA了3次,那3次是用向量求角度去重算的,不知道错在哪了,不得不换思路. 第4次用斜率去重一次就过了. 注意:n定义成long long,不然求C(3,n)时会溢出. 代码: #incl ...