CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    May 2008
    Posts
    38

    wininet through proxy

    Hey guys. I have a small problem trying to use the wininet API through a proxy. I appreciate there's plenty of threads more or less on this subject already but none seemed particularly relevant.

    Basically, I do get a 200 OK response but the actual html returned is (i think) just from the ISA server saying 'under construction'.
    Another thing is, even if I remove the username and password i still get the same response. i'd expect a different response from the proxy if they were wrong but that's not happening.

    Code:
    #include <stdio.h>
    #include <string>
    #include <windows.h>
    #include <wininet.h>
    
    HINTERNET g_hRoot;
    HINTERNET g_hNetSession;
    HINTERNET g_hHttpRequest;
    DWORD g_dwError;
    char g_buffer[512];
    char g_headersbuffer[512];
    int g_nBytesRead = 0;
    char pszProxyUsername[] = "Username";
    char pszProxyPassword[] = "Password";
    
    int main(int argc, char* pszArgs[])
    {
    	std::string HtmlResponse;
    	//get handle from internetopen()
    
    	g_hRoot = InternetOpen("test", INTERNET_OPEN_TYPE_PROXY, "http://proxy", "8080", 0);
    	if(g_hRoot == NULL)
    		printf("InternetOpen() returned NULL\n");
    		
    	//connect to our opened handle
    	g_hNetSession = InternetConnect(g_hRoot, "www.google.co.uk", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
    	if(g_hNetSession == NULL)
    		printf("InternetConnect() returned NULL\n");
    		
    	if(!InternetSetOption(g_hNetSession, INTERNET_OPTION_PROXY_USERNAME, (LPVOID)pszProxyUsername, strlen(pszProxyUsername)))
    	{
    		g_dwError = GetLastError();
    		printf("internetsetoption() failed: %i\n", (int)g_dwError);
    	}
    	if(!InternetSetOption(g_hNetSession, INTERNET_OPTION_PROXY_PASSWORD, (LPVOID)pszProxyPassword, strlen(pszProxyPassword)))
    	{
    		g_dwError = GetLastError();
    		printf("internetsetoption() failed: %i\n", (int)g_dwError);
    	}
    	
    	//open our http request
    	g_hHttpRequest = HttpOpenRequest(g_hNetSession, "GET", "", "HTTP/1.1", "", NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
    	if(g_hHttpRequest == NULL)
    		printf("HttpOpenRequest() returned NULL\n");
    	if(!HttpSendRequest(g_hHttpRequest, NULL, 0, NULL, 0))
    	{
    		g_dwError = GetLastError();
    		printf("HttpSendRequest() failed\nError code %d\n", (int)g_dwError);
    	}
    	//have sent request now ready to read response
    	DWORD dwLength = (DWORD)sizeof(g_headersbuffer);
    	if(!HttpQueryInfo(g_hHttpRequest, HTTP_QUERY_RAW_HEADERS_CRLF, g_headersbuffer, &dwLength, NULL))
    	{
    		g_dwError = GetLastError();
    		printf("\nhttpqueryinfo() failed: %i\n", (int)g_dwError);
    	}
    	
    	InternetReadFile(g_hHttpRequest, g_buffer, sizeof(g_buffer), (DWORD*)&g_nBytesRead);
    	while(g_nBytesRead > 0)
    	{
    		HtmlResponse.append(g_buffer, g_nBytesRead);
    		InternetReadFile(g_hHttpRequest, g_buffer, sizeof(g_buffer), (DWORD*)&g_nBytesRead);
    	}
    	printf("%s\n", HtmlResponse.c_str());
    	printf("\n%s\n", g_headersbuffer);
    	//all done, cleanup
    	InternetCloseHandle(g_hRoot);
    	InternetCloseHandle(g_hNetSession);
    	InternetCloseHandle(g_hHttpRequest);
    	return 0;
    }
    Sorry for the lengthy post, by the way this is compiled with mingw. No errors are thrown up but it just ain't giving the expected results.... any suggestions anyone?

  2. #2
    Join Date
    May 2008
    Posts
    38

    Re: wininet through proxy

    Bah the problem is me obviously.
    Code:
    	g_hRoot = InternetOpen("test", INTERNET_OPEN_TYPE_PROXY, "http://proxy", "8080", 0);
    Should be
    Code:
    	g_hRoot = InternetOpen("test", INTERNET_OPEN_TYPE_PROXY, "proxy:8080", "<local>", 0);
    Despite the fact I was staring at MSDN til my eyes hurt i still managed to miss that. It was only when I finally got a 400 Bad Request response that I tracked down where i was going wrong.... cheers anyways peeps...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured