通过HTTP的POST方式,如何上传文件到服务器? RT 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 post数据,boundary随即生成Content-type: multipart/form-data, boundary=AaB03x --AaB03x content-disposition: form-data; name="user" Wilson Peng --AaB03x content-disposition: form-data; name="myfile" Content-type: multipart/mixed, boundary=BbC04y --BbC04y Content-disposition: attachment; filename="myphoto.gif" Content-type: image/gif Content-Transfer-Encoding: binary ...myphoto.gif 内容略... --BbC04y-- --AaB03x-- http://search.csdn.net/Expert/topic/1084/1084909.xml?temp=.3063318 void CProject1Dlg::OnOK(void){ CString m_sServer; CString m_sPath; UpdateData(true); if(m_sUrl.IsEmpty()) return; CString stmp; stmp=m_sUrl;//m_sUrl is a edit box to input URL;//you can use http://www.csdn.net/expert/topic/55011.shtm or //http://www.aaa.com/test.asp?id=xxx stmp.MakeUpper(); if(stmp.Left(7)!="HTTP://") { AfxMessageBox("wrong url header"); return ; } else stmp=m_sUrl.Mid(7); int nSlashPos=stmp.Find("/"); if(nSlashPos!=-1) { m_sServer=stmp.Left(nSlashPos); m_sPath=stmp.Mid(nSlashPos); stmp= m_sServer+" \n"+m_sPath; AfxMessageBox(stmp); } else { AfxMessageBox("/ not find"); return; } HINTERNET hSession=::InternetOpen("raw html reader",PRE_CONFIG_INTERNET_ACCESS,"",INTERNET_INVALID_PORT_NUMBER,0); if(hSession==NULL) { AfxMessageBox("Internetopen failed"); return; } HINTERNET hConnect=::InternetConnect(hSession,m_sServer,INTERNET_INVALID_PORT_NUMBER,"","",INTERNET_SERVICE_HTTP,0,0); if(hConnect==NULL) { AfxMessageBox("InternetConnect failed"); ::InternetCloseHandle(hSession); return; } HINTERNET hHttpFile=::HttpOpenRequest(hConnect,"GET",m_sPath,HTTP_VERSION,NULL,0,INTERNET_FLAG_DONT_CACHE,0); if(hHttpFile==NULL) { AfxMessageBox("can not call HttpOpenRequest"); ::InternetCloseHandle(hConnect); ::InternetCloseHandle(hSession); return; } CWaitCursor wait; BOOL bSend=::HttpSendRequest(hHttpFile,NULL,0,0,0); if(bSend) { char cQueryBuf[16]; DWORD dwFileSize; DWORD dwQueryBufLen=sizeof(cQueryBuf); BOOL bQuery=::HttpQueryInfo(hHttpFile,HTTP_QUERY_CONTENT_LENGTH,cQueryBuf,&dwQueryBufLen,NULL); if(bQuery) { dwFileSize=(DWORD)atol(cQueryBuf); } else dwFileSize=10*1024; char *lpszBuf=new char[dwFileSize+1]; DWORD dwBytesRead; BOOL bRead=::InternetReadFile(hHttpFile,lpszBuf,dwFileSize+1,&dwBytesRead); m_sContent=(LPCTSTR)lpszBuf; delete lpszBuf; } UpdateData(false);} 谢谢大家的回复!!!vcmute(横秋):可以给一个例子吗?xuzheng318(forever C++)(忧郁王子):我需要上传文件,而不是POST一个http请求这么简单。在服务器端,有一个页面负责接收。请高手多多帮忙!! Q177188 Using HttpSendRequestEx for Large POST Requests rfc1867,有关http form的详细说明接收到的数据,注意要判断是否为chunk编码 利用HTTP方式上传http://www.xiaozhou.net/cooldog/blogview.asp?logID=57 我的BLOG收录了一篇文章。看上面的链接。。 kugou123(酷狗):编译你在blog提供的代码,出现下列问题:nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __endthreadexnafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __beginthreadex #include <process.h> 试试 kugou123(酷狗):正解,我刚刚试过,把#include <process.h>加上后就没问题了 在OnTimer中设置CStatic文本很慢的问题,那个大神帮忙看下 访问异常 求解决 请教在对话框上显示图像的问题 MFC中十进制和十六进制互换 非常非常难的问题??? 异步、同步的含义? 请问这个消息是干什么用的WM_COMMNOTIFY? 如何改变窗口滚动条的颜色或把滚动条设为自定的图形 顶锅盖文问题[弱] 高分!关于Dialog的大小 CListBox控件的疑问,追求完美的朋友请看 高分求救一个CSocket.OnReceive的问题
content-disposition: form-data; name="user" Wilson Peng
--AaB03x
content-disposition: form-data; name="myfile"
Content-type: multipart/mixed, boundary=BbC04y --BbC04y
Content-disposition: attachment; filename="myphoto.gif"
Content-type: image/gif
Content-Transfer-Encoding: binary ...myphoto.gif 内容略...
--BbC04y--
--AaB03x--
{
CString m_sServer;
CString m_sPath;
UpdateData(true);
if(m_sUrl.IsEmpty())
return;
CString stmp;
stmp=m_sUrl;
//m_sUrl is a edit box to input URL;
//you can use http://www.csdn.net/expert/topic/55011.shtm or
//http://www.aaa.com/test.asp?id=xxx
stmp.MakeUpper();
if(stmp.Left(7)!="HTTP://")
{
AfxMessageBox("wrong url header");
return ;
}
else
stmp=m_sUrl.Mid(7);
int nSlashPos=stmp.Find("/");
if(nSlashPos!=-1)
{
m_sServer=stmp.Left(nSlashPos);
m_sPath=stmp.Mid(nSlashPos);
stmp= m_sServer+" \n"+m_sPath;
AfxMessageBox(stmp);
}
else
{
AfxMessageBox("/ not find");
return;
}
HINTERNET hSession=::InternetOpen("raw html reader",PRE_CONFIG_INTERNET_ACCESS,"",INTERNET_INVALID_PORT_NUMBER,0);
if(hSession==NULL)
{
AfxMessageBox("Internetopen failed");
return;
}
HINTERNET hConnect=::InternetConnect(hSession,m_sServer,INTERNET_INVALID_PORT_NUMBER,"","",INTERNET_SERVICE_HTTP,0,0);
if(hConnect==NULL)
{
AfxMessageBox("InternetConnect failed");
::InternetCloseHandle(hSession);
return;
}
HINTERNET hHttpFile=::HttpOpenRequest(hConnect,"GET",m_sPath,HTTP_VERSION,NULL,0,INTERNET_FLAG_DONT_CACHE,0);
if(hHttpFile==NULL)
{
AfxMessageBox("can not call HttpOpenRequest");
::InternetCloseHandle(hConnect);
::InternetCloseHandle(hSession);
return;
}
CWaitCursor wait;
BOOL bSend=::HttpSendRequest(hHttpFile,NULL,0,0,0);
if(bSend)
{
char cQueryBuf[16];
DWORD dwFileSize;
DWORD dwQueryBufLen=sizeof(cQueryBuf);
BOOL bQuery=::HttpQueryInfo(hHttpFile,HTTP_QUERY_CONTENT_LENGTH,cQueryBuf,&dwQueryBufLen,NULL);
if(bQuery)
{
dwFileSize=(DWORD)atol(cQueryBuf);
}
else
dwFileSize=10*1024;
char *lpszBuf=new char[dwFileSize+1];
DWORD dwBytesRead;
BOOL bRead=::InternetReadFile(hHttpFile,lpszBuf,dwFileSize+1,&dwBytesRead);
m_sContent=(LPCTSTR)lpszBuf;
delete lpszBuf;
}
UpdateData(false);}
接收到的数据,注意要判断是否为chunk编码
nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __beginthreadex
正解,我刚刚试过,把#include <process.h>加上后就没问题了