acl/samples/http_client/http_clientDlg.cpp

411 lines
10 KiB
C++
Raw Normal View History

// http_clientDlg.cpp : ʵ<><CAB5><EFBFBD>ļ<EFBFBD>
//
#include "stdafx.h"
#include "http_client.h"
#include "HttpClient.h"
#include "HttpOption.h"
#include "http_clientDlg.h"
#include ".\http_clientdlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// <20><><EFBFBD><EFBFBD>Ӧ<EFBFBD>ó<EFBFBD><C3B3>򡰹<EFBFBD><F2A1B0B9>ڡ<EFBFBD><DAA1>˵<EFBFBD><CBB5><EFBFBD><EFBFBD><EFBFBD> CAboutDlg <20>Ի<EFBFBD><D4BB><EFBFBD>
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// <20>Ի<EFBFBD><D4BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV ֧<><D6A7>
// ʵ<><CAB5>
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()
// Chttp_clientDlg <20>Ի<EFBFBD><D4BB><EFBFBD>
Chttp_clientDlg::Chttp_clientDlg(CWnd* pParent /*=NULL*/)
: CDialog(Chttp_clientDlg::IDD, pParent)
, m_sReqUrl(_T(""))
, m_sLocalFile(_T(""))
, m_bHttp11(TRUE)
, m_bZip(FALSE)
, m_bKeepAlive(FALSE)
, m_sHttpHdrAppend(_T(""))
, m_sServerAddr(_T(""))
, m_sAccept(_T("application/json, text/javascript, */*; q=0.01"))
, m_sCType(_T(""))
, m_bUseAddr(FALSE)
, m_nContentLength(-1)
, m_bForwardAuto(FALSE)
, m_nMaxTry(10)
, m_bPostMethod(FALSE)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void Chttp_clientDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Text(pDX, IDC_EDIT_URL, m_sReqUrl);
DDX_Text(pDX, IDC_EDIT_FILE, m_sLocalFile);
DDX_Control(pDX, IDC_EDIT_HDR_REQ, m_reqCtlEdit);
DDX_Control(pDX, IDC_EDIT_HDR_RES, m_resCtlEdit);
DDX_Control(pDX, IDC_EDIT_RES_BODY, m_resBodyCtlEdit);
}
BEGIN_MESSAGE_MAP(Chttp_clientDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_BUTTON_BROWSER, OnBnClickedButtonBrowser)
ON_BN_CLICKED(IDC_BUTTON_OPTION, OnBnClickedButtonOption)
ON_BN_CLICKED(IDC_BUTTON_GET, OnBnClickedButtonGet)
ON_MESSAGE(WM_USER_DISPLAY, OnWriteDisplay)
ON_BN_CLICKED(IDC_BUTTON_CLEAR, OnBnClickedButtonClear)
ON_WM_SIZE()
ON_WM_CREATE()
END_MESSAGE_MAP()
// Chttp_clientDlg <20><>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
BOOL Chttp_clientDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// <20><>\<5C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>...\<5C><><EFBFBD>˵<EFBFBD><CBB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD>ϵͳ<CFB5>˵<EFBFBD><CBB5>С<EFBFBD>
// IDM_ABOUTBOX <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵͳ<CFB5><CDB3><EFBFBD>Χ<EEB7B6>ڡ<EFBFBD>
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// <20><><EFBFBD>ô˶Ի<CBB6><D4BB><EFBFBD><EFBFBD><EFBFBD>ͼ<EFBFBD><EFBFBD><EAA1A3>Ӧ<EFBFBD>ó<EFBFBD><C3B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><DAB2>ǶԻ<C7B6><D4BB><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD>ܽ<EFBFBD><DCBD>Զ<EFBFBD>
// ִ<>д˲<D0B4><CBB2><EFBFBD>
SetIcon(m_hIcon, TRUE); // <20><><EFBFBD>ô<EFBFBD>ͼ<EFBFBD><CDBC>
SetIcon(m_hIcon, FALSE); // <20><><EFBFBD><EFBFBD>Сͼ<D0A1><CDBC>
// TODO: <20>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD>Ӷ<EFBFBD><D3B6><EFBFBD><EFBFBD>ij<EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
GetDlgItem(IDC_EDIT_FILE)->SetWindowText("result.txt");
CheckDlgButton(IDC_CHECK_SAVE, 0);
CheckDlgButton(IDC_CHECK_ECHO, 0);
m_resBodyCtlEdit.SetLimitText(0);
// <20><><EFBFBD><EFBFBD>״̬<D7B4><CCAC>
int aWidths[3] = {50, 250, -1};
m_wndMeterBar.SetParts(3, aWidths);
m_wndMeterBar.Create(WS_CHILD | WS_VISIBLE | WS_BORDER
| CCS_BOTTOM | SBARS_SIZEGRIP,
CRect(0,0,0,0), this, 0);
m_wndMeterBar.SetText("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", 0, 0);
m_wndMeterBar.SetText("", 1, 0);
m_wndMeterBar.SetText("", 2, 0); //SBT_NOBORDERS);
//int strPartDim[3]= {100, 200, -1}; //<2F>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>
//m_StatusBar.SetParts(3, strPartDim);
////<2F><><EFBFBD><EFBFBD>״̬<D7B4><CCAC><EFBFBD>ı<EFBFBD>
//m_StatusBar.SetText("", 0, 0);
//m_StatusBar.SetText("", 1, 0);
//m_StatusBar.SetText("", 2, 0);
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬<D7B4><CCAC><EFBFBD>м<EFBFBD><D0BC><EFBFBD>ͼ<EFBFBD><CDBC>
//Ϊ<>ڶ<EFBFBD><DAB6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>мӵ<D0BC>ͼ<EFBFBD><CDBC>
//m_StatusBar.SetIcon(0, SetIcon(AfxGetApp()->LoadIcon(IDR_MAINFRAME), FALSE));
return TRUE; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˿ؼ<CBBF><D8BC>Ľ<EFBFBD><C4BD><EFBFBD><E3A3AC><EFBFBD>򷵻<EFBFBD> TRUE
}
void Chttp_clientDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ի<EFBFBD><D4BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD>ť<EFBFBD><C5A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD>
// <20><><EFBFBD><EFBFBD><EFBFBD>Ƹ<EFBFBD>ͼ<EFBFBD><EFBFBD><EAA1A3><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD>ĵ<EFBFBD>/<2F><>ͼģ<CDBC>͵<EFBFBD> MFC Ӧ<>ó<EFBFBD><C3B3><EFBFBD><EFBFBD><EFBFBD>
// <20><EFBFBD>ɿ<EFBFBD><C9BF><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD>ɡ<EFBFBD>
void Chttp_clientDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // <20><><EFBFBD>ڻ<EFBFBD><DABB>Ƶ<EFBFBD><C6B5><EFBFBD><E8B1B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// ʹͼ<CAB9><CDBC><EFBFBD>ڹ<EFBFBD><DAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>о<EFBFBD><D0BE><EFBFBD>
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// <20><><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC>
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
//<2F><><EFBFBD>û<EFBFBD><C3BB>϶<EFBFBD><CFB6><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱϵͳ<CFB5><CDB3><EFBFBD>ô˺<C3B4><CBBA><EFBFBD>ȡ<EFBFBD>ù<EFBFBD><C3B9><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE>
HCURSOR Chttp_clientDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void Chttp_clientDlg::OnBnClickedButtonBrowser()
{
// TODO: <20>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD>ӿؼ<D3BF>֪ͨ<CDA8><D6AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
CFileDialog file(TRUE,"<EFBFBD>ļ<EFBFBD>","result.txt",OFN_HIDEREADONLY,"FILE(*.*)|*.*||",NULL);
if(file.DoModal()==IDOK)
{
CString pathname;
pathname=file.GetPathName();
GetDlgItem(IDC_EDIT_FILE)->SetWindowText(pathname);
}
}
void Chttp_clientDlg::OnBnClickedButtonOption()
{
// TODO: <20>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD>ӿؼ<D3BF>֪ͨ<CDA8><D6AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
CHttpOption option;
option.m_bHttp11 = m_bHttp11;
option.m_bZip = m_bZip;
option.m_bKeepAlive = m_bKeepAlive;
option.m_sHttpHdrAppend = m_sHttpHdrAppend;
option.m_sServerAddr = m_sServerAddr;
option.m_bUseAddr = m_bUseAddr;
option.m_bForwardAuto = m_bForwardAuto;
option.m_nMaxTry = m_nMaxTry;
option.m_bPostMethod = m_bPostMethod;
option.m_sCType = m_sCType;
option.m_sAccept = m_sAccept;
option.m_sHttpBody = m_sBody;
option.m_bPostMethod = m_bPostMethod;
if (option.DoModal() == IDOK) {
m_bHttp11 = option.m_bHttp11;
m_bZip = option.m_bZip;
m_bKeepAlive = option.m_bKeepAlive;
m_sHttpHdrAppend = option.m_sHttpHdrAppend;
m_sServerAddr = option.m_sServerAddr;
if (m_sServerAddr.GetLength() > 0)
m_bUseAddr = option.m_bUseAddr;
else
m_bUseAddr = FALSE;
m_bForwardAuto = option.m_bForwardAuto;
m_nMaxTry = option.m_nMaxTry;
m_bPostMethod = option.m_bPostMethod;
if (m_bPostMethod)
{
m_sBody = option.m_sHttpBody;
if (m_sBody.IsEmpty())
m_bPostMethod = FALSE;
}
m_sCType = option.m_sCType;
m_sAccept = option.m_sAccept;
m_bPostMethod = option.m_bPostMethod;
}
}
void Chttp_clientDlg::OnBnClickedButtonGet()
{
// TODO: <20>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD>ӿؼ<D3BF>֪ͨ<CDA8><D6AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
CString sLocalFile, sReqUrl;
GetDlgItem(IDC_EDIT_FILE)->GetWindowText(sLocalFile);
if (sLocalFile.GetLength() == 0) {
MessageBox("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȷ<EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD>!");
return;
}
GetDlgItem(IDC_EDIT_URL)->GetWindowText(sReqUrl);
if (sReqUrl.GetLength() == 0) {
MessageBox("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȷ<EFBFBD><EFBFBD> URL!");
return;
}
ClearWinText();
if (IsDlgButtonChecked(IDC_CHECK_SAVE))
m_hClient.SaveAs(sLocalFile);
if (IsDlgButtonChecked(IDC_CHECK_ECHO))
m_hClient.DisplayResBody(TRUE);
else
m_hClient.DisplayResBody(FALSE);
m_hClient.EnableHttp11(m_bHttp11);
m_hClient.EnableZip(m_bZip);
m_hClient.EnableKeepAlive(m_bKeepAlive);
m_hClient.m_sHttpHdrAppend = m_sHttpHdrAppend;
m_hClient.m_bUseAddr = m_bUseAddr;
m_hClient.m_sServerAddr = m_sServerAddr;
m_hClient.m_bForwardAuto = m_bForwardAuto;
m_hClient.m_nMaxTry = m_nMaxTry;
m_hClient.m_sHttpBody = m_sBody;
m_hClient.m_bPostMethod = m_bPostMethod;
m_hClient.m_sAccept = m_sAccept;
m_hClient.m_sCtype = m_sCType;
m_hClient.OnDataCallback(this->GetSafeHwnd(), WM_USER_DISPLAY);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>й<EFBFBD>
m_nContentLength = -1;
m_nDownLen = 0;
m_wndMeterBar.GetProgressCtrl().SetPos(0);
m_wndMeterBar.SetText("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", 0, 0);
m_hClient.GetUrl(sReqUrl);
GetDlgItem(IDC_BUTTON_GET)->EnableWindow(FALSE);
}
LRESULT Chttp_clientDlg::OnWriteDisplay(WPARAM wParam, LPARAM lParam)
{
CHttpReport *pReport = (CHttpReport*) wParam;
if (pReport->m_type == TYPE_HDR_REQ) {
m_reqCtlEdit.SetSel(m_reqCtlEdit.GetWindowTextLength(),
m_reqCtlEdit.GetWindowTextLength());
m_reqCtlEdit.ReplaceSel(pReport->m_pBuf);
} else if (pReport->m_type == TYPE_HDR_RES) {
m_resCtlEdit.SetSel(m_resCtlEdit.GetWindowTextLength(),
m_resCtlEdit.GetWindowTextLength());
m_resCtlEdit.ReplaceSel(pReport->m_pBuf);
} else if (pReport->m_type == TYPE_BODY_RES) {
m_resBodyCtlEdit.SetSel(m_resBodyCtlEdit.GetWindowTextLength(),
m_resBodyCtlEdit.GetWindowTextLength());
m_resBodyCtlEdit.ReplaceSel(pReport->m_pBuf);
} else if (pReport->m_type == TYPE_TOTAL_LEN) {
m_nContentLength = pReport->m_nContentLength;
ASSERT(pReport->m_pBuf == NULL);
} else if (pReport->m_type == TYPE_DOWN_LEN) {
m_nDownLen += pReport->m_nDownLen;
if (m_nContentLength > 0) {
http_off_t nStept;
nStept = (m_nDownLen * 100) / (m_nContentLength);
m_wndMeterBar.GetProgressCtrl().SetPos((int) nStept);
}
CString msg;
msg.Format("<EFBFBD><EFBFBD> %I64d <20>ֽ<EFBFBD>, <20><>ʱ %.3f <20><><EFBFBD><EFBFBD>",
m_nDownLen, pReport->m_timeRes);
m_wndMeterBar.SetText(msg, 1, 0);
} else if (pReport->m_type == TYPE_ERROR_CONNECT) {
CString msg;
msg.Format("%s", pReport->m_pBuf);
m_wndMeterBar.GetProgressCtrl().SetText(msg.GetString());
//m_wndMeterBar.GetProgressCtrl.SetWindowText(msg);
} else if (pReport->m_type == TYPE_TIME_RES) {
CString msg;
msg.Format("<EFBFBD><EFBFBD> %I64d <20>ֽ<EFBFBD>, <20><>ʱ %.3f <20><><EFBFBD><EFBFBD>",
m_nDownLen, pReport->m_timeRes);
m_wndMeterBar.SetText(msg, 1, 0);
} else if (pReport->m_type == TYPE_COMPLETE) {
CString msg;
msg.Format("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
m_wndMeterBar.SetText(msg, 0, 0);
GetDlgItem(IDC_BUTTON_GET)->EnableWindow(TRUE);
}
ASSERT(pReport);
delete pReport;
return (0);
}
void Chttp_clientDlg::ClearWinText(void)
{
m_reqCtlEdit.SetSel(0, m_reqCtlEdit.GetWindowTextLength());
m_reqCtlEdit.ReplaceSel("");
m_resCtlEdit.SetSel(0, m_resCtlEdit.GetWindowTextLength());
m_resCtlEdit.ReplaceSel("");
m_resBodyCtlEdit.SetSel(0, m_resBodyCtlEdit.GetWindowTextLength());
m_resBodyCtlEdit.ReplaceSel("");
m_wndMeterBar.SetText("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", 0, 0);
m_wndMeterBar.SetText("", 1, 0);
m_wndMeterBar.GetProgressCtrl().SetPos(0);
//m_reqCtlEdit.Clear();
//m_resCtlEdit.Clear();
//m_resBodyCtlEdit.Clear();
}
void Chttp_clientDlg::OnBnClickedButtonClear()
{
// TODO: <20>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD>ӿؼ<D3BF>֪ͨ<CDA8><D6AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
ClearWinText();
}
void Chttp_clientDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
// TODO: <20>ڴ˴<DAB4><CBB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (m_wndMeterBar.GetSafeHwnd()) {
int aWidths[3] = {cx / 3, cx / 3, -1};
m_wndMeterBar.SetParts(3, aWidths);
m_wndMeterBar.SetText("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", 0, 0);
m_wndMeterBar.SetText("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>2", 1, 0); //SBT_NOBORDERS);
m_wndMeterBar.SetText("", 2, SBT_NOBORDERS);
m_wndMeterBar.SendMessage(WM_SIZE, nType, MAKELONG(cy, cx));
}
}
int Chttp_clientDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialog::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: <20>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ר<EFBFBD>õĴ<C3B5><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
return 0;
}