随笔 - 19  文章 - 3  评论 - 3  阅读 - 20709
  2013年7月18日

代码主要是为了说清楚boundary

void UploadFile(LPTSTR lpFilePath)
{
    HINTERNET hSession=0;
    HINTERNET hConnect=0;
    HINTERNET hRequest=0;
    
    DWORD dwNumberOfBytesWritten=0;
    DWORD dwBytesSend=0;

    
    INTERNET_BUFFERS BufferIn;

    DWORD dwFlag;

    LPCTSTR boundary=TEXT("-----------------------------67491722032265"); //随机字符串
    LPCSTR aboundary="-----------------------------67491722032265"; //ansi

    HANDLE hFile;
    hFile=CreateFile(lpFilePath,
        GENERIC_READ,
        FILE_SHARE_READ|FILE_SHARE_WRITE,
        0,
        OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL,
        0);

    DWORD dwFileSize=GetFileSize(hFile,0);

    
    TCHAR content_type[128];
    _stprintf_s(content_type,TEXT("Content-Type: multipart/form-data; boundary=%s"),boundary);
    LPTSTR referer=TEXT("Referer: http://127.0.0.1/upload/~upload");
    LPTSTR accept=TEXT("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    LPTSTR accept_lan=TEXT("Accept-Language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
    LPTSTR accept_encoding=TEXT("Accept-Encoding: gzip, deflate");
    LPTSTR user_agent=TEXT("User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0");


    hSession=InternetOpen(_T("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0"),
        INTERNET_OPEN_TYPE_PRECONFIG,
        0,
        INTERNET_INVALID_PORT_NUMBER,
        0);
    if (0==hSession)
    {

        return;
    }
        
    hConnect=InternetConnect(hSession,
        TEXT("172.16.1.142"),
        80,
        _T(""),
        _T(""),
        INTERNET_SERVICE_HTTP,
        0,
        0);
    if (0==hConnect)
    {

        InternetCloseHandle(hSession);
        return;
    }



    dwFlag=INTERNET_FLAG_KEEP_CONNECTION;

    hRequest=HttpOpenRequest(hConnect,
        _T("POST"),
        _T("/upload/"),
        HTTP_VERSION,
        0,                //Referrer
        0,                //AcceptTypes 
        dwFlag,
        0);
    if (0==hRequest)
    {

        InternetCloseHandle(hConnect);
        InternetCloseHandle(hSession);
        return;
    }

    HttpAddRequestHeaders(hRequest,content_type,-1,HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE);
    HttpAddRequestHeaders(hRequest,referer,-1,HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE);
    HttpAddRequestHeaders(hRequest,accept,-1,HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE);
    HttpAddRequestHeaders(hRequest,accept_lan,-1,HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE);
    HttpAddRequestHeaders(hRequest,accept_encoding,-1,HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE);

    BYTE* lpBuffer=(BYTE*)VirtualAlloc(0,dwFileSize,MEM_COMMIT,PAGE_READWRITE);
    if (0==lpBuffer)
    {
        InternetCloseHandle(hRequest);
        InternetCloseHandle(hConnect);
        InternetCloseHandle(hSession);
        return;
    }

    DWORD dwRead;
    ReadFile(hFile,lpBuffer,dwFileSize,&dwRead,0);

    CloseHandle(hFile);

    char first_boundary[128];
    char delimiter[128];
    char end_boundary[128];
    sprintf_s(first_boundary,"--%s\r\n",aboundary);
    sprintf_s(delimiter,"\r\n--%s\r\n",aboundary);
    sprintf_s(end_boundary,"\r\n--%s--\r\n",aboundary);

    LPSTR content_dispos="Content-Disposition: form-data; name=\"fileupload1\"; filename=\"notepad.exe\"\r\n";
    
    LPSTR content_type2="Content-Type: application/octet-stream\r\n\r\n";


    LPSTR rn="\r\n";

    BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS );
    BufferIn.Next = NULL; 
    BufferIn.lpcszHeader = NULL;
    BufferIn.dwHeadersLength = 0;
    BufferIn.dwHeadersTotal = 0;
    BufferIn.lpvBuffer = NULL;                
    BufferIn.dwBufferLength = 0;
    BufferIn.dwBufferTotal = dwFileSize
        +strlen(first_boundary)
        +strlen(content_dispos)
        +strlen(content_type2)
        +strlen(end_boundary); //Content-Length:
    BufferIn.dwOffsetLow = 0;
    BufferIn.dwOffsetHigh = 0;

    if (!HttpSendRequestEx(hRequest,&BufferIn,0,0,0))
    {
        InternetCloseHandle(hRequest);
        InternetCloseHandle(hConnect);
        InternetCloseHandle(hSession);
        return;
    }

    InternetWriteFile(hRequest,(byte*)first_boundary,strlen(first_boundary),&dwNumberOfBytesWritten); //first boundary
    InternetWriteFile(hRequest,(byte*)content_dispos,strlen(content_dispos),&dwNumberOfBytesWritten);
    InternetWriteFile(hRequest,(byte*)content_type2,strlen(content_type2),&dwNumberOfBytesWritten);
    InternetWriteFile(hRequest,lpBuffer,dwFileSize,&dwNumberOfBytesWritten);

    //如果还有其他文件
    //InternetWriteFile(hRequest,(byte*)delimiter,strlen(delimiter),&dwNumberOfBytesWritten); //deimiter
    //InternetWriteFile(hRequest,(byte*)content_dispos,strlen(content_dispos),&dwNumberOfBytesWritten);
    //InternetWriteFile(hRequest,(byte*)content_type2,strlen(content_type2),&dwNumberOfBytesWritten);
    //...

    InternetWriteFile(hRequest,(byte*)end_boundary,strlen(end_boundary),&dwNumberOfBytesWritten);//last boundary

    HttpEndRequest(hRequest,0,0,0);


    InternetCloseHandle(hRequest);
    InternetCloseHandle(hConnect);
    InternetCloseHandle(hSession);

    VirtualFree(lpBuffer,0,MEM_RELEASE);
}

关于boundary:

RFC2046

关于MINE Type:

MIME Type Detection in Windows Internet Explorer

Internet media type

附数据包截图

 

 

参考资料:

http://hi.baidu.com/yinghawk/item/62206a8772f3595f26ebd93c

http://yefeng.iteye.com/blog/315847

http://www.cnblogs.com/liangbin/articles/2117288.html

posted @ 2013-07-18 11:19 回头多少个秋 阅读(7467) 评论(2) 推荐(1) 编辑
  2013年7月9日
摘要: 点击Edit global .gitconfig添加[http] sslVerify = false proxy = http://127.0.0.1:8087 阅读全文
posted @ 2013-07-09 09:00 回头多少个秋 阅读(291) 评论(0) 推荐(0) 编辑
  2013年6月15日
摘要: 友元函数不是类的成员函数,本质上仍是普通函数,却能像成员函数那样访问类的私有成员.1.友元函数是定义在类外的普通函数,它不属于任何类,但需要在类的定义中加以声明,声明时只需在友元的名称前加上关键字friend2.友元函数的声明可以放在类的私有部分,也可以放在公有部分3.一个函数可以是多个类的友元函数,只需要在各个类中分别声明简单规则:1.如果函数要执行的任务只涉及一个对象,就使用成员函数2.如果要执行的任务涉及对歌对象,就使用非成员函数#include <iostream>using namespace stdclass DayOfYear{public: friend bool 阅读全文
posted @ 2013-06-15 22:19 回头多少个秋 阅读(160) 评论(0) 推荐(0) 编辑
  2013年6月6日
摘要: 想把svn仓库转换成git.通过网上查阅资料,得知应使用命令git svn clone https://xxx.googlecode.com/svn/ -s如果是本地文件,则命令是git svn clone file:///tmp/xxx-svn -s在windows下操作出现了问题:1.用TortoiseGit,URL处填写D:\Repositories\project1,出现错误:git did not exit cleanly (exit code 1)2.用Git Bash,git svn clone file:///d:\repositories\project1,出现错误Could 阅读全文
posted @ 2013-06-06 22:05 回头多少个秋 阅读(595) 评论(0) 推荐(0) 编辑
  2013年5月23日
摘要: import asyncoreimport socketimport sysclass Receiver(asyncore.dispatcher_with_send): def __init__(self,sock,remote_ip,remote_port): asyncore.dispatcher_with_send.__init__(self,sock=sock) self.buffer="" self.max_buffer_size=1024000 self.remote_ip=remote_ip se... 阅读全文
posted @ 2013-05-23 11:13 回头多少个秋 阅读(4877) 评论(0) 推荐(1) 编辑
  2013年5月21日
摘要: import sqlite3if __name__ =='__main__': db_file='d:\\data.db' conn=sqlite3.connect(db_file) cur=conn.cursor() cur.execute('SELECT * FROM table1') records=cur.fetchall() for x in records: print x[0]+' '+x[1] #有多少列就打印多少个 conn.close() 阅读全文
posted @ 2013-05-21 17:06 回头多少个秋 阅读(322) 评论(0) 推荐(0) 编辑
摘要: import osimport os.pathif __name__ =='__main__': root_dir='g:\\download' for root, dirs, files in os.walk(root_dir): for dir in dirs: print 'root=%s dir=%s' % (root,dir) #print 'root='+root+' dir='+dir for file in files: print 'roo... 阅读全文
posted @ 2013-05-21 17:01 回头多少个秋 阅读(147) 评论(0) 推荐(0) 编辑
  2013年5月2日
摘要: 问题症状:This virtual machine's policies are too old to be run by this version of VMware. Contact your system administrator.解决方法:删除.vmx文件中如下三行policy.vm.mvmtid = "52 d6 6f f9 f3 36 0f 9f-31 da 9f f5 90 7b ce 76"policy.vm.managedVMTemplate = "TRUE"policy.vm.managedVM = "FALSE& 阅读全文
posted @ 2013-05-02 15:34 回头多少个秋 阅读(738) 评论(0) 推荐(0) 编辑
  2013年4月27日
摘要: 参考资料:http://bbs.pediy.com/showthread.php?t=167693http://www.cnblogs.com/autosar/archive/2012/04/08/2437799.html/**/#include <Windows.h>#include <stdio.h>char comb_array[]={'A','B','C','D','E','F','G','H','I','J&# 阅读全文
posted @ 2013-04-27 00:20 回头多少个秋 阅读(185) 评论(0) 推荐(0) 编辑
  2013年3月22日
摘要: 最近写的一个程序,大致用到以下代码: WSADATA wsaData; WSAStartup (MAKEWORD( 2, 2 ),&wsaData); struct addrinfo *aiList=0; sockaddr_in addr; char *ip; while (TRUE) { if (0==getaddrinfo("www.qq.com",0,0,&aiList)) { addr=*(struct sockaddr_in *)aiList->ai_addr; ip=i... 阅读全文
posted @ 2013-03-22 11:14 回头多少个秋 阅读(736) 评论(0) 推荐(0) 编辑
< 2025年4月 >
30 31 1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 1 2 3
4 5 6 7 8 9 10

点击右上角即可分享
微信分享提示