zoukankan      html  css  js  c++  java
  • C++版MsMultiPartFormData

    网上就是找不到这个C++版的MsMultiPartFormData,都是indy下的object pascal版本的。受不了了,我干脆自己写了个。呵呵,花了点时间,弄好了。弄好了才发现,对HTTP的POST请求包的格式好清楚了,呵呵,收获呀。

    ****************************************************************

    MsMultiPartFormData.h

    #pragma once

    class MsMultiPartFormData
    {
    public:
    CString Boundary;
    CString RequestContentType;
    private:
    CString CONTENT_TYPE;
    CString CRLF ;
    CString CONTENT_DISPOSITION ;
    CString FILE_NAME_PLACE_HOLDER ;
    CString CONTENT_TYPE_PLACE_HOLDER;
    CString CONTENT_LENGTH ;
    CString DD;
    CString ALLDATA;

    public:
    MsMultiPartFormData(void);
    public:
    ~MsMultiPartFormData(void);
    private:
    CString GenerateUniqueBoundary(void);
    public:
    void AddFormField(CString FieldName, CString FieldValue);
    public:
    void AddFile(CString FieldName,CString FileName,CString ContentType);
    public:
    void PrepareStreamForDispatch();
    public:
    byte* GetFormData();
    public:
        int GetFormDataLengh();

    };

    ****************************************************************

    MsMultiPartFormData.cpp

    #include "StdAfx.h"
    #include "MsMultiPartFormData.h"
    #include "time.h"
    #include "atlfile.h"
    #include <iostream>
    #include <sstream>
    #include <string>

    MsMultiPartFormData::MsMultiPartFormData(void)
    {
    CONTENT_TYPE="multipart/form-data; boundary=";
    CRLF="\r\n";
    CONTENT_DISPOSITION ="Content-Disposition: form-data; name=\"%s\"";
    FILE_NAME_PLACE_HOLDER ="; filename=\"%s\"";
    CONTENT_TYPE_PLACE_HOLDER = "Content-Type: ";
    CONTENT_LENGTH = "Content-Length: %d\r\n";
    DD="--";

    Boundary = GenerateUniqueBoundary();
    RequestContentType = CONTENT_TYPE + Boundary;
    }

    MsMultiPartFormData::~MsMultiPartFormData(void)
    {
    }

    CString MsMultiPartFormData:: GenerateUniqueBoundary(void)
    {
    CString dd("---------------------------");
    CString tt;
    CString boundary;
    time_t   now;  
    time(&now);
    tt.Format("%d",now);
    boundary =dd+tt;
    return boundary;
    }

    void MsMultiPartFormData:: AddFormField(CString FieldName, CString FieldValue)
    {
    CString field;
    field.Format(CRLF+ DD + Boundary + CRLF + CONTENT_DISPOSITION + CRLF + CRLF +FieldValue, FieldName);
    ALLDATA+=field;
    }

    void MsMultiPartFormData:: AddFile(CString FieldName,CString FileName,CString ContentType)
    {

    //从本地文件中读取文件内容,进行上传
    CString field;
    //CString fileData;
    CAtlFile   file;

    HRESULT   hr=file.Create(FileName,GENERIC_READ,   FILE_SHARE_READ, OPEN_EXISTING);

    if(FAILED(hr))
    {
       return;
    }

    ULONGLONG filesize=0;

    file.GetSize(filesize);

    byte* buffer=new byte[filesize+1];

    if(FAILED(hr))
    {
       delete[]   buffer;
    }

    file.Seek( 0, FILE_BEGIN );
    hr=file.Read(buffer,filesize);

    if(FAILED(hr))
    {
       delete[] buffer;
    }

    file.Close();
    buffer[filesize]=0;

    CString fileData;

    long index;
    for(index=0;index<filesize;index++)
    {
       fileData+=buffer[index];
    }

    field.Format(CRLF + DD + Boundary + CRLF + CONTENT_DISPOSITION +FILE_NAME_PLACE_HOLDER + CRLF + CONTENT_LENGTH +CONTENT_TYPE_PLACE_HOLDER+ContentType+CRLF+CRLF, FieldName, FileName, filesize);
    field+=fileData;
    ALLDATA+=field;
    }

    void MsMultiPartFormData:: PrepareStreamForDispatch()
    {
    CString field;
    field=CRLF+ DD + Boundary + DD +CRLF;
    ALLDATA+=field;
    }

    byte* MsMultiPartFormData:: GetFormData()
    {
    return (byte*)ALLDATA.GetBuffer(ALLDATA.GetLength());
    }

    int MsMultiPartFormData:: GetFormDataLengh()
    {
    return ALLDATA.GetLength();
    }

  • 相关阅读:
    uva 1362(区间dp+计数)
    uva 11174(排列组合+搜索)
    简单递推系列 uva习题
    基本计数方法系列 uva习题
    Codeforces Round #209 (Div. 2) Problem A Table(找规律)
    CodeChef November Challenge 2013 解题报告
    2012 chengdu现场赛 Browsing History HDU4464(简单字符串)
    CodeChef TechFest 2013 Cool Numbers(搜索)
    CodeChef Inscription 2013 Wonderland jewellery(简单题)
    页面滚动marquee标签
  • 原文地址:https://www.cnblogs.com/liangquewei/p/1232121.html
Copyright © 2011-2022 走看看