文件通过http的post方法上传到服务器?

xiaoyan21 2003-07-14 02:10:49
现在我写了一个notes登记的表单?在登记表单中有“标题”、“正文”域,和一个附件上传控件。
我用vc++中的winiet类编写http自动访问这个表单,让它自动提交。
现在“标题”、“正文”域的数据能够提交到服务器中,可文件总是提交不成功?
//-----------------------------//
CString line,ls_url;
CInternetSession lc_isession;
CHttpConnection *pcon = lc_isession.GetHttpConnection("192.168.0.27",80,"admin","password");
ls_url = "/oa/test.nsf/DataInput?OpenForm&dummy=XXBA-5PDA2G";
CString FormDataType = _T("Content-Type: application/x-www-form-urlencoded");

//标题域的数据
CString FormData = _T("Share_Title=test");
//上传文件的数据
FormData +=_T("&%%File9239e669cd235be148256d55000e2a0a.$Body.0.750C;filename=\"f:\test.txt\"..Content-Type: text/plain=sss");
//提交
FormData+=_T("&__Click=0");
CHttpFile *http = pcon->OpenRequest ("POST",ls_url);
http->SendRequest( FormDataType, (LPVOID)(LPCTSTR)FormData, FormData.GetLength() );
DWORD dwRet;
dwRet=100;
http->QueryInfoStatusCode(dwRet);
CString dd;
dd.Format("%d",dwRet);
AfxMessageBox(dd);
http->Close();
lc_isession.Close( );
//-----------------------------------//
在上面程序把FormData +=_T("&%%File9239e669cd235be148256d55000e2a0a.$Body.0.750C;filename=\"f:\test.txt\"..Content-Type: text/plain=sss");
这段代码去掉,标题域的数据能过提交成功。
现在困难的是文件的数据怎么组织?//
望各位能讨论一下:
...全文
659 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
brotherfromaq 2004-01-01
  • 打赏
  • 举报
回复
关注
xiaoyan21 2003-07-21
  • 打赏
  • 举报
回复
上面的你好!
能不能把你的程序发个e_mail给我!
cwd@jinwang.com.cn
谢谢
kingbird 2003-07-16
  • 打赏
  • 举报
回复
CString CSoundCtrl::GetHead()
{
CString sHead("--");
sHead += m_strBoundary;
sHead += "\r\nContent-Disposition: form-data; name=\"LocalFileName\" filename=\"";
sHead += m_strFileName;
sHead += "\"\r\nContent-Type: text/html\r\n\r\n";

return sHead;
}

CString CSoundCtrl::GetTail()
{
CString sTail("\r\n");
sTail += "--";
sTail += m_strBoundary;
sTail += "\r\nContent-Disposition: form-data; name=\"SID\"";
sTail += "\r\n\r\n";
sTail += m_strSaveAsFile;
sTail += "\r\n--";
sTail += m_strBoundary;
sTail += "--\r\n";

return sTail;
}

bool CSoundCtrl::Upload(HINTERNET hRequest)
{
CString sHeader("Content-Type: multipart/form-data, boundary=");
m_strBoundary = GetBoundary();
sHeader += m_strBoundary;
sHeader += "\r\n";

DWORD dwError = 0;

if(HttpAddRequestHeaders(hRequest, (const char*)sHeader, -1, 0))
{
CString sHead = GetHead();
CString sTail = GetTail();
HANDLE hFile, hMap;
unsigned long nLen;
const void* pMap = GetData(hFile, hMap, nLen);

if(pMap != NULL)
{
INTERNET_BUFFERS InBuf;

ZeroMemory(&InBuf, sizeof(INTERNET_BUFFERS));
InBuf.dwStructSize = sizeof(INTERNET_BUFFERS);
InBuf.dwBufferTotal = sHead.GetLength() + nLen + sTail.GetLength();

again:
if(HttpSendRequestEx(hRequest, &InBuf, NULL, HSR_INITIATE, 0))
{
unsigned long ulWrote;
BOOL bWrote;

bWrote = InternetWriteFile(hRequest, (const void*)(const char*)sHead, sHead.GetLength(), &ulWrote);
bWrote &= InternetWriteFile(hRequest, pMap, nLen, &ulWrote);
bWrote &= InternetWriteFile(hRequest, (const void*)(const char*)sTail, sTail.GetLength(), &ulWrote);
bWrote &= HttpEndRequest(hRequest, NULL, HSR_INITIATE, 0);
if(!bWrote)
{
dwError = GetLastError();
{
if(ERROR_INTERNET_FORCE_RETRY == dwError)
goto again;
else
{
m_strErrorMsg = "HttpEndRequest Failed";
m_hResult = HRESULT_FROM_WIN32(dwError);
return false;
}
}
}
}
else
{
m_strErrorMsg = "HttpSendRequestEx Failed";
m_hResult = HRESULT_FROM_WIN32(GetLastError());
return false;
}

if(pMap != NULL)
UnmapViewOfFile(pMap);
if(hMap != NULL)
CloseHandle(hMap);
if(hFile != NULL)
CloseHandle(hFile);
}
else
{
// a mapping error occured - GetData will have set error info
return false;
}
}
else
{
m_strErrorMsg = "HttpAddRequestHeaders Failed";
m_hResult = HRESULT_FROM_WIN32(GetLastError());
return false;
}

return true;
}
kingbird 2003-07-16
  • 打赏
  • 举报
回复
short CSoundCtrl::Post()
{

HINTERNET hOpen, hConnect, hReq;

DWORD dwFlags = INTERNET_FLAG_RELOAD |
INTERNET_FLAG_NO_CACHE_WRITE |
INTERNET_FLAG_KEEP_CONNECTION;

INTERNET_PORT dwPort;

TCHAR szAccept[] = "*/*";
LPSTR AcceptTypes[2] = {0};
AcceptTypes[0] = szAccept;

m_strServerResponse = "";

m_hSoundCtrlDlg.ChangeFace("Uploading..."); //display uploading

if (m_iMp3Encode == 1)
{
Mp3Encode(); //wav --> mp3
}

// This is useful if you want to step into the code from the VB client
//DebugBreak();

if (0 == m_strProxy.GetLength())
{
// No proxy was specified
if ( !(hOpen = InternetOpen( "POSTer", INTERNET_OPEN_TYPE_DIRECT, NULL, 0, 0) ) )
{
m_strErrorMsg = "InternetOpen Failed";
DWORD dwError = GetLastError();
m_hResult = HRESULT_FROM_WIN32(dwError);
goto cleanup;
}
}
else
{
// Use proxy server
if ( !(hOpen = InternetOpen ( "POSTer", INTERNET_OPEN_TYPE_PROXY, m_strProxy, NULL, 0) ) )
{
m_strErrorMsg = "InternetOpen Failed";
m_hResult = HRESULT_FROM_WIN32(GetLastError());
goto cleanup;
}
}

if (m_bSSL)
{
dwPort = INTERNET_DEFAULT_HTTPS_PORT;
dwFlags |= INTERNET_FLAG_SECURE;
}
else
{
dwPort = INTERNET_INVALID_PORT_NUMBER;
}

if ( !(hConnect = InternetConnect( hOpen, m_strHost, dwPort, "", "", INTERNET_SERVICE_HTTP, 0, 0) ))
{
m_strErrorMsg = "InternetConnect Failed";
m_hResult = HRESULT_FROM_WIN32(GetLastError());
goto cleanup;
}

// IE 4 is required in order to use INTERNET_OPTION_USERNAME and INTERNET_OPTION_PASSWORD
// if you can not use IE 4, see Q195650 - HOWTO: How to Handle Proxy Authorization with WinInet

if((0 != m_strUserName.GetLength()) && (0 != m_strPassword.GetLength()))
{
char szUBuff[80];
char szPBuff[80];

strcpy(szUBuff, m_strUserName);
strcpy(szPBuff, m_strPassword);

if(!InternetSetOption(hConnect, INTERNET_OPTION_USERNAME,
m_strUserName.AllocSysString() , m_strUserName.GetLength()) ||
!InternetSetOption(hConnect, INTERNET_OPTION_PASSWORD,
m_strPassword.AllocSysString() , m_strPassword.GetLength()))
{
m_strErrorMsg = "InternetSetOption Failed";
m_hResult = HRESULT_FROM_WIN32(GetLastError());
goto cleanup;
}

}

// establish authentication first to avoid sending the file, being denied then having to send the
// file again see: Q194700 - HOWTO: Using HttpSendRequestEx with Password Protected URLs.

if ( !(hReq = HttpOpenRequest (hConnect, "POST", m_strAcceptor, HTTP_VERSION, "",
(LPCTSTR*) AcceptTypes, dwFlags ,0 ) ))
{
m_strErrorMsg = "HttpOpenRequest Failed";
m_hResult = HRESULT_FROM_WIN32(GetLastError());
goto cleanup;
}

if(!Authenticate(hReq))
{
goto cleanup;
}

InternetCloseHandle(hReq);
//

if ( !(hReq = HttpOpenRequest (hConnect, "POST", m_strAcceptor, HTTP_VERSION, "",
(LPCTSTR*) AcceptTypes, dwFlags ,0 ) ))
{
m_strErrorMsg = "HttpOpenRequest Failed";
m_hResult = HRESULT_FROM_WIN32(GetLastError());
goto cleanup;
}

if(!Upload(hReq))
{
goto cleanup;
}

char pcBuffer[513]; //Get servers return string
DWORD dwBytesRead;

do
{
dwBytesRead=0;
if(InternetReadFile(hReq, pcBuffer, 512, &dwBytesRead))
{
pcBuffer[dwBytesRead]=0x00; // Null-terminate buffer
m_strServerResponse += pcBuffer;
}
}while(dwBytesRead>0);

m_hSoundCtrlDlg.ChangeFace("Magic VoiceMail"); //display uploading

return 0;

cleanup:

if(hOpen) InternetCloseHandle(hOpen);
if(hConnect) InternetCloseHandle(hConnect);
if(hReq) InternetCloseHandle(hReq);

m_hSoundCtrlDlg.ChangeFace("Magic VoiceMail"); //display uploading

return -1;
}

bool CSoundCtrl::Authenticate(HINTERNET hRequest)
{
DWORD dwSize, dwCode;
CHAR szData[51];

if(!HttpSendRequest(hRequest, NULL, 0, NULL, 0))
{
m_strErrorMsg = "HttpSendRequest (Authentication) Failed";
m_hResult = HRESULT_FROM_WIN32(GetLastError());
return false;
}

dwSize = sizeof (DWORD) ;
if ( !HttpQueryInfo (hRequest, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &dwCode, &dwSize, NULL))
{
m_strErrorMsg = "HttpQueryInfo Failed";
m_hResult = HRESULT_FROM_WIN32(GetLastError());
return false;
}

do
{
// read all the data off the handle
InternetReadFile(hRequest, (LPVOID)szData, 50, &dwSize);
}
while (dwSize != 0);

if(dwCode != 200) // OK
{
m_strErrorMsg = "Authentication Failed";
m_hResult = HRESULT_FROM_WIN32(dwCode);
return false;
}

return true;
}

CString CSoundCtrl::GetBoundary()
{
GUID guid;
unsigned char* pBuffer;
CString sGuid;

UuidCreate(&guid);
UuidToString((GUID*)&guid, (unsigned char**)&pBuffer);
sGuid = (const char*)pBuffer;
RpcStringFree((unsigned char**)&pBuffer);
return sGuid;
}

const void * CSoundCtrl::GetData(HANDLE &hFile, HANDLE &hMap, unsigned long &nLength)
{
void* pBuffer = NULL;

hFile = CreateFile(m_strFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if(hFile != INVALID_HANDLE_VALUE)
{
BY_HANDLE_FILE_INFORMATION FileInfo;
GetFileInformationByHandle(hFile, &FileInfo);
nLength = FileInfo.nFileSizeLow;
hMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
if(hMap != NULL)
{
pBuffer = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0);
if(pBuffer != NULL)
return pBuffer;
else
{
CloseHandle(hMap);
CloseHandle(hFile);
hMap = NULL;
hFile = NULL;
m_strErrorMsg = "MapViewOfFile Failed";
m_hResult = HRESULT_FROM_WIN32(GetLastError());
}
}
else
{
CloseHandle(hFile);
hFile = NULL;
m_strErrorMsg = "CreateFileMapping Failed";
m_hResult = HRESULT_FROM_WIN32(GetLastError());
}
}
else
{
hFile = NULL;
m_strErrorMsg = "CreateFile Failed";
m_hResult = HRESULT_FROM_WIN32(GetLastError());
}

return pBuffer;
}

xiaoyan21 2003-07-15
  • 打赏
  • 举报
回复
我现在用网络监视器监视到文件上传的数据格式,但我不知道怎么组织?

1。文件发送数据格式:
-----------------------------7d3cb26bc0348..Content-Disposition: form-data; name="%%File9239e669cd235be148256d55000e2a0a.$Body.0.750C"; filename="F:\test.txt"..Content-Type: text/plain....sss..
2。一般域发送数据格式:
-----------------------------7d3cb26bc0348..Content-Disposition: form-data; name="Share_Title"....test..
我可以组织为Share_Title=test
---------------------------------------------
--------------------------------------------
大家能给我看一下文件发送的数据怎么组织吗?
xiaoyan21 2003-07-15
  • 打赏
  • 举报
回复
大家能给我看看吗?
我很急,这个问题我搞了几天了。
jvcit 2003-07-14
  • 打赏
  • 举报
回复
关注!!
xiaoyan21 2003-07-14
  • 打赏
  • 举报
回复
没人感兴趣吗?
wyjok 2003-07-14
  • 打赏
  • 举报
回复
感兴趣,顶一下!
xiaoyan21 2003-07-14
  • 打赏
  • 举报
回复
能不能给我写个例子!
fantiyu 2003-07-14
  • 打赏
  • 举报
回复
首先,你要组织好文件头部描述,最主要的内容是文件名,其他关于文件格式类型之类的可以略过
记住\r\n\r\n结尾和&%%开头
其次,把文件头部大小加上文件大小计算成一个http附加内容大小(post的内容)放入http头

content-length: xxxxxx
构造好http头,\r\n\r\n结尾

把文件头部描述作为末尾追加进去
然后开始传送文件数据

完成

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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