一:WinSock API

WSAStartup,getaddrinfo,socket,connect,send,recv,WSAGetLastError


二:WinINet API

InternetOpen:初始化一个应用程序,以使用 WinINet 函数

InternetConnect:建立 Internet 的连接,成功返回非0。如果返回0。要InternetCloseHandle释放这个句柄

InternetOpenURL:通过一个完整的FTP、HTTP的URL打开资源

InternetReadFile,InternetWriteFile


HTTPOpenRequest:一旦和服务器的连接已经建立,我们打开了想要的文件。HttpOpenRequest和HttpOpenRequest一起工作打开文件。HttpOpenRequest去创建个请求句柄并且把参数存储在句柄中。HttpSendRequest把请求参数送到HTTP服务器

HTTPQueryInfo:用来查询一个HTTP请求的信息

使用样例举例如下:

https://blog.csdn.net/DebugFan/article/details/7549747

代码有改动:

// WinINet.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#include <wininet.h>

#pragma comment(lib,"wininet.lib")

BOOL HttpDump(const TCHAR *lpszServerName, int iPort, const TCHAR *lpszObjectName)
{
	TCHAR cBuf[2048];
	DWORD dwRead;
	HINTERNET hINet = NULL, hConnection = NULL, hRequest = NULL;
	__try
	{
		hINet = InternetOpen(L"HttpDump/1.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
		if (hINet == NULL)
		{
			printf("InternetOpen Failed. Error code: %d\n", GetLastError());
			return false;
		}
		hConnection = InternetConnect(hINet, lpszServerName, iPort, L" ", L" ", INTERNET_SERVICE_HTTP, 0, 0);
		if (hConnection == NULL)
		{
			printf("InternetConnect Failed. Error code: %d\n", GetLastError());
			return false;
		}
		hRequest = HttpOpenRequest(hConnection, L"GET", lpszObjectName, NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
		if (hRequest == NULL)
		{
			printf("HttpOpenRequest Failed. Error code: %d\n", GetLastError());
			return false;
		}
		if (HttpSendRequest(hRequest, NULL, 0, NULL, 0) == 0)
		{
			printf("HttpSendRequest Failed. Error code: %d\n", GetLastError());
			return false;
		}
		printf("Dump:\n");
		do
		{
			if (InternetReadFile(hRequest, cBuf, sizeof(cBuf)-1, &dwRead))
			{
				printf("InternetReadFile Failed. Error code: %d\n", GetLastError());
				return false;
			}
			else
			{
				if (dwRead == 0)
				{
					break;
				}
				else
				{
					cBuf[dwRead] = 0;
					printf("%s", cBuf);
				}
			}
		} while (TRUE);
		printf("\nEnd Dump\n");
		return TRUE;
	}
	__finally
	{
		if (hRequest)
		{
			InternetCloseHandle(hRequest);
		}
		if (hINet)
		{
			InternetCloseHandle(hINet);
		}
		if (hConnection)
		{
			InternetCloseHandle(hConnection);
		}
	}
	return FALSE;
}

int _tmain(int argc, _TCHAR* argv[])
{
	if (HttpDump(L"www.baidu.com", 80, L""))
	{
		printf("HttpDump OK\n");
	}
	else
	{
		printf("HttpDump Failed\n");
	}
	return 0;
}

结果是这样的:

HttpSendRequest Failed. Error code: 12029

HttpDump Failed


查询了一下12029:ERROR_INTERNET_CANNOT_CONNECT:The attempt to connect to the server failed.

估计是这么玩baidu是没有权限的吧~~~



三:COM接口

URLDownloadToFile:文件下载

CoInitialize:用来告诉 Windows以单线程的方式创建com对象

CoCreateInstance:函数名。用指定的类标识符创建一个Com对象,用指定的类标识符创建一个未初始化的对象