Win32 HTTP Download
头文件HTTPClient.h:
#pragma once
#ifndef HTTPClient_H_
#define HTTPClient_H_ #include <string>
using namespace std; class HTTPClient
{
public:
HTTPClient(void);
~HTTPClient(void); bool DownloadFile(string serverName,int port,string sourcePath, string fileName, string localDirectory);
};
#endif
源文件HTTPClient.cpp:
#include "StdAfx.h"
#include "HTTPClient.h"
#include <winsock.h>
#include <fstream>
//#include "Log.h"
#pragma comment(lib,"ws2_32.lib") HTTPClient::HTTPClient(void)
{
} HTTPClient::~HTTPClient(void)
{
} bool HTTPClient::DownloadFile(string serverName,int port, string sourcePath, string fileName, string localDirectory)
{
//Log &log = Log::getLog("HTTPClient","DownloadFile");
//log.debug("\nserverName = %s; sourcePath = %s; fileName = %s\n",serverName.c_str(),sourcePath.c_str(),fileName.c_str()); WSADATA wsaData;
int nRet; //
// Initialize WinSock.dll
//
nRet = WSAStartup(0x101, &wsaData);
if (nRet)
{
//log.debug("\nWinSock.dll initialize failed. WSAStartup(): %d\n", nRet);
WSACleanup();
return false;
}
else
{
//log.debug("\nWSAStartup success!\n");
} //
// Check WinSock version
//
if (wsaData.wVersion != 0x101)
{
//log.debug("\nWinSock version not supported\n");
WSACleanup();
return false;
}
else
{
//log.debug("\nwsaData.wVersion ==0x101\n");
} ofstream fout;
string newfile = localDirectory + fileName;
fout.open(newfile.c_str(),ios_base::binary);
if(!fout.is_open())
{
//log.debug("open newfile error!");
}
else
{
//log.debug("open local newfile success!");
} IN_ADDR iaHost;
LPHOSTENT lpHostEntry; iaHost.s_addr = inet_addr(serverName.c_str());
if (iaHost.s_addr == INADDR_NONE)
{
// Wasn't an IP address string, assume it is a name
lpHostEntry = gethostbyname(serverName.c_str());
}
else
{
// It was a valid IP address string
lpHostEntry = gethostbyaddr((const char *)&iaHost,
sizeof(struct in_addr), AF_INET);
}
if (lpHostEntry == NULL)
{
//log.debug("gethostbyname() error");
return false;
}
else
{
//log.debug("gethostbyname() success!");
} //
// Create a TCP/IP stream socket
//
SOCKET Socket; Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (Socket == INVALID_SOCKET)
{
//log.debug("socket() error");
return false;
}
else
{
//log.debug("socket() success!");
} //
// Find the port number for the HTTP service on TCP
//
SOCKADDR_IN saServer;
//LPSERVENT lpServEnt; //lpServEnt = getservbyname("http", "tcp");
//if (lpServEnt == NULL)
// saServer.sin_port = htons(80);
//else
// saServer.sin_port = lpServEnt->s_port; saServer.sin_port = htons(port); //
// Fill in the rest of the server address structure
//
saServer.sin_family = AF_INET;
saServer.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list); //
// Connect the socket
//
nRet = connect(Socket, (LPSOCKADDR)&saServer, sizeof(SOCKADDR_IN));
if (nRet == SOCKET_ERROR)
{
//log.debug("connect() error");
closesocket(Socket);
return false;
}
else
{
//log.debug("connect() success!");
} //
// Format the HTTP request
//
char szBuffer[]; string requestFile = sourcePath + fileName; sprintf(szBuffer, "GET %s\n", requestFile.c_str());
//log.debug("sended GET %s Request",requestFile.c_str());
nRet = send(Socket, szBuffer, strlen(szBuffer), );
if (nRet == SOCKET_ERROR)
{
//log.debug("send() error");
closesocket(Socket);
return false;
} //
// Receive the file contents and print to stdout
//
while()
{
// Wait to receive, nRet = NumberOfBytesReceived
nRet = recv(Socket, szBuffer, sizeof(szBuffer), );
if (nRet == SOCKET_ERROR)
{
//log.debug("recv() error");
break;
} //log.debug("\nrecv() returned %d bytes", nRet);
// Did the server close the connection?
if (nRet == )
break;
// Write to stdout
// fwrite(szBuffer, nRet, 1, stdout);
fout.write(szBuffer,nRet);
}
closesocket(Socket);
fout.close(); WSACleanup();
return true;
}
调用示例:
#include "stdafx.h"
#include "HTTPClient.h" int _tmain(int argc, _TCHAR* argv[])
{ HTTPClient* client;
client=new HTTPClient();
bool a = client->DownloadFile("127.0.0.1",,"/","KeyBoardSelectionComboBox.zip","c:\\");
bool b = client->DownloadFile("www.zt.cn",,"/ClientBin/","Main.xap","c:\\"); return ;
}
Win32 HTTP Download的更多相关文章
- gtk+2.24.0-glib-2.28.1-staticLib-mingw32-x86-2016-08-10.7z
GTK_PATH=D:/MSYS/opt/gtk+2.24.0-staticLib b1-static.sh --------------------------------------------- ...
- 怎样在WINDOWS下面编译LIBCURL
我测试过,好像没OK This is a short note about building cURL with SSL support on Windows. Tools required: cUR ...
- cocos2d-x 从win32到android移植的全套解决方案
引言:我们使用cocos2d-x引擎制作了一款飞行射击游戏,其中创新性地融入了手势识别功能.但是我们在移植过程中遇到了很多的问题,同时也发现网上的资料少而不全.所以在项目行将结束的时候,我们特地写了这 ...
- Serial Port Programming using Win32 API(转载)
In this tutorial we will learn How to communicate with an external device like a microcontroller boa ...
- Download the WDK, WinDbg, and associated tools
Download the WDK, WinDbg, and associated tools This is where you get your Windows Driver Kit (WDK) a ...
- 【转】VS2012编译出来的程序,在XP上运行,出现“.exe 不是有效的 win32 应用程序” “not a valid win32 application”
原文网址:http://www.cnblogs.com/Dageking/archive/2013/05/15/3079394.html VS2012编译出来的程序,在XP上运行,出现“.exe 不是 ...
- win32进阶之路:程序托盘图标+右键弹出菜单
开场白 本次介绍两个非常棒且实用的技巧:程序托盘图标和右键弹出菜单,效果如下图. 程序托盘图标用了迅雷的图标,右键点击时候会弹出三个选项的菜单. 程序托盘图标设置 我会用尽可能清晰明了的步骤介绍方式 ...
- VS2012编译出来的程序,在XP上运行,出现“.exe 不是有效的 win32 应用程序” “not a valid win32 application”
升级vs2010到vs2012,突然发现build出来的应用程序无法运行,提示“不是有效的 win32 应用程序” or “not a valid win32 application”. 参考CSDN ...
- WIN32不得不会:视频播放器
我愿分享我所有的技术,你可愿意做我的朋友? ----赵大哥 为何要写这篇博客 纯WIN32API打造,自认为对底层的理解略有帮助,和大家分享成果和知识点. 已经实现功能有:打开.播放.关闭功能. 核心 ...
随机推荐
- php返回数组后处理(开户成功后弹窗提示)
1. 在注册的时候,注册成功后经常会弹窗提示自己注册的信息,这类做法需要返回mysql数据库中获取的数组值,返回给前台页面,赋值给弹窗. 2.做法: 返回数组 打印的数组的值 返回数组处理 赋值给弹窗 ...
- Vue如何使用动态刷新Echarts组件
这次给大家带来Vue如何使用动态刷新Echarts组件,Vue使用动态刷新Echarts组件的注意事项有哪些,下面就是实战案例,一起来看一下. 需求背景:dashboard作为目前企业中后台产品的“门 ...
- Matlab给三维点云添加高斯噪声和随机噪声
写在前面 在我们进行点云配准一类的模拟实验时,第一步就是对原始点云进行适当的RT变换,并添加一定的噪声,得到测量点云,然后才可以用我们的算法去进行后面的配准操作.在添加噪声这一块,matlab里并没有 ...
- Oracle数据库函数总结
1.ceil()向上取整,结果为124.select ceil(123.123) from dual;2.floor()向下取整,结果为123.select floor(123.123) from d ...
- ArcGIS10+:ArcGIS version not specified. You must call RuntimeManager.Bind before creating any ArcGIS
ArcGIS10+版本,使用VS创建一个简单的AE应用程序,然后拖放一个toolbar.LicenseControl以及MapControl控件. 接着编译应用程序,编译成功. 然后单击F5运行程序, ...
- CentOS6.5安装vncserver实现图形化访问
一. 安装gnome图形化桌面 #yum groupinstall -y "X Window System" #yum groupinstall -y "Desktop& ...
- webpack+express多页站点开发
学习了webpack门级的教程后,觉得可能是专门为单页应用而量身打造的,比如webpack+react.webpack+vue等,都可以解决各种资源的依赖加载.打包的问题.甚至css都是打包在js里去 ...
- C语言const与#define
const 定义的是变量不是常量,只是这个变量的值不允许改变是常变量!带有类型.编译运行的时候起作用存在类型检查. define 定义的是不带类型的常数,只进行简单的字符替换.在预编译的时候起作用,不 ...
- 数据库复制 Nacicate Premium
之前都是“备份-还原”,抑或“导出-导入”.今天在将SqlServer中的数据导入到MySql中时发现了一个非常方便的方法,无需任何繁琐的配置和操作.废话少说,进入正题: 工具:Navicat Pre ...
- nodepad++添加新主题
https://www.cnblogs.com/d0main/p/6915460.html