最近一直在看有关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. python 部署 Restful web

    使用python web做Restful 风格,很简单,采用Flask框架轻松实现一个RESTful的服务. Restful相关介绍请查看:https://www.ibm.com/developerw ...

  2. MySql 中文乱码解决办法

    mysql存入的中文数据乱码,可能有这两个原因 原因一 : 数据源配置和mysql字符集编码不符,或数据源配置没有设置字符集 解决方案:在数据源配置添加字符集 useUnicode=true& ...

  3. 用JS制作一个信息管理平台完整版

      前  言 JRedu 在之前的文章中,介绍了如何用JS制作一个实用的信息管理平台. 但是那样的平台功能过于简陋了,我们今天来继续完善一下. 首先我们回顾一下之前的内容.   1.JSON的基础知识 ...

  4. 【个人笔记】《知了堂》mysql表连接

    为什么使用表连接 什么是表连接? 如果数据来自多个表,那么可以采用链接查询的方式来实现.因此表连接就是多个表连接合在一起实现查询效果 表连接的原理 表连接采用的是笛卡尔乘积,称之为横向连接. 笛卡尔乘 ...

  5. [Java语言] 《struts2和spring MVC》的区别_动力节点

    1.Struts2是类级别的拦截, 一个类对应一个request上下文,SpringMVC是方法级别的拦截,一个方法对应一个request上下文,而方法同时又跟一个url对应,所以说从架构本身上Spr ...

  6. Hive基础(1)---Hive是什么

    1. Hive是什么 Hive是基于Hadoop的数据仓库解决方案.由于Hadoop本身在数据存储和计算方面有很好的可扩展性和高容错性,因此使用Hive构建的数据仓库也秉承了这些特性. 这是来自官方的 ...

  7. Tomcat的四种基于HTTP协议的Connector性能比较

    Tomcat从5.5版本开始,支持以下四种Connector的配置分别为: <Connector port="8081" protocol="org.apache. ...

  8. 记2017问鼎杯预赛的wp---来自一个小菜鸡的感想

    这次准备写一下几个misc和密码题目..很坑. 打了一整天的比赛,越来越觉得自己很菜了. 有一道题目叫做"真真假假",这道题目只有一个提示--Xor.第一眼知道是异或,也就知道这一 ...

  9. Happy 2006 poj2773

    Happy 2006 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 9049   Accepted: 3031 Descri ...

  10. Average of Levels in Binary Tree

    Given a non-empty binary tree, return the average value of the nodes on each level in the form of an ...