3,055
社区成员
发帖
与我相关
我的任务
分享
CString strHttpName="http://localhost/TestReg/RegForm.aspx"; // 需要提交数据的页面
CString strFormData = "username=abc&password=123"; // 需要提交的数据
CInternetSession sess;
CHttpFile* fileGet;
CString strHeaders = _T("Content-Type: application/x-www-form-urlencoded"); // 请求头
try
{
fileGet=(CHttpFile*)sess.OpenURL(strHttpName);//打开文件
}
catch(CException* e)
{
fileGet = 0;
throw;
}
CString strSentence, strGetSentence = "";
if(fileGet)
{
DWORD dwStatus;
DWORD dwBuffLen = sizeof(dwStatus);
BOOL bSuccess = fileGet->QueryInfo(HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &dwStatus, &dwBuffLen);
if( bSuccess && dwStatus>= 200 &&dwStatus<300 )
{
BOOL result = fileGet->SendRequest(strHeaders, (LPVOID)(LPCTSTR)strFormData, strFormData.GetLength());
while(fileGet->ReadString(strSentence)) // 读取提交数据后的返回结果
{
strGetSentence = strGetSentence + strSentence + char(13) + char(10);
}
AfxMessageBox(strGetSentence); // 显示返回网页内容
}
else
{
strSentence.Format("POST出错,错误码:%d", dwStatus);
AfxMessageBox(strSentence);
}
fileGet->Close();
delete fileGet;
}
else
AfxMessageBox("不能找到网页文件!");
sess.Close();[/quote
不好意思,这个post我知道,但我需求是上传文件,第三个参数是 file 类,这个我不知道怎么传
CString strHttpName="http://localhost/TestReg/RegForm.aspx"; // 需要提交数据的页面
CString strFormData = "username=abc&password=123"; // 需要提交的数据
CInternetSession sess;
CHttpFile* fileGet;
CString strHeaders = _T("Content-Type: application/x-www-form-urlencoded"); // 请求头
try
{
fileGet=(CHttpFile*)sess.OpenURL(strHttpName);//打开文件
}
catch(CException* e)
{
fileGet = 0;
throw;
}
CString strSentence, strGetSentence = "";
if(fileGet)
{
DWORD dwStatus;
DWORD dwBuffLen = sizeof(dwStatus);
BOOL bSuccess = fileGet->QueryInfo(HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &dwStatus, &dwBuffLen);
if( bSuccess && dwStatus>= 200 &&dwStatus<300 )
{
BOOL result = fileGet->SendRequest(strHeaders, (LPVOID)(LPCTSTR)strFormData, strFormData.GetLength());
while(fileGet->ReadString(strSentence)) // 读取提交数据后的返回结果
{
strGetSentence = strGetSentence + strSentence + char(13) + char(10);
}
AfxMessageBox(strGetSentence); // 显示返回网页内容
}
else
{
strSentence.Format("POST出错,错误码:%d", dwStatus);
AfxMessageBox(strSentence);
}
fileGet->Close();
delete fileGet;
}
else
AfxMessageBox("不能找到网页文件!");
sess.Close();