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打造,自认为对底层的理解略有帮助,和大家分享成果和知识点. 已经实现功能有:打开.播放.关闭功能. 核心 ...
随机推荐
- 一个简单的DLL生成和测试
DLL文件内容: SKLDll.h #ifndef _SKLDLL_H_#define _SKLDLL_H_ #ifndef DLL_API #define DLL_API extern " ...
- MySQL内连接和外连接
INNER JOIN(内连接,或等值连接):获取两个表中字段匹配关系的记录. LEFT JOIN(左连接):获取左表所有记录,即使右表没有对应匹配的记录. RIGHT JOIN(右连接): 与 LEF ...
- 通过编写聊天程序来熟悉python中多线程及socket的用法
1.引言 Python中提供了丰富的开源库,方便开发者快速就搭建好自己所需要的应用程序.本文通过编写基于tcp/ip协议的通信程序来熟悉python中socket以及多线程的使用. 2.python中 ...
- css盒模型(Box Model)
所有HTML元素可以看作盒子,在CSS中,"box model"这一术语是用来设计和布局时使用. CSS盒模型本质上是一个盒子,封装周围的HTML元素,它包括:边距,边框,填充,和 ...
- FineReport中如何用JavaScript解决控件值刷新不及时
我们经常利用按钮进行一些页面值的处理工作,但是默认的逻辑造成,每次新填报的值,需要点击下空白区域或是执行某个其他操作才可以被正确读取,那么我们如何处理呢? 例:当我们用常规取值的时候,虽然B3单元格录 ...
- Linux 加阿里yum源
阿里 yum 源设置 阿里云Linux安装镜像源地址:http://mirrors.aliyun.com/CentOS系统更换软件安装源 第一步:备份你的原镜像文件,以免出错后可以恢复.mv /etc ...
- 配置 tsconfig.json
作用 指导编译器如何生成 JS 文件 参数 target: 编译目标平台(es3, es5, es2015) module: 组织代码方式(commonjs, AMD) sourceMap:编译文件对 ...
- SSL Certificates深入理解
http://www.littlewhitedog.com/content-71.html https://www.verisign.com/en_US/website-presence/websit ...
- 深入理解SVG坐标体系和transformations- viewport, viewBox,preserveAspectRatio
本文翻译自blog: https://www.sarasoueidan.com/blog/svg-coordinate-systems/ SVG元素不像其他HTML元素一样受css盒子模型所制约.这个 ...
- Oracle EBS 获取说明性弹性域全局数据元
SELECT b.flex_value_set_id, t.application_column_name, t.form_left_prompt FROM fnd_descriptive_flexs ...