http_verb_put问题

liuyong82 2003-06-28 01:00:19
请问,http_verb_put如何实现http文件上传啊?
...全文
94 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
liuyong82 2003-06-30
  • 打赏
  • 举报
回复
我们试了一下,不过好像不太行,这位高手能不能给一段上传文件到服务器的示例程序呢?
我是希望在客户端用http_verb_post方法向服务端upload.asp发送上传文件请求,通过upload.asp来实现功能。
内容如下:
upload.aspset upload = server.CreateObject("uploadfilecom.uploadfile")
upload.SaveTo("d:\temp")
set upload = nothing
客户端vc代码为:
CString strHeaders =
_T("Content-Type: application/x-www-form-urlencoded");
CString strFormData = _T("c:\a.doc");
CInternetSession session;
CHttpConnection* pConnection =
session.GetHttpConnection(_T("ServerNameHere"));
CHttpFile* pFile =
pConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST,
_T("upload.asp"));
BOOL result = pFile->SendRequest(strHeaders,
(LPVOID)(LPCTSTR)strFormData, strFormData.GetLength());
这段代码有问题吗?为什么我上传都没有反应呢?希望能得到这位大虾的帮助,我们的任务很紧
masterz 2003-06-28
  • 打赏
  • 举报
回复
替代实现方法
Q177188 Using HttpSendRequestEx for Large POST Requests

#include <Windows.h>
#include <WinINet.h>
#include <stdio.h>

BOOL UseHttpSendReqEx(HINTERNET hRequest, DWORD dwPostSize);
#define BUFFSIZE 500

void main( int argc, char **argv )
{
DWORD dwPostSize;

if (argc < 4)
{
printf("Usage: Bigpost <Size> <Server> <Path>\n");
printf("<Size> is the number of KB to POST\n");
printf("<Server> is the server to POST to\n");
printf("<Path> is the virtual path to POST to\n");
exit(0);
}

if ( ((dwPostSize = strtoul(argv[1],NULL,10)) == 0) || (dwPostSize >= 2047999) )
{
printf("%s is invalid size. Valid sizes are from 1 to 2047999\n", argv[1]);
exit(0);
}

printf( "Test of POSTing %luKB with WinInet\n", dwPostSize);

dwPostSize *= 1024; // Convert KB to bytes

HINTERNET hSession = InternetOpen( "HttpSendRequestEx", INTERNET_OPEN_TYPE_PRECONFIG,
NULL, NULL, 0);
if(!hSession)
{
printf("Failed to open session\n");
exit(0);
}


HINTERNET hConnect = InternetConnect(hSession, argv[2], INTERNET_DEFAULT_HTTP_PORT,
NULL, NULL, INTERNET_SERVICE_HTTP,NULL, NULL);
if (!hConnect)
printf( "Failed to connect\n" );
else
{
HINTERNET hRequest = HttpOpenRequest(hConnect, "POST", argv[3],
NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
if (!hRequest)
printf( "Failed to open request handle\n" );
else
{
if(UseHttpSendReqEx(hRequest, dwPostSize))
{
char pcBuffer[BUFFSIZE];
DWORD dwBytesRead;

printf("\nThe following was returned by the server:\n");
do
{ dwBytesRead=0;
if(InternetReadFile(hRequest, pcBuffer, BUFFSIZE-1, &dwBytesRead))
{
pcBuffer[dwBytesRead]=0x00; // Null-terminate buffer
printf("%s", pcBuffer);
}
else
printf("\nInternetReadFile failed");
}while(dwBytesRead>0);
printf("\n");
}
if (!InternetCloseHandle(hRequest))
printf( "Failed to close Request handle\n" );
}
if(!InternetCloseHandle(hConnect))
printf("Failed to close Connect handle\n");
}
if( InternetCloseHandle( hSession ) == FALSE )
printf( "Failed to close Session handle\n" );

printf( "\nFinished.\n" );
}



BOOL UseHttpSendReqEx(HINTERNET hRequest, DWORD dwPostSize)
{
INTERNET_BUFFERS BufferIn;
DWORD dwBytesWritten;
int n;
BYTE pBuffer[1024];
BOOL bRet;

BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS ); // Must be set or error will occur
BufferIn.Next = NULL;
BufferIn.lpcszHeader = NULL;
BufferIn.dwHeadersLength = 0;
BufferIn.dwHeadersTotal = 0;
BufferIn.lpvBuffer = NULL;
BufferIn.dwBufferLength = 0;
BufferIn.dwBufferTotal = dwPostSize; // This is the only member used other than dwStructSize
BufferIn.dwOffsetLow = 0;
BufferIn.dwOffsetHigh = 0;

if(!HttpSendRequestEx( hRequest, &BufferIn, NULL, 0, 0))
{
printf( "Error on HttpSendRequestEx %d\n",GetLastError() );
return FALSE;
}

FillMemory(pBuffer, 1024, 'D'); // Fill buffer with data

bRet=TRUE;
for(n=1; n<=(int)dwPostSize/1024 && bRet; n++)
{
if(bRet=InternetWriteFile( hRequest, pBuffer, 1024, &dwBytesWritten))
printf( "\r%d bytes sent.", n*1024);
}

if(!bRet)
{
printf( "\nError on InternetWriteFile %lu\n",GetLastError() );
return FALSE;
}

if(!HttpEndRequest(hRequest, NULL, 0, 0))
{
printf( "Error on HttpEndRequest %lu \n", GetLastError());
return FALSE;
}

return TRUE;
}
要在服务器端接受文件,参考: Q189651 Uploading a File to IIS Using a Browser

18,356

社区成员

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

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