18,356
社区成员
发帖
与我相关
我的任务
分享
//以下是post
// CString strErrorMsg;
// CString strFileName(L"d:\\test.log");
CString strResponsePage(L"d:\\test.log");
strResponsePage += ".log";
int nResult = 5;
CString strErrorMsg;
CString strURL;
GetDlgItem(IDC_EDIT1)->GetWindowText(strURL);
CString strParamName;
GetDlgItem(IDC_EDIT2)->GetWindowText(strParamName);
CString strParamValue;
GetDlgItem(IDC_EDIT3)->GetWindowText(strParamValue);
CString strParamName1;
GetDlgItem(IDC_EDIT5)->GetWindowText(strParamName1);
CString strParamValue1;
GetDlgItem(IDC_EDIT6)->GetWindowText(strParamValue1);
CString strParamName2;
GetDlgItem(IDC_EDIT7)->GetWindowText(strParamName2);
CString strParamValue2;
GetDlgItem(IDC_EDIT8)->GetWindowText(strParamValue2);
CString strParam;
// if(!strParamName.IsEmpty() && !strParamValue.IsEmpty())
// {
// strParam = strParamName;
// strParam += "=";
// strParam += strParamValue;
// strParam += "&";
// strParam += strParamName1;
// strParam += "=";
// strParam += strParamValue1;
// strParam += "&";
// strParam += strParamName2;
// strParam += "=";
// strParam += strParamValue2;
// }
// else
// {
strParam = "idnumber=test";
/* }*/
if(!strURL.IsEmpty() && !strParam.IsEmpty())
{
int nResult = 5;
CInternetSession session(_T("HttpFilePOSTer"));
CHttpConnection* pServer = NULL;
CHttpFile* pFile = NULL;
TCHAR szBuff[1024];
DWORD dwRet;
CFile file;
// http://192.168.0.155:8080/UploadFile/servlet/strTest?name=123&birthday=20140827&address=456
DWORD dwServiceType;
CString strServer, strObject, strUsername, strPassword;
INTERNET_PORT nPort;
if (AfxParseURLEx(strURL, dwServiceType, strServer, strObject, nPort, strUsername, strPassword) == FALSE)
{
strErrorMsg = "地址错误";
nResult = 1;
return ;
}
try
{
pServer = session.GetHttpConnection(strServer, nPort);
CString objName(L"/translate_txt");
pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST, strObject);//_T("/uploadfilepj/newfile.txt"));
//pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST, objName);
TRACE(strObject);TRACE(_T("\n"));
CString szHeaders = L"Content-Type: application/x-www-form-urlencoded;";
pFile->AddRequestHeaders(szHeaders);
CString strHead(L"Accept: */*");
pFile->AddRequestHeaders(strHead);
BOOL res = pFile->SendRequest(szHeaders,szHeaders.GetLength(),(LPVOID)(LPCTSTR)strParam,strParam.GetLength());
//BOOL res = pFile->SendRequest(NULL,0,(LPVOID)(LPCTSTR)strParam,strParam.GetLength());
if(res == TRUE)
{
// AfxMessageBox()
}
else
{
return;
}
pFile->EndRequest();
pFile->QueryInfoStatusCode(dwRet);
if(dwRet == HTTP_STATUS_OK)
{
strErrorMsg = "提交成功";
AfxMessageBox(_T("oK,成功"));
}
else
{
strErrorMsg = "提交失败";
AfxMessageBox(_T("oK,失败"));
}
}
catch (CInternetException* pEx)
{
nResult = 1;
pEx->GetErrorMessage(szBuff, 1024);
strErrorMsg = szBuff;
}
if(pFile)
{
delete pFile;
pFile = NULL;
}
if(pServer)
{
delete pServer ;
pServer = NULL;
}
}