我的目标:我试图提交帖子请求
问题:邮件请求未发送或格式不正确或缺少某些信息。我不确定确切的问题。
什么是成功的帖子请求:对网站的成功发布请求将更改用户Country location
,这是网站功能的一部分。
当前节目输出:我收到200 -OK
响应并且没有错误
其他信息:在我的代码中,我正在提出2个请求。
从第一个请求开始,我接收并阅读html页面以查找RequestDigest
值。执行对SharePoint网站的发布请求时需要此值。
对于第二个请求,我使用找到的RequestDigest
值并将其添加到帖子请求中,并将用户的国家/地区更改为加拿大(" CA")。
注意:我是C ++ / MFC的新手,我已经坚持了一个多星期了!任何帮助和/或指导将不胜感激
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=%2FwEPDwUKLTM0MDQ5MzI3MQ9kFgJmD2QWAgIBDxYCHgVjbGFzcwUtbXMtZGlhbG9nIG1zLWRpYWxvZy1uciBtcy1kaWFsb2cgbXMtZGlhbG9nLW5yFgICAw9kFg4CEQ9kFgRmD2QWBAIBD2QWAgIBDxYCHgdWaXNpYmxlaBYCZg9kFgQCAg9kFgQCAQ8WAh8BaGQCAw8WCB4TQ2xpZW50T25DbGlja1NjcmlwdAWPAWphdmFTY3JpcHQ6Q29yZUludm9rZSgnVGFrZU9mZmxpbmVUb0NsaWVudFJlYWwnLDEsIDEsICdodHRwOlx1MDAyZlx1MDAyZnhjb2xsYWJvcmF0aW9uLnB3Yy5jYVx1MDAyZnRlYW1cdTAwMmZwaHlzaWNhbGxvY2F0aW9uJywgLTEsIC0xLCAnJywgJycpHhhDbGllbnRPbkNsaWNrTmF2aWdhdGVVcmxkHihDbGllbnRPbkNsaWNr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FGUY1LU1haW5JUC1pbnRlcm5hbC5wd2MuY2FkZAIIDw8WAh8MBeUBVG8gZGV0ZXJtaW5lIGlmIHRlY2huaWNhbCBkYXRhIGlzIGFjY2Vzc2libGUsIHlvdXIgY3VycmVudCBwaHlzaWNhbCBsb2NhdGlvbiBtdXN0IGJlIHByb3ZpZGVkLiA8YnIvPjxici8%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();
}
答案 0 :(得分:1)
您的代码似乎没问题,请确保您从正确的地址获取摘要值。
答案 1 :(得分:0)
代码看起来不错,但我错过了pFile->EndRequest();
- 但这不应该是原因。
如果我是你,我会检查服务器响应,即查看服务器访问日志。你需要在这里更具体,以便人们可以提供帮助。如果你得到200响应,那么它也可能是服务器问题。