WinINet - HttpSendRequest() Error

Hello again,
It appears as if the windows API's are once again giving me trouble.
I have this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
std::vector<std::string> defs;
HINTERNET c=InternetOpen("Hom's AI", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, NULL);
HINTERNET ws=InternetConnect(c,"services.aonaware/.com",INTERNET_DEFAULT_HTTP_PORT,NULL,NULL,INTERNET_SERVICE_HTTP,0,0);
std::string rtypestr="GET";
std::string versionstr="HTTP/1.1";
const char* rtype=rtypestr.c_str();
const char* version=versionstr.c_str();
PCTSTR accept[]={"text/html", "application/xhtml+xml", "application/xml;q=0.9", "image/webp", NULL};
					if(std::string::npos!=s.rfind("programming dictionary"))
					{
						//http://services.aonaware.com//DictService/DictService.asmx/DefineInDict?dictId=foldoc&word=
		std::string word;
		voce::synthesize("Input Word");
		std::cout << "Input Word" << std::endl;
		std::cin >> word;
		std::stringstream wordstrm;
		wordstrm << "//DictService/DictService.asmx/DefineInDict?dictId=foldoc&word=" << word;
		std::string wordstr=wordstrm.str();
		const char* wordc=wordstr.c_str();
		HINTERNET h=HttpOpenRequest(ws, rtype, wordc, version, *accept, NULL, INTERNET_FLAG_CACHE_IF_NET_FAIL, 0);
		BOOL res=HttpSendRequest(h, NULL, 0, NULL, 0);
		ErrorExit("HSR");
		if(res==true)
		{
        	    std::ofstream w;
        	    DWORD dwBytes;
        	    char ch;
        	    w.open("webpage.txt");
        					while(InternetReadFile(h,&ch,1,&dwBytes))
        		{
            		if(dwBytes != 1)break;
            		w << ch;
        		}
        		w.close();
        		ErrorExit("IRF");
    	    }


This code runs fine, except the HttpSendRequest() function fails with error 12018, which indicates an invalid handle was provided.

So, what is wrong, as it appears as if the handle is valid

Info:
IDE: Orwell Dev-C++
Compiler: TDM-GCC 4.8.1 32-bit
OS: Windows 8.1 32-bit

Includes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <windows.h>
#include <wininet.h>
#include <strsafe.h> //ErrorExit()
#include <iostream>
#include <iomanip> //used elsewhere
#include <cstdlib> //used elsewhere
#include <ctime> //used elsewhere
#include <string>
#include <sstream>
#include <fstream>
#include <vector> //used elsewhere
#include <jni.h> //needed for voce
#include <voce.h> //speech recognition/synthesis library
#include "comp32.h" //ErrorExit() 


Relevant MSDN Documentation:
HttpOpenRequest(): https://msdn.microsoft.com/en-us/library/windows/desktop/aa384233(v=vs.85).aspx
HttpSendRequest(): https://msdn.microsoft.com/en-us/library/windows/desktop/aa384247(v=vs.85).aspx
InternetOpen(): https://msdn.microsoft.com/en-us/library/windows/desktop/aa385096(v=vs.85).aspx
InternetConnect(): https://msdn.microsoft.com/en-us/library/windows/desktop/aa384363(v=vs.85).aspx

Thanks,
~Hom
Last edited on
Ohai,

WinApi is fun stuff! My first bet would be your initialization of one of your many objects failed. So step 1.

check if the following objects ==NULL

c ws h

please consider using more descriptive variables :)

step 2.

"services.aonaware/.com"


I'm betting this isn't what you intend to have here.

step 3.

Happy dance.
Thanks!
Apparently, the URL with a slash through it was probably the error, as no errors were raised. (Shame on me as I'm primarily a web programmer xD)

I've added the NULL-checking to be safe.

Much appreciated!

~Hom
Topic archived. No new replies allowed.