#include "StdAfx.h"
#include "WinHttpPostUpFile.h"
//每次1M以内
UploadInterfaceErrorCode WinHttpPostUpFile::StartSendData(const std::wstring& url, const std::wstring& cookie,const std::wstring& filepath, long lstart,long lend,long luploadid )
{
//数据检查
std::string strfilepath;
long perread=lend-lstart+1;
if (perread<=0 || perread>send_data_max_){
return kErrorParameterInput;
}
UploadInterfaceErrorCode is_ok=kNoError;
is_ok=GetHandle(url);
if (is_ok!=kNoError){
return is_ok;
}
std::wstring strHeader(L"");
//strHeader+=L"Cookie:";
//strHeader+=cookie;
//strHeader+=L"\r\n";
strHeader+=L"Content-Type: multipart/form-data; boundary=--boundary\r\n";
//计算WinHttpSendRequest倒数第二个参数:body总长度
std::string upload_id;
std::string seg_start;
std::string seg_end;
std::string seg_md5;
std::string exinfo;
std::stringstream stream_id;
std::stringstream stream_st;
std::stringstream stream_en;
stream_id <<luploadid; //从long型数据输入
stream_id >> upload_id; //转换为 string
stream_st<<lstart;
stream_st>>seg_start;
stream_en<<lend;
stream_en>>seg_end;
exinfo="----boundary\r\n";
exinfo+="Content-Disposition: form-data; name=\"file\"; filename=\"";
strfilepath=UnicodeToUTF8(filepath);
exinfo+=strfilepath;
exinfo+="\"\r\n";
exinfo+="Content-Type:application/octet-stream\r\n\r\n";
//读文件
FILE *fp;
if (_wfopen_s(&fp,filepath.c_str(),_T("rb"))){
return kErrorOpenFile;
}
fseek(fp,lstart,SEEK_SET);
MD5 md5;
std::string filecontent("");
char *buffer;
buffer= new char[1024*1024]();
perread=fread(buffer,sizeof(char),1024*1024,fp);
if (!feof(fp) && perread != 1024*1024){
delete [] buffer;
fclose(fp);
return kErrorReadFile;
}
filecontent.append(buffer, perread);
delete [] buffer;
fclose(fp);
//文件其他信息
std::string endinfo="----boundary\r\n";
endinfo+="Content-Disposition: form-data; name=\"uploadId\"\r\n\r\n";
endinfo+=upload_id;
endinfo+="\r\n";
endinfo+="----boundary\r\n";
endinfo+="Content-Disposition: form-data; name=\"start\"\r\n\r\n";
endinfo+=seg_start;
endinfo+="\r\n";
endinfo+="----boundary\r\n";
endinfo+="Content-Disposition: form-data; name=\"end\"\r\n\r\n";
endinfo+=seg_end;
endinfo+="\r\n";
endinfo+="----boundary\r\n";
endinfo+="Content-Disposition: form-data; name=\"md5\"\r\n\r\n";
md5.update(filecontent);
seg_md5=md5.toString();
endinfo+=seg_md5;
endinfo+="\r\n";
std::string endboundary="----boundary--\r\n";
std::string total=exinfo+"\r\n"+endinfo+endboundary;
BOOL bresult=TRUE;
bresult=WinHttpAddRequestHeaders(hRequest, strHeader.c_str(), strHeader.length(), WINHTTP_ADDREQ_FLAG_ADD | WINHTTP_ADDREQ_FLAG_REPLACE);
if (bresult)
{
bresult=WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, 0, 0,total.length()+perread, 0);
if (is_stop_){
return kNoError;
}
}
else return kErrorAddRequestHeader;
DWORD dwWritten = 0;
if (bresult){
bresult=WinHttpWriteData(hRequest, exinfo.c_str(), exinfo.length(), &dwWritten);
if (is_stop_){
return kNoError;
}
}
else return kErrorRequestHandle;
if (bresult){
bresult=WinHttpWriteData(hRequest, filecontent.c_str(), filecontent.length(), &dwWritten);
if (is_stop_){
return kNoError;
}
}
else return kErrorSendData;
if (bresult){
std::string _total="\r\n"+endinfo+endboundary;
bresult=WinHttpWriteData(hRequest, _total.c_str(), _total.length(), &dwWritten);
if (is_stop_){
return kNoError;
}
}
else return kErrorSendData;
if (bresult){
is_ok=GetResponseData();
if (is_stop_){
return kNoError;
}
if (is_ok!=kNoError){
return is_ok;
}
return kNoError;
}
else return kErrorSendData;
}
WinHttpPostUpFile::WinHttpPostUpFile():
hSession(NULL),
hConnect(NULL),
hRequest(NULL),
is_stop_(FALSE),
send_data_max_(1024*1024),
response_data_("")
{
}
UploadInterfaceErrorCode WinHttpPostUpFile::GetResponseData()
{
if (!WinHttpReceiveResponse(hRequest,NULL)){
return kErrorReceiveResponse;
}
//得到返回状态码
DWORD dwSize = 0, dwDownloaded = 0;
std::wstring code(L"");
WinHttpQueryHeaders(hRequest,WINHTTP_QUERY_STATUS_CODE, WINHTTP_HEADER_NAME_BY_INDEX,NULL,&dwSize,WINHTTP_NO_HEADER_INDEX);
if( GetLastError() == ERROR_INSUFFICIENT_BUFFER){
wchar_t *szHeader = new wchar_t[dwSize]();
WinHttpQueryHeaders(hRequest,WINHTTP_QUERY_STATUS_CODE,WINHTTP_HEADER_NAME_BY_INDEX, szHeader, &dwSize,WINHTTP_NO_HEADER_INDEX);
code=szHeader;
delete[] szHeader;
}
else return kErrorReceiveResponse;
LPSTR pszOutBuffer = nullptr;
if(code==L"200")
{
do
{
dwSize = 0;
if (!WinHttpQueryDataAvailable( hRequest, &dwSize))break;
if (!dwSize)break;
pszOutBuffer = new char[dwSize+1]();
if (!WinHttpReadData( hRequest, (LPVOID)pszOutBuffer, dwSize, &dwDownloaded)){
break;
}
else{
if (is_stop_){
break;
}
response_data_=pszOutBuffer;
}
} while (dwSize > 0);
delete [] pszOutBuffer;
return kNoError;
}
else return kErrorReceiveResponse;
}
bool WinHttpPostUpFile::Stop()
{
lock_.Lock();
is_stop_=TRUE;
lock_.Unlock();
if (hRequest) WinHttpCloseHandle(hRequest);
if (hConnect) WinHttpCloseHandle(hConnect);
if (hSession) WinHttpCloseHandle(hSession);
hRequest=NULL;
hConnect=NULL;
hSession=NULL;
return true;
}
WinHttpPostUpFile::~WinHttpPostUpFile()
{
if (hRequest) WinHttpCloseHandle(hRequest);
if (hConnect) WinHttpCloseHandle(hConnect);
if (hSession) WinHttpCloseHandle(hSession);
hRequest=NULL;
hConnect=NULL;
hSession=NULL;
}
UploadInterfaceErrorCode WinHttpPostUpFile::GetHandle( std::wstring url)
{
//解析url
URL_COMPONENTS uc = {0};
uc.dwStructSize = sizeof(uc);
WCHAR host[MAX_PATH]={0};
uc.lpszHostName = host;
uc.dwHostNameLength = MAX_PATH;
WCHAR path[MAX_PATH]={0};
uc.lpszUrlPath = path;
uc.dwUrlPathLength = MAX_PATH * 5;
WCHAR extra[MAX_PATH]={0};
uc.lpszExtraInfo = extra;
uc.dwExtraInfoLength = MAX_PATH * 5;
if (!WinHttpCrackUrl(url.c_str(), url.length(), ICU_ESCAPE, &uc))
{
return kErrorWinHttpCrackUrl;
}
hSession=WinHttpOpen(L"User Agent",WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,WINHTTP_NO_PROXY_NAME,WINHTTP_NO_PROXY_BYPASS,0);
if(hSession){
hConnect=WinHttpConnect(hSession, host,INTERNET_DEFAULT_HTTP_PORT,0);
}
else return kErrorSessionHandle;
if(hConnect){
hRequest=WinHttpOpenRequest(hConnect, L"POST",path,L"HTTP/1.1", WINHTTP_NO_REFERER,WINHTTP_DEFAULT_ACCEPT_TYPES,0);
}
if (hRequest){
return kNoError;
}
else return kErrorConnectHandle;
}
std::string WinHttpPostUpFile::get_response_data()
{
return response_data_;
}
- 1
- 2
前往页