http://www.cnblogs.com/LCCRNblog/p/3833472.html
这一篇博客中,实现了获取http请求/响应后的html源码,现在需要获取http请求/响应的头部Header。。通过对WinHttp Api的查看,现给出实现代码。。
1 // WinHttpTest.cpp : 定义控制台应用程序的入口点。 2 // 3 //#include <stdafx.h> 4 #include <vector> 5 #include <winsock2.h> 6 #include <Winhttp.h> 7 //#include <urlmon.h> 8 #include <windows.h> 9 #include <iostream> 10 #include <fstream> 11 #include <string> 12 #include "AtlBase.h" 13 #include "AtlConv.h" 14 using namespace std; 15 #pragma comment(lib, "winhttp")//这一句不能省略 16 string GetHost(string strUrl) 17 { 18 int indexHttp = strUrl.find("http://"); 19 if(indexHttp != -1) 20 { 21 strUrl = strUrl.substr(7); 22 } 23 else 24 return ""; 25 int indexSlash = strUrl.find("/"); 26 if(indexSlash != -1) 27 { 28 return strUrl.substr(0, indexSlash); 29 } 30 else 31 return strUrl; 32 return ""; 33 } 34 string GetRequestStr(string strUrl) 35 { 36 int indexHttp = strUrl.find("http://"); 37 if(indexHttp != -1) 38 { 39 strUrl = strUrl.substr(7); 40 } 41 else 42 return ""; 43 int indexSlash = strUrl.find("/"); 44 if(indexSlash == -1) 45 { 46 return ""; 47 } 48 else 49 return strUrl.substr(indexSlash); 50 } 51 void GetHtml(string strUrl) 52 { 53 string strHost = GetHost(strUrl);//获取Host 54 string strRequestStr = GetRequestStr(strUrl);//获取请求路径 55 USES_CONVERSION; 56 //2014年7月9日10:02:29 57 //LPCWSTR的定义 typedef const wchar_t* LPCWSTR; 58 //LPSTR的定义 typedef char* LPCWSTR; 59 //LPWSTR的定义 typedef wchar_t* LPWSTR; 60 LPCWSTR host = A2CW(strHost.c_str());//string转换为常量指针类型 61 LPCWSTR requestStr = A2CW(strRequestStr.c_str()); 62 //Variables 63 DWORD dwSize = 0; 64 BOOL bResults = FALSE; 65 66 //Note the definition of HINTERNET 67 HINTERNET hSession = NULL, 68 hConnect = NULL, 69 hRequest = NULL; 70 71 //2014年7月9日10:39:33 72 //Search the WinHttp API 73 //what to do when call the function WinHttpOpen? 74 // Use WinHttpOpen to obtain a session handle. 75 hSession = WinHttpOpen(L"WinHTTP Example/1.0", 76 WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, 77 WINHTTP_NO_PROXY_NAME, 78 WINHTTP_NO_PROXY_BYPASS, 0); 79 // Specify an HTTP server. 80 if (hSession) 81 hConnect = WinHttpConnect(hSession, host, 82 INTERNET_DEFAULT_HTTP_PORT, 0); 83 // Create an HTTP request handle. 84 if (hConnect) 85 hRequest = WinHttpOpenRequest(hConnect, L"GET", requestStr, 86 NULL, WINHTTP_NO_REFERER, 87 NULL, 88 NULL); 89 // Send a request. 90 if (hRequest) 91 bResults = WinHttpSendRequest(hRequest, 92 WINHTTP_NO_ADDITIONAL_HEADERS, 93 0, WINHTTP_NO_REQUEST_DATA, 0, 94 0, 0); 95 // End the request. 96 if (bResults) 97 bResults = WinHttpReceiveResponse(hRequest, NULL); 98 99 //2014年7月9日16:35:40 100 //获取请求Header 101 LPVOID lpOutBuffer = NULL; 102 int temp; 103 if (bResults) 104 { 105 //此处的参数与获取响应的Header参数不同 106 WinHttpQueryHeaders( hRequest, WINHTTP_QUERY_RAW_HEADERS_CRLF|WINHTTP_QUERY_FLAG_REQUEST_HEADERS, 107 WINHTTP_HEADER_NAME_BY_INDEX, NULL, 108 &dwSize, WINHTTP_NO_HEADER_INDEX); 109 110 // Allocate memory for the buffer. 111 if( GetLastError() == ERROR_INSUFFICIENT_BUFFER ) 112 { 113 temp = dwSize/sizeof(WCHAR); 114 lpOutBuffer = new WCHAR[temp]; 115 116 // Now, use WinHttpQueryHeaders to retrieve the header. 117 bResults = WinHttpQueryHeaders( hRequest, 118 WINHTTP_QUERY_RAW_HEADERS_CRLF|WINHTTP_QUERY_FLAG_REQUEST_HEADERS, 119 WINHTTP_HEADER_NAME_BY_INDEX, 120 lpOutBuffer, &dwSize, 121 WINHTTP_NO_HEADER_INDEX); 122 } 123 124 125 } 126 //两种输出方式。。输出header,此处cout行不通 127 wcout<<(WCHAR*)lpOutBuffer; 128 if (bResults) 129 printf("\n%S",lpOutBuffer); 130 131 delete [] lpOutBuffer; 132 133 //2014年7月9日14:43:20 获取响应头部 134 if (bResults) 135 { 136 WinHttpQueryHeaders( hRequest, WINHTTP_QUERY_RAW_HEADERS_CRLF, 137 WINHTTP_HEADER_NAME_BY_INDEX, NULL, 138 &dwSize, WINHTTP_NO_HEADER_INDEX); 139 140 // Allocate memory for the buffer. 141 if( GetLastError() == ERROR_INSUFFICIENT_BUFFER ) 142 { 143 temp = dwSize/sizeof(WCHAR); 144 lpOutBuffer = new WCHAR[temp]; 145 146 // Now, use WinHttpQueryHeaders to retrieve the header. 147 bResults = WinHttpQueryHeaders( hRequest, 148 WINHTTP_QUERY_RAW_HEADERS_CRLF, 149 WINHTTP_HEADER_NAME_BY_INDEX, 150 lpOutBuffer, &dwSize, 151 WINHTTP_NO_HEADER_INDEX); 152 } 153 154 155 } 156 //两种输出方式。。输出header,此处cout行不通 157 wcout<<(WCHAR*)lpOutBuffer; 158 if (bResults) 159 printf("\n%S",lpOutBuffer); 160 161 delete [] lpOutBuffer; 162 } 163 int _tmain(int argc, _TCHAR* argv[]) 164 { 165 166 GetHtml("http://bbs.bccn.net/thread-294526-1-1.html"); 167 system("pause"); 168 return 0; 169 }