Whenever I try to call the WinHttpSendRequest()
function, it always returns null.
It is worth mentioning that the function works fine in a normal .exe
, but doesn't work in a .dll
.
I was trying to find a solution for a long time.
Here is the code:
bool c_lw_http::send_request(std::wstring s_url, std::vector<BYTE>& bt_reply, const PWCHAR psz_type, const LPVOID p_data, const DWORD dw_data_len)
{
bool b_result = false;
if (!m_h_session_) return b_result;
INTERNET_PORT w_srv_port = 0;
std::wstring s_server, s_object;
parse_url_a(s_url, s_server, s_object, w_srv_port);
const HINTERNET h_connect = WinHttpConnect((HINTERNET)m_h_session_, (LPCWSTR)s_server.c_str(), (INTERNET_PORT)w_srv_port, (DWORD)0);
if (!h_connect) return b_result;
LPCWSTR psz_accept_types[2];
psz_accept_types[0] = L"*/*";
psz_accept_types[1] = NULL;
const HINTERNET h_request = WinHttpOpenRequest(h_connect, psz_type,
s_object.c_str(), L"HTTP/1.1", m_psz_referer_, psz_accept_types,
w_srv_port == INTERNET_DEFAULT_HTTPS_PORT ? WINHTTP_FLAG_SECURE : 0);
if (!h_request) {
MessageBoxA(0, "h_request", 0, 0);
}
DWORD dwOptionValue = WINHTTP_DISABLE_REDIRECTS;
WinHttpSetOption(h_request, WINHTTP_OPTION_DISABLE_FEATURE, &dwOptionValue, sizeof(dwOptionValue));
// Custom Header: Content-Type
BOOL b_http_result = WinHttpAddRequestHeaders(h_request,
LWHTTP_CONT_TYPE, -1, WINHTTP_ADDREQ_FLAG_ADD);
if (!b_http_result) {
MessageBoxA(0, "b_http_result", 0, 0);
goto CleanUp;
}
b_http_result = WinHttpSendRequest((HINTERNET)h_request,(LPCWSTR)WINHTTP_NO_ADDITIONAL_HEADERS, (DWORD)0, (LPVOID)p_data, (DWORD)dw_data_len, (DWORD)dw_data_len, (DWORD_PTR)NULL);
if (!b_http_result) {
MessageBoxA(0, "b_http_result2", 0, 0);
goto CleanUp;
}
b_http_result = WinHttpReceiveResponse((HINTERNET)h_request, (LPVOID)NULL);
if (!b_http_result) {
MessageBoxA(0, "b_http_result3", 0, 0);
goto CleanUp;
}
if ((m_dw_last_reply_size_ = read_req_reply(h_request, bt_reply)))
b_result = true;
CleanUp:
if (h_request)
LI_FN(WinHttpCloseHandle).get()((HINTERNET)h_request);
if (h_connect)
LI_FN(WinHttpCloseHandle).get()((HINTERNET)h_connect);
return b_result;
}