VC HTTPS post
时间: 2023-07-08 08:46:38 浏览: 48
要在 VC 中使用 HTTPS POST,可以使用 WinINet 库中的函数。以下是一个简单的例子:
```c++
#include <windows.h>
#include <wininet.h>
#include <tchar.h>
#pragma comment(lib, "wininet.lib")
int main()
{
HINTERNET hInternet = InternetOpen(_T("MyApp"), INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
if (!hInternet)
{
printf("InternetOpen failed: %d\n", GetLastError());
return 1;
}
HINTERNET hConnect = InternetConnect(hInternet, _T("www.example.com"), INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
if (!hConnect)
{
printf("InternetConnect failed: %d\n", GetLastError());
InternetCloseHandle(hInternet);
return 1;
}
HINTERNET hRequest = HttpOpenRequest(hConnect, _T("POST"), _T("/post"), NULL, NULL, NULL, INTERNET_FLAG_SECURE | INTERNET_FLAG_IGNORE_CERT_CN_INVALID | INTERNET_FLAG_IGNORE_CERT_DATE_INVALID, 0);
if (!hRequest)
{
printf("HttpOpenRequest failed: %d\n", GetLastError());
InternetCloseHandle(hConnect);
InternetCloseHandle(hInternet);
return 1;
}
const char* szPostData = "key1=value1&key2=value2";
if (!HttpSendRequest(hRequest, NULL, 0, (LPVOID)szPostData, strlen(szPostData)))
{
printf("HttpSendRequest failed: %d\n", GetLastError());
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hInternet);
return 1;
}
char szResponse[1024];
DWORD dwRead = 0;
if (!InternetReadFile(hRequest, szResponse, sizeof(szResponse), &dwRead))
{
printf("InternetReadFile failed: %d\n", GetLastError());
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hInternet);
return 1;
}
szResponse[dwRead] = '\0';
printf("Response: %s\n", szResponse);
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hInternet);
return 0;
}
```
注意,在使用 HttpOpenRequest 函数时,需要将 INTERNET_FLAG_SECURE 标志和忽略证书相关的标志一起使用。另外,需要注意在 POST 数据时,需要将数据转换为 LPVOID 类型。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)