C语言实现获取网络时间(c语言实现获取网络时间的代码)

C语言实现获取网络时间(c语言实现获取网络时间的代码)

编码文章call10242025-08-04 21:45:371A+A-

自己写的代码,C语言实现获取网络时间,代码如下:
#include <windows.h>

#include <winhttp.h>

#include <stdio.h>

#include <string.h>

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


void removeQuotes(const char* source, char* destination) {

if (source == NULL || destination == NULL) {

printf("Invalid input parameters.\n");

return;

}

int i = 0, j = 0;

while (source[i] != '\0') {

if (source[i] != '"') {

destination[j++] = source[i];

}

i++;

}

destination[j] = '\0'; // 确保目标字符串以空字符结尾

}

void PrintDate(const char* content) {

const char* dateStr = strstr(content, "var today = \"");

if (dateStr) {

dateStr += 12; // 跳过 "var today = \""

const char* endStr = strstr(dateStr, "\";"); // 查找结尾的双引号和分号

if (endStr) {

char date[100];

int length = endStr - dateStr;

if (length >= sizeof(date)) {

length = sizeof(date) - 1; // 防止溢出

}

strncpy_s(date, sizeof(date), dateStr, length);

date[length] = '\0';

char destination[100]; // 确保目标字符串有足够的空间

removeQuotes(date, destination);

printf("%s\n", destination);

}

}

else {

printf("Date not found.\n");

}

}

int main() {

HINTERNET hSession = WinHttpOpen(L"WinHTTP Example Program", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);

if (!hSession) {

printf("WinHttpOpen failed (%d)\n", GetLastError());

return 1;

}

HINTERNET hConnect = WinHttpConnect(hSession, L"www.gcti.edu.cn", INTERNET_DEFAULT_HTTPS_PORT, 0);

if (!hConnect) {

printf("WinHttpConnect failed (%d)\n", GetLastError());

WinHttpCloseHandle(hSession);

return 1;

}

HINTERNET hRequest = WinHttpOpenRequest(hConnect, L"GET", L"/time/time.asp", NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_SECURE);

if (!hRequest) {

printf("WinHttpOpenRequest failed (%d)\n", GetLastError());

WinHttpCloseHandle(hConnect);

WinHttpCloseHandle(hSession);

return 1;

}

if (!WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0)) {

printf("WinHttpSendRequest failed (%d)\n", GetLastError());

WinHttpCloseHandle(hRequest);

WinHttpCloseHandle(hConnect);

WinHttpCloseHandle(hSession);

return 1;

}

if (!WinHttpReceiveResponse(hRequest, NULL)) {

printf("WinHttpReceiveResponse failed (%d)\n", GetLastError());

WinHttpCloseHandle(hRequest);

WinHttpCloseHandle(hConnect);

WinHttpCloseHandle(hSession);

return 1;

}

DWORD dwSize = 0;

WinHttpQueryDataAvailable(hRequest, &dwSize);

if (dwSize == 0) {

printf("No data available.\n");

WinHttpCloseHandle(hRequest);

WinHttpCloseHandle(hConnect);

WinHttpCloseHandle(hSession);

return 1;

}

char* buffer = (char*)malloc(dwSize + 1);

if (!buffer) {

printf("Memory allocation failed.\n");

WinHttpCloseHandle(hRequest);

WinHttpCloseHandle(hConnect);

WinHttpCloseHandle(hSession);

return 1;

}

DWORD dwDownloaded = 0;

if (!WinHttpReadData(hRequest, (LPVOID)buffer, dwSize, &dwDownloaded)) {

printf("WinHttpReadData failed (%d)\n", GetLastError());

free(buffer);

WinHttpCloseHandle(hRequest);

WinHttpCloseHandle(hConnect);

WinHttpCloseHandle(hSession);

return 1;

}

buffer[dwSize] = '\0'; // Null-terminate the buffer

PrintDate(buffer);

free(buffer);

WinHttpCloseHandle(hRequest);

WinHttpCloseHandle(hConnect);

WinHttpCloseHandle(hSession);

return 0;

}

点击这里复制本文地址 以上内容由文彬编程网整理呈现,请务必在转载分享时注明本文地址!如对内容有疑问,请联系我们,谢谢!
qrcode

文彬编程网 © All Rights Reserved.  蜀ICP备2024111239号-4