Rana
Rana

Reputation: 1755

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

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=%2FwEPDwUKLTM0MDQ5MzI3MQ9kFgJmD2QWAgIBDxYCHgVjbGFzcwUtbXMtZGlhbG9nIG1zLWRpYWxvZy1uciBtcy1kaWFsb2cgbXMtZGlhbG9nLW5yFgICAw9kFg4CEQ9kFgRmD2QWBAIBD2QWAgIBDxYCHgdWaXNpYmxlaBYCZg9kFgQCAg9kFgQCAQ8WAh8BaGQCAw8WCB4TQ2xpZW50T25DbGlja1NjcmlwdAWPAWphdmFTY3JpcHQ6Q29yZUludm9rZSgnVGFrZU9mZmxpbmVUb0NsaWVudFJlYWwnLDEsIDEsICdodHRwOlx1MDAyZlx1MDAyZnhjb2xsYWJvcmF0aW9uLnB3Yy5jYVx1MDAyZnRlYW1cdTAwMmZwaHlzaWNhbGxvY2F0aW9uJywgLTEsIC0x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Dw8WAh8MBQZEYXRlIDpkZAIFDw8WAh8MBQtIb3N0IE5hbWUgOmRkAgYPDxYCHwwFGUY1LU1haW5JUC1pbnRlcm5hbC5wd2MuY2FkZAIIDw8WAh8MBeUBVG8gZGV0ZXJtaW5lIGlmIHRlY2huaWNhbCBkYXRhIGlzIGFjY2Vzc2libGUsIHlvdXIgY3VycmVudCBwaHlzaWNhbCBsb2NhdGlvbiBtdXN0IGJlIHByb3ZpZGVkLiA8YnIvPjxici8%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();
}

Upvotes: 0

Views: 2908

Answers (2)

David.C
David.C

Reputation: 136

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

Upvotes: 1

thomiel
thomiel

Reputation: 2937

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.

Upvotes: 0

Related Questions