Unable to send POST request via CHttpFile - C++/MFC - c++

My Goal: I'm trying to submit a post request
Issue: The post request is not being sent or is not formatted correctly or missing some information. I'm not sure about the exact problem.
What is a Successful Post Request: A successful post request to the website will change the user's Country location, this is part of the website feature.
Current Program Output: I get a 200 -OK response and No errors
Additional information: In my code below, I am making 2 requests.
From the first request, i receive and read an html page to find the RequestDigest value. This value is needed to perform a post request to a SharePoint website.
For the second request, I use the RequestDigest Value found and add it in post request and change the user's country to Canada ("CA").
Note: I'm new to C++ / MFC and I have been stuck on the problem for over a week! any help and/or guidance will be greatly appreciated
void CUserPassDiag::Connect(){
CInternetSession session(_T("My Session"));
CHttpConnection* pServer = NULL;
CHttpFile* pFile = NULL;
char *szBuff = new char[100000];
try
{
// First Request to retrieve the Digest value
CString strServerName = _T("boration.ptt.ca");
CString headers = _T("Content-Type: application/x-www-form-urlencoded\r\n");
headers += _T("Host: boration.ptt.ca\r\n");
headers += _T("Method: POST\r\n");
headers += _T("Pragma: no-cache\r\n");
headers += _T("Accept: application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, *//*\r\n");
headers += _T("UserAgent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET CLR 1.1.4322; .NET4.0C; .NET4.0E; InfoPath.3\r\n");
headers += _T("Keep-Alive: true\r\n");
headers += _T("Accept-Language: en-CA\r\n");\
headers += _T("Referer: http://boration.ptt.ca/team/physicallocation/_Layouts/Ptt.PhysicalLocationWidget/Page.aspx\r\n");
// Headers Ready
CString szHeaders = _T(headers);
DWORD dwRet;
// URL
CString strObject = _T("/team/D135/SitePages/PttHome.aspx");
// get connection with username and password
pServer = session.GetHttpConnection(strServerName, INTERNET_DEFAULT_HTTP_PORT, _T("user"), _T("pass"));
// open request
pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST, strObject);
pFile->AddRequestHeaders(szHeaders);
pFile->SendRequest();
pFile->QueryInfoStatusCode(dwRet); // response is 200 OK
// Data is retrieve and can be queried for digest value
pFile->Read(szBuff,100000);
CString digest = CString(szBuff);
std::string str = digest.GetString();
std::smatch match;
std::regex re("0x(\\d|[A-Z]).*0000");
if (std::regex_search(str, match, re)){
digest = CString(std::string(match[0]).c_str());
}
// close the server and chttpfile
pServer->Close();
pFile->Close();
/*
* Second Request Preperation
*/
// Post data - CA stands for Canada and this value is added to the post reuquest to change the users country
CString country = "CA";
// add a new header
CString xRequestDigest = "X-RequestDigest: " + digest + _T("\r\n");
headers += xRequestDigest;
// formatting the digest value to be placed in the post request
digest.Replace(_T(" "),_T("+"));
digest.Replace(_T(":"),_T("%3A"));
digest.Replace(_T(","),_T("%2C"));
// new URL
strObject = _T("/team/physicallocation/_Layouts/Ptt.PhysicalLocationWidget/Page.aspx");
// post data to be sent
CString strFormData = _T("__REQUESTDIGEST=" + digest + "&__VIEWSTATE=%2FwEPDwUKLTM0MDQ5MzI3MQ9kFgJmD2QWAgIBDxYCHgVjbGFzcwUtbXMtZGlhbG9nIG1zLWRpYWxvZy1uciBtcy1kaWFsb2cgbXMtZGlhbG9nLW5yFgICAw9kFg4CEQ9kFgRmD2QWBAIBD2QWAgIBDxYCHgdWaXNpYmxlaBYCZg9kFgQCAg9kFgQCAQ8WAh8BaGQCAw8WCB4TQ2xpZW50T25DbGlja1NjcmlwdAWPAWphdmFTY3JpcHQ6Q29yZUludm9rZSgnVGFrZU9mZmxpbmVUb0NsaWVudFJlYWwnLDEsIDEsICdodHRwOlx1MDAyZlx1MDAyZnhjb2xsYWJvcmF0aW9uLnB3Yy5jYVx1MDAyZnRlYW1cdTAwMmZwaHlzaWNhbGxvY2F0aW9uJywgLTEsIC0xLCAnJywgJycpHhhDbGllbnRPbkNsaWNrTmF2aWdhdGVVcmxkHihDbGllbnRPbkNsaWNrU2NyaXB0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haW5JUC1pbnRlcm5hbC5wd2MuY2FkZAIIDw8WAh8MBeUBVG8gZGV0ZXJtaW5lIGlmIHRlY2huaWNhbCBkYXRhIGlzIGFjY2Vzc2libGUsIHlvdXIgY3VycmVudCBwaHlzaWNhbCBsb2NhdGlvbiBtdXN0IGJlIHByb3ZpZGVkLiA8YnIvPjxici8%2BDQpBbGwgdmFsdWVzIHByb3ZpZGVkIGFyZSBsb2dnZWQgYW5kIHJldGFpbmVkIGZvciBhdWRpdGluZyBwdXJwb3Nlcy48YnIvPjxici8%2BTWFrZSBzdXJlIHRvIHVwZGF0ZSBsb2NhdGlvbiB3aGVuIHRyYXZlbGxpbmcuIGRkAgoPDxYCHwwFB0NvbmZpcm1kZAILDw8WAh8MBQZDYW5jZWxkZAIFD2QWBAIBDw8WAh8MBQdXYXJuaW5nZGQCBQ8PFgIfDAUFQ2xvc2VkZGQShQ6UJsf5SBoFkPM2ZpdvDrHQ0w%3D%3D&__EVENTVALIDATION=%2FwEWBwKO0pUjAqmflsILAs2u9bkDAr36nu0CAo3uhpUDAubLxrYNAtT449AGHxcQwoL9xE%2FsJbw1%2BBKH5DPplNg%3D&ctl00%24PlaceHolderSearchArea%24ctl01%24ctl00=http%3A%2F%2Fxcollaboration.pwc.ca%2Fteam%2Fphysicallocation&InputKeywords=Search+this+site...&ctl00%24PlaceHolderSearchArea%24ctl01%24ctl04=0&countries="+country+"&ctl00%24PlaceHolderMain%24inpHide="+country+"&ctl00%24PlaceHolderMain%24btnSave=Confirm&__spText1=&__spText2=");
// Open connection
pServer = session.GetHttpConnection(strServerName, INTERNET_DEFAULT_HTTP_PORT, _T("user"), _T("pass"));
pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST, strObject);
// add 'content-length' header
CString contentLength;
contentLength.Format("Content-Length: %d",strFormData.GetLength());
contentLength+= "\r\n";
headers += _T(contentLength);
szHeaders = _T(headers);
// add the header
pFile->AddRequestHeaders(szHeaders);
//make a new request
bool res = pFile->SendRequest(szHeaders,(LPVOID)(LPCSTR)strFormData ,strFormData.GetLength());
pFile->QueryInfoStatusCode(dwRet);
TRACE("\n%d\n",res);
}catch(CInternetException* e){
TCHAR sz[1024];
e->GetErrorMessage(sz,1024);
_tprintf_s(_T("ERROR! %s\n"), sz);
e->Delete();
}
session.Close();
}

Your code seems fine, make sure you are getting the digest value from the right address.

Code looks good but I'm missing a pFile->EndRequest(); -- but that shouldn't be the cause.
If I were you, I'd check the server responses i.e. have a look at the servers access log. You need to be more specific here so people can help. If you get a 200 response, it might be a server problem as well.

Related

parsing JSON data from URL with C++

I have a MYSQL database linked to a webpage which, through some PHP code pushes pertinent data using JSON to a blank page. I am trying to grab this data using C++ and sockets and verifying a Key using the URL data.
I have a method of doing this using C# and a simple WebClient().DownloadString but I am having issues translating it to C++.
I have tried using some of the other libs mentioned around stackoverflow but haven't had the luck I was hoping for. Any help would be greatly appreciated.
char* SocketRequest_URL(char* URL, char* Key, int sign, char* Path = "")
{
char *begin = bufferReturn;
char *end = begin + sizeof(bufferReturn);
std::fill(begin, end, 0);
Host = gethostbyname(URL);
SocketAddress.sin_addr.s_addr = *((unsigned long*)Host->h_addr);
SocketAddress.sin_family = AF_INET;
SocketAddress.sin_port = SERVER_PORT;
Socket = socket(AF_INET, SOCK_STREAM, 0);
if (connect(Socket, (struct sockaddr *)&SocketAddress, sizeof(SocketAddress)) != CELL_OK) {
return "CONNECTION ERROR";
}
strcpy(RequestBuffer, "GET /");
strcat(RequestBuffer, Path);
char sign_Buf[15];
sprintf(sign_Buf, "%d", sign);
strcat(RequestBuffer, sign_Buf);
strcat(RequestBuffer, "/");
strcat(RequestBuffer, Key);
strcat(RequestBuffer, " HTTP/1.0\r\nHOST: ");
strcat(RequestBuffer, URL);
strcat(RequestBuffer, "\r\nUser-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");
strcat(RequestBuffer, "\r\n\r\n");
send(Socket, RequestBuffer, strlen(RequestBuffer), 0);
while (recv(Socket, bufferReturn, 1024, 0) > 0)
{
return bufferReturn;
}
}
Essentially, the page I am trying to pull data from has information pushed to it. I want to grab that data and save it. I can't even seem to get connected to the link.
I would not recommend to use socket API for HTTP/S interactions. Here is a list of the C/C++ libraries/stacks that may be used for that, for example:
WinInet
WinHttp
Qt
POCO
Boost.Asio
libcurl
Also, you should add if condition for socket API functions and check returning values which could give you a tip about what's happening in your code. And your code returns from the function after first successful call to the recv that is incorrect as you must expect as much calls to recv as needed, until it returns -1.

Send data using WinHttpClient and https c++

I have a c++ application that sending data using http with WinHttpClient
and i want to start sanding https
bool ret = false;
WinHttpClient client(wstring(url.begin(), url.end()));
ret = client.SetAdditionalDataToSend((BYTE *)data.c_str(), (unsigned int)data.length());
if (ret)
{
// Set request headers.
wchar_t szSize[50] = L"";
swprintf_s(szSize, L"%d", data.size());
wstring headers = L"Content-Length: ";
headers += szSize;
headers += L"\r\nContent-Type: application/x-www-form-urlencoded\r\n";
string apiVersion = ...;
headers += L"\r\nAccept-Version: " + wstring(apiVersion.begin(), apiVersion.end()) + L"\r\n";
ret = client.SetAdditionalRequestHeaders(headers);
client.SetUserAgent(L"..." + to_wstring(...));
}
// Send http post request.
if (ret) ret = client.SendHttpRequest(L"POST");
i have read that i need to add
client.SetRequireValidSslCertificates(false);
after i create the client, but when i tried noting change
so the question it how to send it using https?

Write full HTTP POST request packet

I need to write a full http request for invoke a SOAP service. I don't have libraries for soap request so i need to write full HTTP packet. this is how i've proceeded (i'm programming an arduino board):
String body = HttpRequestBody("33", "77%");
client.println("POST /dataserver HTTP/1.1");
client.println("Accept: text/xml, multipart/related");
client.println("Content-Type: text/xml; charset=utf-8");
client.println("SOAPAction: \"http://example.com/Functions/SendDataRequest\"");
client.println("User-Agent: Arduino WiFiShield");
client.println("Content-Length: "+body.length());
client.println("Host: arduino-data-server.appspot.com/dataserver");
client.println("Connection: Keep-Alive");
client.println();
client.println(body);
client represent connection to my webservice. This is HttpRequestBody function:
String HttpRequestBody(String v1, String v2) {
Serial.println("Generating xml message...");
String res = "";
res += "<?xml version=\"1.0\"?>\n\r";
res +="<S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\"\n\r";
res +="<S:Body>\n\r";
res +="<ns2:sendData xmlsn:ns2=\"http://example.com\">\n\r";
res +="<arg0>"+v1+"</arg0>\n\r";
res +="<arg1>"+v2+"</arg1>\n\r";
res +="</ns2:sendData>\n\r";
res +="</S:Body>\n\r";
res +="</S:Envelope>\n\r";
Serial.println(res);
return res;
}
But something gone wrong and i can't contact webserver. Webserver works and it's reacable, because if i change POST into GET, on webservice log, i see connection. How can i fix it?
In HttpRequestBody you are allocating: String res = ""; and then updating it. Finally, you return res. But, res was allocated on the stack of HttpRequestBody (??), you are NOT guaranteed that it will be there after HttpRequestbody terminates.
You probably need to do the C++ equivalent of malloc used in C code, in order to ensure that res is on the heap and does not get freed.

Custom 404 response for GWAN

When people request a servlet that is not found on csp folder
it will show a "404 Not Found" Response
Not Found
The requested URL was not found on this server.
is there a way we can check a servlet exists or not, to create custom 404 page?
Just like Gil said. You can use HDL_HTTP_ERRORS to intercept HTTP errors. To make it more clear here is a sample connection handler that replaces 404 error with a custom error message.
#include "gwan.h"
int init(int argc, char *argv[])
{
u32 *states = (u32*)get_env(argv, US_HANDLER_STATES);
*states = (1 << HDL_HTTP_ERRORS);
return 0;
}
int main(int argc, char *argv[])
{
if((long)argv[0] != HDL_HTTP_ERRORS)
return 255; // Continue if not an error
// get the HTTP reply code
int *status = (int*)get_env(argv, HTTP_CODE);
if(!status || *status != 404)
return 255; // Continue if not a 404 error
static char custom_err[] =
"<!DOCTYPE HTML><html><head><title>404 Not Found</title><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"><link href=\"/imgs/errors.css\" rel=\"stylesheet\" type=\"text/css\"></head>"
"<body><h1>Not Found</h1>"
"<p>This is a custom 404 not found error. What makes you think that this link exist?!!</p></body></html>";
static char header[] =
"HTTP/1.1 %s\r\n"
"Server: G-WAN\r\n"
"Date: %s\r\n"
"Content-Type: text/html; charset=UTF-8\r\n"
"Content-Length: %u\r\n\r\n";
int len = sizeof(custom_err)-1;
char *date = (char*)get_env(argv, SERVER_DATE);
// Set our http reply headers
build_headers(argv, header,
http_status(*status),
date, // current server date
len); // Reply length
// Set our reply using our custom error
set_reply(argv, custom_err, len, *status);
return 2; // End request then send reply
}
void clean(int argc, char *argv[]) {}
Take note if you are returning a 404 error from a servlet. Make sure you do a
xbuf_empty(get_reply(argv));
to empty the contents of the reply buffer. It will not reach HDL_HTTP_ERRORS if there are any content on the reply buffer. It will just reply with whatever the reply buffer has.
Both Content-Type and Connections handlers can check if the resource exists before serving it.
But the HDL_HTTP_ERRORS state of Connection Handlers lets you intercept HTTP errors to alter the default reply otherwise generated by G-WAN. It is defined in the documented G-WAN API Handler States.
That's most probably what you are looking for.

Uploading PNG File (HTTP POST) with C++ Winsock API

I'm trying to upload a PNG file through Winsock2 HTTP Post. Here's my request string:
boundary = "-----rueldotme";
request += "POST " + uri + " HTTP/1.1\r\n";
request += "Host: " + hostname + "\r\n";
request += "User-Agent: " + UserAgent + "\r\n";
request += "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n";
request += "Accept-Language: en-us,en;q=0.5\r\n";
request += "Accept-Encoding: gzip,deflate\r\n";
request += "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n";
request += "Keep-Alive: 115\r\n";
request += "Connection: keep-alive\r\n";
request += "Content-Length: " + fileSize + "\r\n";
request += "Content-Type: multipart/form-data, boundary=" + boundary + "\r\n";
request += "\r\n";
request += "--" + boundary + "\r\n";
request += "Content-Disposition: form-data; name=\"filename\"; filename=\"" + fileName + "\"\r\n";
request += "Content-Type: image/png; charset=binary\r\n";
request += "Content-Transfer-Encoding: binary\r\n";
request += "\r\n";
request += "%s\r\n";
request += "\r\n";
The connection is OK, no errors and such, the fileCon by the way is from ReadFile(). And there's no error code. The number of bytes read is the same as the output of GetFileSize(). I tried displaying the contents of fileCon but only gave me this:
Don't mind the title "Error" (I set it).
Also, the request takes ages to complete, and gives me a blank response. Yep, blank with no HTTP headers. Am I doing the request right? Should I really have to include the file contents at the POST data?
Thanks in advance.
EDIT: The PNG size is about 256KB. I'm in a 1mbps connection.
EDIT: Sorry if the information was insufficient. Anyway, here's what I did lately:
int flz;
char bdata[BSIZE];
DWORD dwe, bytesRead = 0;
HANDLE fh = CreateFile(fileName.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
LPVOID fbuff = NULL;
flz = GetFileSize(fh, NULL);
fbuff = malloc(flz);
ReadFile(fh, fbuff, flz, &bytesRead, NULL));
...
sprintf_s(bdata, BSIZE, request.c_str(), reinterpret_cast<char *>(fbuff)); //BSIZE = 1024
...
send(sock, bdata, std::strlen(bdata), 0);
Not enough information to solve the problem, so I'll give a meta-answer instead:
Use a packet sniffer (e.g., wireshark) to check exactly what data is actually being sent and received. This will let you verify that the request is as it should be, and that the "blank response" you're getting really is blank.
One wild stab in the dark:
You haven't included any variable declarations in your code snippet, so I don't know what type "fileCon" is, but don't forget that the PNG data is likely to contain null bytes, which will mess up a default conversion from a char* to a std::string.
Edit:
Your modification contains the same bug that the std::string based version had, namely, that the PNG data is likely to contain null bytes. Perhaps this code will explain more clearly:
const char* data = "Hello\0world."; // some data that contains a null byte
std::string dataStr(data);
std::cout << dataStr << "\n"; // will print "Hello".
std::cout << dataStr.size() << "\n"; // will print "5"
char buf[512];
sprintf_s(buf, sizeof(buf), "Data: %s\n", data);
std::cout << buf; // will print "Data: Hello"
Both the conversion to std::string and formatting with sprintf will interpret the null byte as being the end of the data, and so the rest of the original data ("world.") will never be used.