Click to See Complete Forum and Search --> : Send HTTP request.


Craig Gemmill
March 5th, 2003, 12:38 AM
Basically what I am looking for is the ability to send an http request to a specific address and dispose of the returned data.

Here is the situation. We are having a major remote database problem, so in the mean time we have come up with this workaround:

1. The c app send an http request to:
www.something.com?x=something&y=something&z=something

2. The webapp takes the variables passed in the address (x,y,z) and does it's thing.

So all the c app has to do is send the page request and kill the connection.

I am not the c developer, but he is having problems doing this, so I thought I would ask the good ol' codeguru community.

Thanks in advance for any help you can provide.

kuphryn
March 5th, 2003, 02:16 PM
Post the section of code the developer is having problems with.

Kuphryn

mwilliamson
March 5th, 2003, 03:53 PM
When in doubt check the RFC. There might be something on w3.org too.

Craig Gemmill
March 6th, 2003, 12:17 PM
I sent the developer over to w3.org and this is what he came up with for testing:

#include <windows.h>
#include <wininet.h>
#include <iostream>
using namespace std;


void main () {
HINTERNET hinternet, hHTTPSession, hHTTPRequest;

hinternet = InternetOpen("Request", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);

if (hinternet == NULL) {
cout << "InternetOpen() Failed - " << GetLastError() << endl;
return;
} else {
cout << "Internet session openend." << endl;
}

hHTTPSession = InternetConnect(hinternet, "www.something.com", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);

if (hHTTPSession == NULL) {
cout << "InternetConnect() Failed - " << GetLastError() << endl;
return;
} else {
cout << "Connected to site." << endl;
}

hHTTPRequest = HttpOpenRequest(hHTTPSession, "POST", "/formtest.aspx", NULL, NULL, NULL, 0, 0);

if (hHTTPRequest == NULL) {
cout << "HttpOpenRequest() Failed - " << GetLastError() << endl;
return;
} else {
cout << "Open Request." << endl;
}

TCHAR header[] = TEXT("Content-Type: application/x-www-form-urlencoded");
TCHAR data[] = TEXT("txtsomething1=something&txtsomething2=something");

if (HttpSendRequest(hHTTPRequest, header, strlen(header), data, strlen(data))) {
cout << "Send Request successful." << endl;
} else {
cout << "HttpSendRequest() Failed - " << GetLastError() << endl;
return;
}


InternetCloseHandle(hHTTPRequest);
InternetCloseHandle(hHTTPSession);
InternetCloseHandle(hinternet);

}


That will send the request as POST'ed data to a specific page.

Thanks again for getting us going in the right direction.