利用HTTP方式上传

334 views 十二月 27, 04 by Timothy

#include
#include
#include
#include
BOOL UseHttpSendReqEx(HINTERNET hRequest, DWORD dwPostSize,CString strLocalFile);
BOOL Upload(CString bstrLocalFile,CString bstrServerIP,CString strServerPort,CString bstrRemoteFile);
#define BUFFSIZE 500

void main( int argc, char **argv )
{

if (argc < 5)
{
printf("Usage: Bigpost \n”);
printf(“ is the local file to POST\n”);
printf(“ is the server’s IP to POST to\n”);
printf(“ is the server’s Port to POST to\n”);
printf(“ is the virtual path to POST to\n”);
exit(0);
}
Upload(argv[1],argv[2],argv[3],argv[4]);
}
BOOL UseHttpSendReqEx(HINTERNET hRequest, DWORD dwPostSize,CString strLocalFile)
{
DWORD dwRead;
BYTE* buffer;
printf(“Local file:%s\n”,strLocalFile);
FILE* fLocal;
if((fLocal=fopen(strLocalFile,”rb”))==NULL){
printf(“Can’t open the file:%s,maybe it doesn’t exist!\n”,strLocalFile);
return false;
}
fseek(fLocal,0L,SEEK_END);
dwRead=ftell(fLocal);
rewind(fLocal);
buffer=(BYTE *)malloc(dwRead);
if(!buffer){
printf(“not enough memory!\n”);
return false;
}
printf(“length of file:%d\n”,dwRead);
dwRead=fread(buffer,1,dwRead,fLocal);
dwPostSize=dwRead;

INTERNET_BUFFERS BufferIn;
DWORD dwBytesWritten;
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;
}
bRet=TRUE;
if(bRet=InternetWriteFile( hRequest, buffer, dwPostSize, &dwBytesWritten))
printf( “\r%d bytes sent.”, dwPostSize);
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;
}
fclose(fLocal);
free(buffer);
return TRUE;
}

BOOL Upload(CString strLocalFile,CString strServerIP,CString strServerPort,CString strRemoteFile){
DWORD dwPostSize=0;
int intServerPort=atoi(strServerPort);
HINTERNET hSession = InternetOpen( “HttpSendRequestEx”, INTERNET_OPEN_TYPE_PRECONFIG,
NULL, NULL, 0);
if(!hSession)
{
printf(“Failed to open session\n”);
return FALSE;
}
HINTERNET hConnect = InternetConnect(hSession, strServerIP, intServerPort,
NULL, NULL, INTERNET_SERVICE_HTTP,NULL, NULL);
if (!hConnect){
printf( “Failed to connect\n” );
return FALSE;
}else{
HINTERNET hRequest = HttpOpenRequest(hConnect, “PUT”, strRemoteFile,
NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
if (!hRequest){
printf( “Failed to open request handle\n” );
}else{
if(UseHttpSendReqEx(hRequest, dwPostSize,strLocalFile))
{
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]=0×00; // 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” );
return FALSE;
}
printf( “\nFinished.\n” );
return TRUE;
}

分享到:

声明: 此Blog中的文章和随笔仅代表作者在某一特定时间内的观点和结论,对其完全的正确不做任何担保或假设
本站文章均采用 知识共享署名-相同方式共享3.0 协议进行授权,除非注明,本站文章均为原创,转载请注明转自 Timothy's Space 并应以链接形式标明本文地址!

你可能也对下列文章感兴趣

  • 没有相关文章!

Add your comment

4 Responses to "利用HTTP方式上传"

  1. 黑乎乎 CHINA 说道:

    很好,谢谢了

  2. known CHINA 说道:

    HI cooldog :
    我用IIS做了一个简单的网页,IP:192.168.0.3 虚拟目录:/test
    不知道能不用你的代码:
    Upload("D:\\1.txt","192.168.0.3","80","/test");
    这样调用因该每错吧,可是为什么文件传不上去呢??
    是我IIS的问题??
    我通过抓包得到一些信息:
    PUT /test HTTP/1.1
    User-Agent: HttpSendRequestEx
    Host: 192.168.0.3
    Content-Length: 60
    Cache-Control: no-cache

    %windir%\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll //文件内容
    服务器返回:
    HTTP/1.1 405 Method Not Allowed
    Cache-Control: private
    Allow: GET, HEAD, OPTIONS, TRACE
    Content-Type: text/html; charset=utf-8
    Server: Microsoft-IIS/7.0
    X-Powered-By: ASP.NET
    Date: Fri, 07 Nov 2008 05:39:26 GMT
    Content-Length: 5402

    麻烦你给看看
    我QQ:277077675
    eMail:known-914@163.com
    期待你的解答

  3. cooldog CHINA 说道:

    @known:
    没有开放PUT权限?

  4. known CHINA 说道:

    我用的是IIS7不怎么回用
    应该再那里设置呢。
    我吧虚拟目录/test设置了完全控制权限
    也不成


Leave a Reply

 您已输入0

(Ctrl+Enter)