加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

WINDOWS网络API总结

发布时间:2020-12-13 21:07:09 所属栏目:Windows 来源:网络整理
导读:一:WinSock API WSAStartup,getaddrinfo,socket,connect,send,recv,WSAGetLastError 二:WinINet API InternetOpen:初始化一个应用程序,以使用 WinINet 函数 InternetConnect:建立 Internet 的连接,成功返回非0。如果返回0。要InternetCloseHandl

一: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,0);
		if (hINet == NULL)
		{
			printf("InternetOpen Failed. Error code: %dn",GetLastError());
			return false;
		}
		hConnection = InternetConnect(hINet,lpszServerName,iPort,L" ",INTERNET_SERVICE_HTTP,0);
		if (hConnection == NULL)
		{
			printf("InternetConnect Failed. Error code: %dn",GetLastError());
			return false;
		}
		hRequest = HttpOpenRequest(hConnection,L"GET",lpszObjectName,INTERNET_FLAG_KEEP_CONNECTION,0);
		if (hRequest == NULL)
		{
			printf("HttpOpenRequest Failed. Error code: %dn",GetLastError());
			return false;
		}
		if (HttpSendRequest(hRequest,0) == 0)
		{
			printf("HttpSendRequest Failed. Error code: %dn",GetLastError());
			return false;
		}
		printf("Dump:n");
		do
		{
			if (InternetReadFile(hRequest,cBuf,sizeof(cBuf)-1,&dwRead))
			{
				printf("InternetReadFile Failed. Error code: %dn",GetLastError());
				return false;
			}
			else
			{
				if (dwRead == 0)
				{
					break;
				}
				else
				{
					cBuf[dwRead] = 0;
					printf("%s",cBuf);
				}
			}
		} while (TRUE);
		printf("nEnd Dumpn");
		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 OKn");
	}
	else
	{
		printf("HttpDump Failedn");
	}
	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对象,用指定的类标识符创建一个未初始化的对象

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读