http post file问题

tuobana123 2009-01-15 04:05:03
请教,用c/c++语言,通过http协议的post方法,怎样上传文件到服务器呢?
用java实现的比较多,能用C/C++实现吗,该如何实现?
...全文
247 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
星羽 2009-01-15
  • 打赏
  • 举报
回复
http://download.csdn.net/source/316736
c++文件post上传服务器
星羽 2009-01-15
  • 打赏
  • 举报
回复

vc mfc

bool CTCSDlg::UpFileOfHttp(TCHAR* strSourceName,TCHAR* strDestName,TCHAR* _strFtpSite)
{
TCHAR* pName=new TCHAR[256];
wsprintf(pName,L"%s?filename=%s",m_strFtpSite,strDestName);
AfxOutInfoToFile("===上传文件名:",strSourceName);
AfxOutInfoToFile("===上传地址:",pName);
ASSERT(m_strFtpSite != "" && strSourceName != "");
BOOL bResult = FALSE;
DWORD dwType = 0;
CString strServer;
CString strObject;

INTERNET_PORT wPort = 80;
DWORD dwFileLength = 0;
char * pFileBuff = NULL;
CHttpConnection * pHC = NULL;
CHttpFile * pHF = NULL;
CInternetSession cis;

bResult = AfxParseURL(pName, dwType, strServer, strObject, wPort);

AfxOutInfoToFile(L"===解析的服务器:",strServer.GetBuffer(strServer.GetLength()));
AfxOutInfoToFile(L"===解析的地址:",strObject.GetBuffer(strObject.GetLength()));
//AfxOutInfoToFile("===解析的地址:",m_strFtpSite);
if(!bResult)
return false;
CFile file;
try
{
if(!file.Open(strSourceName, CFile::shareDenyNone | CFile::modeRead))
return false;
dwFileLength = file.GetLength();
if(dwFileLength <= 0)
return false;

pFileBuff = new char[dwFileLength];
memset(pFileBuff, 0, sizeof(char) * dwFileLength);
file.Read(pFileBuff, dwFileLength);
const int nTimeOut = 5000;
cis.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, nTimeOut); //联接超时设置

cis.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 1); //重试1次
pHC = cis.GetHttpConnection(strServer, wPort); //取得一个Http联接
pHF = pHC->OpenRequest(CHttpConnection::HTTP_VERB_POST, strObject);

if(!pHF->SendRequest(NULL, 0, pFileBuff, dwFileLength))
{
delete[]pFileBuff;
pFileBuff = NULL;
pHF->Close();
pHC->Close();
cis.Close();
AfxOutInfoToFile(L"上传中,SendRequest error\n");
return FALSE;
}

CString szError=L"0";
char szBuff[1024];
UINT nRead;
while (nRead = pHF->Read(szBuff,1023))
{
szError=CString(szBuff,nRead);
AfxOutInfoToFile(L"返回值:",szError.GetBuffer(szError.GetLength()));
}


/*
DWORD dwError, dwBufferLength;
TCHAR szError[256]={_T("0")};

while(pHF->Read(szError,256))
{
lstrcpy(szError,L"error");
AfxOutInfoToFile(L"返回的值 :",szError);
}
*/
//if(lstrcmp(_T("0"),szError)==0)
// {
// return false;
// }

DWORD dwStateCode = 0;
pHF->QueryInfoStatusCode(dwStateCode);
if(dwStateCode == HTTP_STATUS_OK)
bResult = TRUE;
else
{

AfxOutInfoToFile(L"上传中,服务器返回的代码为");
return false;
}

// AfxOutInfoToFile(L"上传中,服务器返回的代码为");
// AfxOutInfoToFile(L"上传中,服务器返回的代码为");
}

catch(...)
{
char sz[256] = "";
// pEx->GetErrorMessage(sz, 25);
CString str;
// str.Format("InternetException occur!\r\n%s", sz);
//AfxMessageBox(str);
// g_writelog.WriteLine("上传中,%s",str);

}

delete[]pFileBuff;
pFileBuff = NULL;
file.Close();
pHF->Close();
pHC->Close();
cis.Close();

}
nineforever 2009-01-15
  • 打赏
  • 举报
回复
http://www.codeproject.com/KB/IP/httpget-post.aspx
Learn-anything 2009-01-15
  • 打赏
  • 举报
回复
k

3,882

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧