I'm a professional lawyer and an amateur programmer (mainly C++). On a daily basis, I must access the same website and search for tons of court cases (plain text). Recently, I came up with the idea of writing a simple C++ function to automatically submit the form data to the site, which would save me lots of work time. I've been learning WinInet over the past few days and, after many hours, I managed to send a simple GET request and retreive the main page source code, but I can't go any further than that. My objective is to send a POST request (I think) with the search parameters to the site and obtain as a response the source code of the real search result page.
What I managed to program so far is almost worthless, but at least it gives a glimpse of my current state.
#include <windows.h>
#include <wininet.h>
#include <string>
#pragma comment (lib, "Wininet.lib")
bool GetRequest() {
HINTERNET hSession = InternetOpen(L"Mozilla/5.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (!hSession) return false;
HINTERNET hConnect = InternetConnect(hSession, L"scon.stj.jus.br", INTERNET_INVALID_PORT_NUMBER, L"", L"", INTERNET_SERVICE_HTTP, 0, 0);
if (!hConnect) return false;
HINTERNET hRequest = HttpOpenRequest(hConnect, L"GET", L"/", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
if (!hRequest) return false;
if (!HttpSendRequest(hRequest, 0, 0, 0, 0)) return false;
//from here on, I am able to read the main page source code using InternetReadFile, but I can't submit any form data
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hSession);
return true;
}
EDIT:
I'm still having trouble trying to replicate a search request. Whenever I run the following code, I keep receiving the same webpage as a response. I think the values I'm passing with the GET request are wrong. I just copied the URL link generated by the browser when I perform a generic search, but the webform seems to have a different input list (I don't know if that's possible). Anyway, here's my current code:
#include <windows.h>
#include <wininet.h>
#include <string>
#pragma comment (lib, "Wininet.lib")
#define BUFFERSIZE 2048
bool GetRequest() {
std::wstring inputValues = L"/SCON/pesquisar.jsp?preConsultaPP=&pesquisaAmigavel=+fraude&acao=pesquisar&novaConsulta=true&i=1&b=ACOR&livre=fraude&filtroPorOrgao=&filtroPorMinistro=&filtroPorNota=&data=&operador=e&thesaurus=JURIDICO&p=true&tp=T&processo=&classe=&uf=&relator=&dtpb=&dtpb1=&dtpb2=&dtde=&dtde1=&dtde2=&orgao=&ementa=¬a=&ref=";
HINTERNET hSession = InternetOpen(L"Mozilla/5.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (!hSession) return false;
HINTERNET hConnect = InternetConnect(hSession, L"processo.stj.jus.br", INTERNET_INVALID_PORT_NUMBER, L"", L"", INTERNET_SERVICE_HTTP, 0, 0);
if (!hConnect) return false;
HINTERNET hGetRequest = HttpOpenRequest(hConnect, L"GET", inputValues.c_str(), NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
if (!hGetRequest) return false;
if (!HttpSendRequest(hGetRequest, NULL, 0, 0, 0)) return false;
char buffer[BUFFERSIZE + 1] = "";
DWORD dwBytesRead;
BOOL bRead;
FILE* file=NULL;
fopen_s(&file, "output.txt", "w");
if (!file) return false;
while (true) {
bRead = InternetReadFile(hGetRequest, buffer, BUFFERSIZE, &dwBytesRead);
if (dwBytesRead == 0) break;
if (bRead) buffer[dwBytesRead] = 0;
fwrite(buffer, sizeof(char), strlen(buffer), file);
}
InternetCloseHandle(hGetRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hSession);
return true;
}