惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
The Exploit Database - CXSecurity.com
V
Vulnerabilities – Threatpost
Google DeepMind News
Google DeepMind News
Attack and Defense Labs
Attack and Defense Labs
Webroot Blog
Webroot Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
TaoSecurity Blog
TaoSecurity Blog
I
Intezer
Application and Cybersecurity Blog
Application and Cybersecurity Blog
N
News | PayPal Newsroom
S
Security Affairs
T
Tor Project blog
P
Proofpoint News Feed
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
S
Security @ Cisco Blogs
H
Heimdal Security Blog
Hacker News: Ask HN
Hacker News: Ask HN
Help Net Security
Help Net Security
U
Unit 42
云风的 BLOG
云风的 BLOG
The Hacker News
The Hacker News
Cisco Talos Blog
Cisco Talos Blog
量子位
F
Full Disclosure
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 叶小钗
有赞技术团队
有赞技术团队
T
Troy Hunt's Blog
P
Privacy & Cybersecurity Law Blog
Forbes - Security
Forbes - Security
人人都是产品经理
人人都是产品经理
L
Lohrmann on Cybersecurity
Apple Machine Learning Research
Apple Machine Learning Research
Microsoft Security Blog
Microsoft Security Blog
博客园 - Franky
腾讯CDC
AI
AI
Last Week in AI
Last Week in AI
Latest news
Latest news
Google Online Security Blog
Google Online Security Blog
N
Netflix TechBlog - Medium
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
IT之家
IT之家
Martin Fowler
Martin Fowler
Blog — PlanetScale
Blog — PlanetScale
V2EX - 技术
V2EX - 技术
酷 壳 – CoolShell
酷 壳 – CoolShell

博客园 - chuncn

Eslint 规则说明 磁盘阵列RAID原理、种类及性能优缺点对比 asp.net中调用命令行 JavaScript中的CSS属性对照表 webRTC-实时流媒体的福音 qt 5.1 quick vs qt 4.x quick for control zeroc ice的概念、组成与服务 国内外期货、外汇、股指期货 交易时间 使用Visual Leak Detector for Visual C++ 捕捉内存泄露 window8 metro 之 RSA sqlite in qt Windows环境下使用Boost Qt 表格&列表数据驱动化(c++) 原创 Qt读写INI配置文件 Qt多国语言的实现与切换(国际化) Qt编码风格 -- 转 win32里玩事件-转 c++中捕捉内存泄露、异常 win32 DirectUI控件开发与调用指南
win32 http download
chuncn · 2012-03-26 · via 博客园 - chuncn

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[1024];

	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), 0);
	if (nRet == SOCKET_ERROR)
	{
		//log.debug("send() error");
		closesocket(Socket);    
		return false;
	}

	//
	// Receive the file contents and print to stdout
	//
	while(1)
	{
		// Wait to receive, nRet = NumberOfBytesReceived
		nRet = recv(Socket, szBuffer, sizeof(szBuffer), 0);
		if (nRet == SOCKET_ERROR)
		{
			//log.debug("recv() error");
			break;
		}

		//log.debug("\nrecv() returned %d bytes", nRet);
		// Did the server close the connection?
		if (nRet == 0)
			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",80,"/","KeyBoardSelectionComboBox.zip","c:\\");
	bool b = client->DownloadFile("www.zt.cn",88,"/ClientBin/","Main.xap","c:\\");
	
	return 0;
}