📄 wcehttpdlg.cpp
字号:
// WceHttpDlg.cpp : implementation file
//
#include "stdafx.h"
#include "WceHttp.h"
#include "WceHttpDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CWceHttpDlg dialog
CWceHttpDlg::CWceHttpDlg(CWnd* pParent /*=NULL*/)
: CDialog(CWceHttpDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CWceHttpDlg)
m_sURL = _T("http://alexg-d2/Soap.cpp");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CWceHttpDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CWceHttpDlg)
DDX_Text(pDX, IDC_EDIT1, m_sURL);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CWceHttpDlg, CDialog)
//{{AFX_MSG_MAP(CWceHttpDlg)
ON_BN_CLICKED(IDC_BUTTON1, OnGo)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CWceHttpDlg message handlers
BOOL CWceHttpDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
CenterWindow(GetDesktopWindow()); // center to the hpc screen
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CWceHttpDlg::OnGo()
{
UpdateData();
TCHAR szServer[1024];
TCHAR szPath[1024];
URL_COMPONENTS crackedURL;
memset(&crackedURL, 0, sizeof(crackedURL));
crackedURL.dwStructSize = sizeof(crackedURL);
crackedURL.lpszHostName = szServer;
crackedURL.dwHostNameLength = 1024;
crackedURL.lpszUrlPath = szPath;
crackedURL.dwUrlPathLength = 1024;
if(!InternetCrackUrl(L"http://erann-d/rmsmanager", 0, 0, &crackedURL))
{
TRACE(_T("Cannot crack URL: %lu\n"),GetLastError());
return;
}
HINTERNET hOpen = InternetOpen (L"WceHttp", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if ( !hOpen )
{
AfxMessageBox(L"Failed to open WinInet");
return;
}
CString sHttpHeader = L"Accept: application/zip,text/*\r\n";
// sHttpHeader += L"\"\0";
HINTERNET hOpenUrl = InternetOpenUrl(
hOpen,m_sURL,
// sHttpHeader,sHttpHeader.GetLength(),
NULL,0,
INTERNET_FLAG_PRAGMA_NOCACHE|INTERNET_FLAG_KEEP_CONNECTION,
0);
if ( !hOpenUrl )
{
DWORD dwErr = GetLastError();
TCHAR szBuffer[4096];
DWORD dwBufferLength = sizeof(szBuffer);
BOOL bRes = InternetGetLastResponseInfo(&dwErr, szBuffer, &dwBufferLength);
AfxMessageBox(L"Failed to open WinInet");
InternetCloseHandle(hOpen);
return;
}
char szBuffer[4096];
DWORD dwNumberOfBytesRead = 0;
TCHAR wszTmp[4097];
while ( InternetReadFile(hOpenUrl, szBuffer, 4096,&dwNumberOfBytesRead) && dwNumberOfBytesRead )
{
memset(wszTmp,0,sizeof(wszTmp));
MultiByteToWideChar(CP_ACP,0,szBuffer,dwNumberOfBytesRead,wszTmp,sizeof(wszTmp));
// TRACE(L"%s\n",wszTmp);
}
InternetCloseHandle(hOpenUrl);
InternetCloseHandle(hOpen);
}
void CWceHttpDlg::OnButton2()
{
UpdateData();
CString sInfo;
DWORD dwConnStateFlags = 0;
BOOL bRes = InternetGetConnectedState(&dwConnStateFlags,0);
if ( bRes )
{
sInfo.Format(L"ConnState = %X",dwConnStateFlags);
AfxMessageBox(sInfo);
}
HINTERNET hGETRequest;
DWORD dwFlags = INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_KEEP_CONNECTION |
INTERNET_FLAG_IGNORE_CERT_CN_INVALID|INTERNET_FLAG_IGNORE_CERT_DATE_INVALID|
INTERNET_FLAG_PRAGMA_NOCACHE;
LPTSTR pszAcceptTypes [] = {TEXT("text/*"), NULL};
TCHAR szServer [1024];
TCHAR szEndpoint [1024];
URL_COMPONENTS crackedURL;
int nPort;
HINTERNET hConnect;
HINTERNET hOpen;
CString sHTTPHeader;
if (m_sURL.IsEmpty ())
return;
//Crack URL ...
ZeroMemory (& crackedURL, sizeof (URL_COMPONENTS));
crackedURL.dwStructSize = sizeof (URL_COMPONENTS);
crackedURL.lpszHostName = szServer;
crackedURL.dwHostNameLength = 1024;
crackedURL.lpszUrlPath = szEndpoint;
crackedURL.dwUrlPathLength = 1024;
InternetCrackUrl (m_sURL, 0, 0, &crackedURL);
nPort = crackedURL.nPort;
hOpen = InternetOpen (L"WceHttp", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if ( !hOpen )
{
AfxMessageBox(L"Failed to open WinInet");
return;
}
hConnect = InternetConnect (hOpen, szServer, nPort, L"", L"", INTERNET_SERVICE_HTTP, 0, 0);
if ( !hConnect )
{
sInfo.Format(L"InternetConnect failed: %lu", GetLastError ());
AfxMessageBox(sInfo);
return;
}
// Open an HTTP request handle...
hGETRequest = HttpOpenRequest (hConnect, L"GET", szEndpoint, NULL, NULL, (LPCTSTR*) pszAcceptTypes, dwFlags, 0);
if ( !hGETRequest )
{
sInfo.Format(L"HttpOpenRequest failed: %lu", GetLastError ());
AfxMessageBox(sInfo);
InternetCloseHandle (hConnect);
return;
}
// send the request...
sHTTPHeader = L"Content-Type: text/*\r\n";
if (! HttpSendRequest (hGETRequest, (LPCTSTR) sHTTPHeader, sHTTPHeader.GetLength (), NULL, 0))
{
sInfo.Format(L"HttpSendRequest failed: %lu", GetLastError ());
AfxMessageBox(sInfo);
InternetCloseHandle (hGETRequest);
InternetCloseHandle (hConnect);
return;
}
DWORD dwNumberOfBytesAvailable = 0;
if ( InternetQueryDataAvailable(hGETRequest,&dwNumberOfBytesAvailable,0,0) )
{
sInfo.Format(L"File size is : %lu",dwNumberOfBytesAvailable);
AfxMessageBox(sInfo);
}
char szBuffer[4096];
DWORD dwNumberOfBytesRead = 0;
TCHAR wszTmp[4097];
int i = 0;
DWORD dwTotal = 0;
while ( InternetReadFile(hGETRequest, szBuffer, 4096,&dwNumberOfBytesRead) && dwNumberOfBytesRead )
{
memset(wszTmp,0,sizeof(wszTmp));
MultiByteToWideChar(CP_ACP,0,szBuffer,dwNumberOfBytesRead,wszTmp,sizeof(wszTmp));
i++;
dwTotal += dwNumberOfBytesRead;
}
sInfo.Format(L"Read %d block(s) - %lu byte(s)",i,dwTotal);
AfxMessageBox(sInfo);
InternetCloseHandle (hGETRequest);
InternetCloseHandle (hConnect);
InternetCloseHandle (hOpen);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -