Solved

Calling an HTTP server from MFC, using POST

Posted on 2009-04-07
3
1,055 Views
Last Modified: 2013-12-03
I have to modify existing code (shown below).
The existing code queries an HTTP server using a simple GET (Actual URL is not shown)
Then starts to download the response (First few lines of the download loop are shown below)
The new code must use a POST request, and otherwise do the same.
Where do I tell the server
1)This is a POST, not a GET request,
2)Where is the POSTed buffer supposed to show in the request?
3)Anything else about the HTTP headers matters if it's a POST, not a GET?, if so please tell.

...
	HINTERNET  hConnect = NULL;
	HINTERNET  hSession = NULL;
	BYTE *pBuffer;
	hSession = InternetOpen("SPLOOKUP",  INTERNET_OPEN_TYPE_PRECONFIG,NULL, NULL, 0);
	if(hSession == NULL ){
		delete pParam;
		return;
	}
	hConnect = InternetOpenUrl(hSession,  csLink ,NULL, 0, INTERNET_FLAG_RELOAD, 0);  
	if(hConnect == NULL ){
		InternetCloseHandle(hSession);
		delete pParam;
		return;
	}
 
	char szSizeBuffer[32];
	DWORD dwLengthSizeBuffer = sizeof(szSizeBuffer); 
	BOOL bQuery = ::HttpQueryInfo(hConnect,HTTP_QUERY_CONTENT_LENGTH, szSizeBuffer, &dwLengthSizeBuffer, NULL) ;
	DWORD dwFileSize=atol(szSizeBuffer);
	BOOL bRet = TRUE;
	BOOL bCanceled = FALSE;
	CString csInBuffer;
 
	static const iMAXSIZE = 50000;
 
	while(true){
		DWORD dwNumberOfBytesAvailable = 0;
		InternetQueryDataAvailable(hConnect, &dwNumberOfBytesAvailable, 0, 0);
		pBuffer = new BYTE[dwNumberOfBytesAvailable+1];
		DWORD dwBytesRead = 0;
		bRet = InternetReadFile(hConnect,(LPVOID)pBuffer,dwNumberOfBytesAvailable,  &dwBytesRead);  
...

Open in new window

0
Comment
Question by:yossikally
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 31

Expert Comment

by:Zoppo
ID: 24092089
Hi yossikally,

I'm not quite expert for this, but in MSDN I found no way to do this with InternetOpenUrl.

I think you'll have to use InternetConnect to initialize the connection after InternetOpen and then use HttpSendRequestEx, but unfortunateley up to now the only sample code I found seems to have a bug (http://www.codeguru.com/forum/showthread.php?p=1194917) - but I guess it shouldn't be to difficult to find other samples.

Hope that helps,

ZOPPO
0
 

Author Comment

by:yossikally
ID: 24092443
Thanks Zoppo

Aside from the error, I couln't quite understand from the example how and where I send the request headers along with the request.

0
 
LVL 2

Accepted Solution

by:
crystyce earned 500 total points
ID: 24094666
Maybe this example helps
	CString url_data = "I'm posting this by http";
 
	HINTERNET hInternetSession;
	hInternetSession = ::InternetOpen("Serv",INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0 );
 
	if (hInternetSession==NULL)
		return 0;
 
	HINTERNET hFile,hRequest;
	
	hFile = InternetConnect(hInternetSession,server,port,NULL,NULL,INTERNET_SERVICE_HTTP,0,(DWORD_PTR)&dwContext);
	if (hFile==NULL)
	{
		InternetCloseHandle(hInternetSession);
		return 0;
	}
 
	
	DWORD dwFlags=INTERNET_FLAG_NO_AUTH|INTERNET_FLAG_PRAGMA_NOCACHE|INTERNET_FLAG_RELOAD;
	
	hRequest = HttpOpenRequest(hFile,"POST","",NULL,NULL,NULL,dwFlags,(DWORD_PTR)&dwContext);
 
	if (hFile==NULL)
	{
		InternetCloseHandle(hFile);
		InternetCloseHandle(hInternetSession);
		return 0;
	}
 
	BOOL ret = HttpSendRequest(hRequest,NULL,0,(LPVOID)(LPCTSTR)url_data,url_data.GetLength());
	if (!ret)
	{
		InternetCloseHandle(hRequest);
		InternetCloseHandle(hFile);
		InternetCloseHandle(hInternetSession);
		return 0;
	}
	
	while (...)
	{
		status = InternetReadFile(hRequest,buf,buffer_size,&dwBytes);
	}

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SignTool Error with Windows 10 5 221
fizzArray2 challenge 1 103
Delphi Mdi application Child forms get behind control 7 315
Please explain the parts of these 2 LINQ expressions 3 87
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question