Thread: Asyncronous WinHttp

  1. #1
    Bit Fiddler
    Join Date
    Sep 2009
    Posts
    79

    Asyncronous WinHttp

    Hi all.

    I'm not used with windows programming at all to begin with. I'm more used with curl. However. I got to use Windows API this time, and I'm trying to get a hang of WinHttp but obviously get something wrong.

    I wrote a little piece of code to go through the logic I need, but it segfaults. Can someone be so nice and point out where I'm going wrong?

    Code:
    #include <stdio.h>
    #include <assert.h>
    #include <stdlib.h>
    
    #include <windows.h>
    #include <winhttp.h>
    
    void http_callback(HINTERNET sess, DWORD_PTR ctx, DWORD sts, LPVOID info, DWORD infosz) {
    
    	printf("Status: 0x%08x\n", sts);
    	fflush(stdout);
    
    }
    int main(int argc, char **argv) {
    
    	int r;
    	HINTERNET sess;
    	HINTERNET conn;
    	HINTERNET requ;
    	
    	sess = WinHttpOpen(
    		L"winhttp-test",
    		WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
    		WINHTTP_NO_PROXY_NAME,
    		WINHTTP_NO_PROXY_BYPASS,
    		WINHTTP_FLAG_ASYNC);
    	assert(sess);
    
    	r = (int) WinHttpSetStatusCallback(
    		sess,
    		(WINHTTP_STATUS_CALLBACK) http_callback,
    		WINHTTP_CALLBACK_FLAG_ALL_NOTIFICATIONS,
    		0);
    	assert(r != -1);
    
    	conn = WinHttpConnect(
    		sess,
    		L"www.google.com",
    		INTERNET_DEFAULT_HTTPS_PORT,
    		0);
    	assert(conn);
    
    	requ = WinHttpOpenRequest(
    		conn,
    		L"GET",
    		L"/index.html",
    		NULL,
    		WINHTTP_NO_REFERER,
    		WINHTTP_DEFAULT_ACCEPT_TYPES,
    		WINHTTP_FLAG_SECURE);
    	assert(requ);
    	
    	r = WinHttpSendRequest(
    		requ,
    		WINHTTP_NO_ADDITIONAL_HEADERS,
    		0,
    		NULL,
    		0,
    		0,
    		0);
    
    	assert(r);
    
    	printf("Request sent!\n");
    
    	Sleep(10000);
    
    	return EXIT_SUCCESS;
    
    }
    The output of this program simply gets...
    Code:
    Status: 0x00000400
    ...and then it crashes.

    Thanks.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    3,519
    Did you compiler it with NDEBUG defined or not defined?

    If defined, try not defining it!

    The assert statements are removed from created code if NDEBUG is defined.
    assert - cppreference.com

    Tim S.
    Last edited by stahta01; 12-29-2018 at 10:09 PM. Reason: Add link
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. winHTTP.h
    By Hobbes80 in forum C++ Programming
    Replies: 7
    Last Post: 05-13-2008, 08:39 AM
  2. WinHttp problems
    By bgrn in forum Windows Programming
    Replies: 7
    Last Post: 02-03-2006, 07:29 AM
  3. winHTTP : spotting a 404 (and other server errors)
    By reanimated in forum Windows Programming
    Replies: 1
    Last Post: 05-28-2004, 05:58 PM

Tags for this Thread