Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
So I try to write a simple code for HTTP GET and POST sessions.
InternetOpen(), InternetConnect() and OpenRequest() functions work, or at least they provide a valid pointer. Now when I call the HttpSendRequest() function, it keeps coming back with an error 12005 ("The URL is invalid"). The URL is: http://145.239.244.171:3001/api/get/customers.
Thanks for your help.
C++
LPCWSTR host = L"http://145.239.244.171";
LPCWSTR url = L"api/get/customers";
HINTERNET hIntOpen = InternetOpen((LPCTSTR)"Mozilla/5.0 (Windows NT 6.1; rv:58.0) Gecko/20100101 Firefox/58.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
HINTERNET hIntConnect = InternetConnect(hIntOpen, host, 3001, NULL, NULL, INTERNET_SERVICE_HTTP, INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_NO_CACHE_WRITE, 0);
HINTERNET hHttpOpenReq = HttpOpenRequest(hIntConnect, _T("GET"), url, NULL, NULL, 0, INTERNET_FLAG_RELOAD, 0);

BOOL hHttpSendReq = HttpSendRequest(hHttpOpenReq, NULL, NULL, 0, 0);

if (!hHttpSendReq)
{
    DWORD ErrorNum = GetLastError();
    std::cout << "Failed to open URL \nError No: " << ErrorNum;
    InternetCloseHandle(hIntConnect);
    InternetCloseHandle(hIntOpen);
    getchar();
    return 0;
}


What I have tried:

I checked many examples on the net, I tried to set different arguments in the function calls, but my assumption is still just an assumption. I also tried to provide the port number (3001) in the InternetConnect() function instead of INTERNET_DEFAULT_HTTP_PORT.
Posted
Updated 22-Feb-18 10:37am
v5
Comments
phil.o 19-Feb-18 18:19pm    
IMO, the port number should be part of the host, not the url. Have you tried to declare host as http://145.255.244.171:3001 and url as /api/get/customers instead?
m_smith 19-Feb-18 18:24pm    
In that case I get an error 12007 ("The server name could not be resolved").
Rick York 19-Feb-18 19:15pm    
Have you tried it with the port on the end of the URL : /api/get/customers:3001?
m_smith 19-Feb-18 19:24pm    
Yeah, the same result...
phil.o 19-Feb-18 20:40pm    
Could you try
LPCWSTR host = L"145.255.244.171";
LPCWSTR url = L"/api/get/customers";
// ...
HINTERNET hIntConnect = InternetConnect(hIntOpen, host, 3001, NULL, NULL, INTERNET_SERVICE_HTTP, INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_NO_CACHE_WRITE, 0);
?
It seems to me specifying the protocol in the server name is redundant, since this protocol is later specified in the call to the InternetConnect function.

Ok, thanks for your efforts, guys, I found the solution, i should have removed 'http://' from the URL... that's it :) if anybody is interested, here is the code :

int main()
{
	LPCWSTR host = _T("145.239.244.171");
	LPCWSTR url = _T("api/get/customers");
	HINTERNET hIntOpen = InternetOpen(_T("Mozilla/5.0 (Windows NT 6.1; rv:58.0) Gecko/20100101 Firefox/58.0"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
	HINTERNET hIntConnect = InternetConnect(hIntOpen, host, 3001, NULL, NULL, INTERNET_SERVICE_HTTP, INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_NO_CACHE_WRITE, 0);
	HINTERNET hHttpOpenReq = HttpOpenRequest(hIntConnect, _T("GET"), url, NULL, NULL, 0, INTERNET_FLAG_RELOAD, 0);

	BOOL hHttpSendReq = HttpSendRequest(hHttpOpenReq, NULL, NULL, 0, 0);
 
Share this answer
 
You must learn to understand that the error is the answer of the server to your request. That means that the server dont understand your URL. I would guess that the URL isnt valid or you are on the wrong port. Why dont you use the standard port 80?

Try the request in the browser.

Tip: use for all string the same macro. => _T()
Tip 2: use Wireshark to see the network traffic
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900