• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C++版--MsMultiPartFormData

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

网上就是找不到这个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();
}


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# 非public的方法和属性的单元测试发布时间:2022-07-13
下一篇:
c#计算两点间距离发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap