#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <wininet.h>
LPCSTR User_Agent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36";
LPCSTR Accept = "Accept: */*";
char *text;
DWORD dword, dwIndex;
int main(int argc, char *argv[])
{
HINTERNET hinternet = InternetOpenA(User_Agent, 1, 0, 0, 0);
HINTERNET hinternet1 = InternetConnectA(hinternet, "down.ali213.net", 443, 0, 0, 3, 0, 0);
HINTERNET hinternet2 = HttpOpenRequestA(hinternet1, "GET", "/pcgame/", "HTTP/1.1", 0, 0,
INTERNET_FLAG_SECURE | INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS, 0);
HttpSendRequestA(hinternet2, Accept, strlen(Accept), 0, 0);
text = malloc(1024);
FILE *fp = fopen("temp.txt", "wb+");
while (InternetReadFile(hinternet2, text, 1024, &dword), dword)
fwrite(text, 1, dword, fp);
fflush(fp);
free(text);
long len = ftell(fp);
fseek(fp, 0, SEEK_SET);
text = malloc(len + 1);
text[len] = '\000';
fread(text, 1, len, fp);
char guo[1];
dword = 1;
HttpQueryInfoA(hinternet2, HTTP_QUERY_RAW_HEADERS_CRLF, guo, &dword, &dwIndex);
char *bt = malloc(dword);
bt[dword - 1] = '\000';
HttpQueryInfoA(hinternet2, HTTP_QUERY_RAW_HEADERS_CRLF, bt, &dword, &dwIndex);
printf("响应标头:\n%s", bt);
free(bt);
wchar_t *zhh = NULL;
if (strstr(text, "charset=utf-8"))
{
int kamande = MultiByteToWideChar(CP_UTF8, 0, text, -1, 0, 0);
zhh = malloc(kamande * sizeof(wchar_t));
MultiByteToWideChar(CP_UTF8, 0, text, -1, zhh, kamande);
WideCharToMultiByte(CP_ACP, 0, zhh, -1, text, len + 1, 0, 0 );
}
for (int i = 0; i < len; i++)
if (text[i] == '\r')
text[i] = '\n';
printf("网页文本内容:\n%s", text);
free(zhh);
free(text);
fclose(fp);
InternetCloseHandle(hinternet2);
InternetCloseHandle(hinternet1);
InternetCloseHandle(hinternet);
system("pause");
return 0;
}