WINDOWS网络API总结
一:WinSock API WSAStartup,getaddrinfo,socket,connect,send,recv,WSAGetLastError 二:WinINet API InternetOpen:初始化一个应用程序,以使用 WinINet 函数 InternetConnect:建立 Internet 的连接,成功返回非0。如果返回0。要InternetCloseHandle释放这个句柄 InternetOpenURL:通过一个完整的FTP、HTTP的URL打开资源 InternetReadFile,InternetWriteFile
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对象,用指定的类标识符创建一个未初始化的对象 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- active-directory – Active Directory用户 – 所有属性突然
- 仿真文件系统(Windows)
- 汇编 – 如何在DOS中以不同颜色编写文本?
- windows-installer – msi在Inno Setup中返回代码
- active-directory – Windows Server 2003 – 应用WCRY补丁
- Windows API一日一练 13 TranslateMessage函数
- .net – 如何打包单个文件的Windows EXE进行下载?
- botframework – Skype for Business中未显示的卡片和选择(
- windows – 如何通过命令行在全屏视图中打开pdf
- 如何在Windows上为PHP全局设置CURLOPT_CAINFO?