📄 httpdelegate.cpp
字号:
// HttpDelegate.cpp: implementation of the CHttpDelegate class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "HttpDelegate.h"
#include "string.h"
#include "Winnls.h"
#include <stdio.h>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#define SIZE 2048
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
static CString SetupPath="\\Program Files\\执法通系统\\download\\";
//CStringFile M_StringFile;
CHttpDelegate CHttpDelegate::m_HttpDelegate;
CMyInternetSession * CHttpDelegate::m_pSession;
static char *filebuffer=new char[409600];
static char *xmlbuffer=new char[409600];
static xmlsize=0;
WCHAR *widexml=new WCHAR[409600];
//CString filebuffer;
//static char filedata[409600];
//static char *filedata;
//static CString strs;
//static char *filedata;
//static TCHAR widestr[409600]=_T("");
static filesize=0;
static char *datapart=new char[419840];
static int lenc=0;
static CString datahead;
CHttpDelegate::CHttpDelegate()
{
m_pSession = NULL;
m_pConnection = NULL;
m_pHttpFile = NULL;
}
CHttpDelegate::~CHttpDelegate()
{
DeleteConnection();
delete []filebuffer;
}
CHttpDelegate * CHttpDelegate::GetInstance()
{
return &m_HttpDelegate;
}
BOOL CHttpDelegate::PostGetData(HttpParams * pHttpParams,BOOL bLinked)
{
BOOL bRet = FALSE;
if(pHttpParams == NULL)
return FALSE;
if(!ParseURL(pHttpParams))
return FALSE;
if(GetHttpFile(pHttpParams->m_strServerName,pHttpParams->m_strObject,pHttpParams->m_nPort,pHttpParams,bLinked)) bRet = TRUE;
return bRet ;
}
/*bool CHttpDelegate::PostGetQueryInfo(HttpParams * pHttpParams,BOOL bLinked){
bool bRet = false;
::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
if(pHttpParams == NULL)
{
::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
return FALSE;
}
if(!ParseURL(pHttpParams))
{
::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
return FALSE;
}
if(GetMultiFile(pHttpParams->m_strServerName,pHttpParams->m_strObject,pHttpParams->m_nPort,pHttpParams,bLinked)) bRet = TRUE;
::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
return bRet;
}
bool CHttpDelegate::GetMultiFile(LPCTSTR ServerName,LPCTSTR strObject,INTERNET_PORT nPort,HttpParams *pHttpParams,BOOL bLinked)
{
CString rString=_T("");
int j=0;
pHttpParams->m_Contents = _T("");
if(strObject == NULL) return FALSE;
if(GetHttpStatus(ServerName, strObject,nPort,bLinked)!= 200)
return FALSE;
if(m_pHttpFile != NULL && m_pConnection != NULL)
m_pHttpFile->QueryInfo(HTTP_QUERY_CONTENT_TYPE,rString);
if(m_pHttpFile != NULL && m_pConnection != NULL && m_pSession != NULL)
{
xmlsize=0;
char *tmps=new char[2048];
strcpy(tmps,"");
int lensxml=0;
CStdioFile csf;
if(!csf.Open(_T("\\My Documents\\test.txt"),CFile::modeCreate|CFile::modeWrite | CFile::typeBinary | CFile::shareDenyWrite))
{
AfxMessageBox(L"写文件错误!\n文件正在使用中,请先关闭程序!",MB_ICONSTOP);
return FALSE;
}
int n=0;
char *buf=new char[2048];
//AfxMessageBox(L"开始读文件");
while((n=m_pHttpFile->Read(buf,sizeof(buf)))>0)
{
csf.Write(buf,n);
}
csf.Close();
delete []buf;
//AfxMessageBox(L"文件下载成功!");
if(!csf.Open(_T("\\My Documents\\test.txt"),CFile::modeRead | CFile::typeBinary | CFile::shareDenyWrite))
{
AfxMessageBox(L"读文件错误!\n文件正在使用中,请先关闭程序!",MB_ICONSTOP);
}
else
{
char *xml=new char[409600];
while((lensxml=csf.Read(tmps,2048))>0)
{
for(int rr=0;rr<lensxml;rr++)
{
xml[xmlsize+rr]=tmps[rr];
}
xmlsize+=lensxml;
}
pHttpParams->m_Contents=xml;
delete xml;
csf.Close();
wcscpy(widexml,_T(""));
strcpy(tmps,"");
}
}
return TRUE;
}*/
BOOL CHttpDelegate::PostAndGetData(HttpParams * pHttpParams,BOOL bLinked)
{
BOOL bRet = FALSE;
//::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
if(pHttpParams == NULL)
{
//AfxMessageBox(_T("null"));
::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
return FALSE;
}
if(!ParseURL(pHttpParams))
{
//AfxMessageBox(_T("url error"));
::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
return FALSE;
}
//AfxMessageBox(_T("GetHttpFile start"));
if(GetHttpFile(pHttpParams->m_strServerName,pHttpParams->m_strObject,pHttpParams->m_nPort,pHttpParams,bLinked)) bRet = TRUE;
//AfxMessageBox(_T("GetHttpFile end"));
//if(PostGetData(pHttpParams)) bRet=TRUE;
//::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
/*
if(bRet == false)
{
AfxMessageBox(_T("here"));
}
*/
return bRet ;
}
BOOL CHttpDelegate::PostAndGetDatatpcx(HttpParams * pHttpParams,BOOL bLinked)
{
BOOL bRet = FALSE;
::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
if(pHttpParams == NULL)
{
//AfxMessageBox(_T("null"));
::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
return FALSE;
}
if(!ParseURL(pHttpParams))
{
//AfxMessageBox(_T("url error"));
::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
return FALSE;
}
//AfxMessageBox(_T("GetHttpFile"));
if(GetHttpFile(pHttpParams->m_strServerName,pHttpParams->m_strObject,pHttpParams->m_nPort,pHttpParams,bLinked)) bRet = TRUE;
//AfxMessageBox(_T("GetHttpFile"));
//if(PostGetData(pHttpParams)) bRet=TRUE;
::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
/*
if(bRet == false)
{
AfxMessageBox(_T("here"));
}
*/
return bRet ;
}
BOOL CHttpDelegate::PostPicture(HttpParams * pHttpParams,BOOL bLinked)
{
BOOL bRet = FALSE;
if(pHttpParams == NULL)
return FALSE;
if(!ParseURL(pHttpParams))
return FALSE;
if(PostGetData(pHttpParams)) bRet=TRUE;
return bRet ;
}
BOOL CHttpDelegate::PostData(HttpParams * pHttpParams,BOOL bLinked)
{
BOOL bRet = FALSE;
::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
if(pHttpParams == NULL)
{
//AfxMessageBox(_T("null"));
::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
return FALSE;
}
if(!ParseURL(pHttpParams))
{
//AfxMessageBox(_T("url error"));
::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
return FALSE;
}
//AfxMessageBox(_T("here"));
//if(GetHttpFile(pHttpParams->m_strServerName,pHttpParams->m_strObject,pHttpParams->m_nPort,pHttpParams,bLinked)) bRet = TRUE;
if(PostGetData(pHttpParams)) bRet=TRUE;
::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
/*
if(bRet == false)
{
AfxMessageBox(_T("here"));
}
*/
return bRet ;
}
BOOL CHttpDelegate::InitialHttp()
{
m_pSession = new CMyInternetSession(
NULL,
1);
//int ntimeOut = 30;
/*
The time-out value in milliseconds to use for Internet connection requests.
If a connection request takes longer than this timeout, the request is canceled.
The default timeout is infinite. */
//m_pSession->SetOption(INTERNET_OPTION_CONNECT_TIMEOUT,ntimeOut);
/* The delay value in milliseconds to wait between connection retries.*/
//m_pSession->SetOption(INTERNET_OPTION_CONNECT_BACKOFF,1000);
/* The retry count to use for Internet connection requests. If a connection
attempt still fails after the specified number of tries, the request is canceled.
The default is five. */
//m_pSession->SetOption(INTERNET_OPTION_CONNECT_RETRIES,1);
//m_pSession->SetOption(INTERNET_REQFLAG_CACHE_WRITE_DISABLED,0);
m_pSession->EnableStatusCallback(TRUE);
return TRUE;
}
BOOL CHttpDelegate::DeleteConnection()
{
if (m_pHttpFile != NULL)
{
m_pHttpFile->Close();
delete m_pHttpFile;
}
if (m_pConnection != NULL)
{
m_pConnection->Close();
delete m_pConnection;
}
if (m_pSession != NULL)
{
m_pSession->Close();
delete m_pSession;
}
return TRUE;
}
BOOL CHttpDelegate::ParseURL(HttpParams *pHttpParams)
{
BOOL bRet = FALSE;
if(pHttpParams->m_pszURL.IsEmpty()) return bRet;
LPCTSTR lpsz = pHttpParams->m_pszURL;
if(lpsz == NULL) return bRet;
int nLen = lstrlen(lpsz);
int i=0;
while (nLen)
{
if( *lpsz == L'/') i++;
++lpsz;
nLen--;
}
if( i< 3) pHttpParams->m_pszURL += L"/";
AfxParseURL(pHttpParams->m_pszURL,pHttpParams->m_dwServiceType,
pHttpParams->m_strServerName,pHttpParams->m_strObject,pHttpParams->m_nPort);
lpsz = pHttpParams->m_strObject;
if(lpsz == NULL) return bRet;
bRet = TRUE;
nLen = lstrlen(lpsz);
BOOL bdot = FALSE;
while (nLen)
{
if( *lpsz == L'.') bdot = TRUE;
++lpsz;
nLen--;
}
if(pHttpParams->m_strObject.GetLength()-1 > 0)
{
if( bdot == FALSE && pHttpParams->m_strObject[pHttpParams->m_strObject.GetLength()-1] != L'/')
{}
// pHttpParams->m_strObject += L"/";
}
return bRet;
}
BOOL CHttpDelegate::GetHttpFile(LPCTSTR ServerName,LPCTSTR strObject,INTERNET_PORT nPort,HttpParams *pHttpParams,BOOL bLinked)
{
CString rString=_T("");
int j=0;
pHttpParams->m_Contents = _T("");
if(strObject == NULL) return FALSE;
//int flsg=GetHttpStatus(ServerName, strObject,nPort,bLinked);
if(GetHttpStatus(ServerName, strObject,nPort,bLinked)!= 200)
{
//AfxMessageBox(_T("200"));
return FALSE;
}
if(m_pHttpFile != NULL && m_pConnection != NULL)
{
m_pHttpFile->QueryInfo(HTTP_QUERY_CONTENT_TYPE,rString);
}
if(m_pHttpFile != NULL && m_pConnection != NULL && m_pSession != NULL)
{
//AfxMessageBox(_T("ok here"));
//pHttpParams->m_Contents=_T("POST /upload_file/UploadFile HTTP/1.1\n\nAccept: text/plain, */*\n\nAccept-Language: zh-cn\n\nHost: 192.168.29.65:80\n\nContent-Type:multipart/form-data;boundary=---------------------------7d33a816d302b6\nUser-Agent: Mozilla/4.0 (compatible; OpenOffice.org)\n\nContent-Length: 424\nConnection: Keep-Alive\n\n\n\n-----------------------------7d33a816d302b6\n\nContent-Disposition: form-data; name=\"userfile1\"; filename=\"E:\\s\"\n\nContent-Type: application/octet-stream\n\n\n\na\n\nbb\n\nXXX\n\nccc\n\n-----------------------------7d33a816d302b6\n\nContent-Disposition: form-data; name=\"text1\"\n\n\n\n\nfoo\n\n-----------------------------7d33a816d302b6\n\nContent-Disposition: form-data; name=\"password1\"\n\n\n\n\nbar\n\n-----------------------------7d33a816d302b6--\n\n");
//xmlsize=0;
//strcpy(xmlbuffer,"");
////////////////////////////////////
xmlsize=0;
char *tmps=new char[2048];
strcpy(tmps,"");
int lensxml=0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -