// Meteorsis_HTTP_sample_VC6Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "Meteorsis_HTTP_sample_VC6.h"
#include "Meteorsis_HTTP_sample_VC6Dlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMeteors_HTTP_sample_VC6Dlg dialog

CMeteorsis_HTTP_sample_VC6Dlg::CMeteorsis_HTTP_sample_VC6Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMeteorsis_HTTP_sample_VC6Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMeteorsis_HTTP_sample_VC6Dlg)
	m_txtusername = _T("");
	m_txtpassword = _T("");
	m_txtcontent = _T("");
	m_txtsendsmsres = _T("");
	m_txtgetsmsdstatusres = _T("");
	m_txtgetusercreditres = _T("");
	m_txtgetserverqueueres = _T("");
	m_txtlangeng = _T("");
	m_txtdos = _T("");
	m_txtsenderid = _T("");
	m_txtsmsdid = _T("");
	m_txtrecipient = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDI_ICON1);
}

void CMeteorsis_HTTP_sample_VC6Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMeteorsis_HTTP_sample_VC6Dlg)
	DDX_Text(pDX, IDC_EDIT1, m_txtusername);
	DDX_Text(pDX, IDC_EDIT2, m_txtpassword);
	DDX_Text(pDX, IDC_EDIT4, m_txtcontent);
	DDX_Text(pDX, IDC_EDIT8, m_txtsendsmsres);
	DDX_Text(pDX, IDC_EDIT10, m_txtgetsmsdstatusres);
	DDX_Text(pDX, IDC_EDIT11, m_txtgetusercreditres);
	DDX_Text(pDX, IDC_EDIT12, m_txtgetserverqueueres);
	DDX_Text(pDX, IDC_EDIT5, m_txtlangeng);
	DDX_Text(pDX, IDC_EDIT6, m_txtdos);
	DDX_Text(pDX, IDC_EDIT7, m_txtsenderid);
	DDX_Text(pDX, IDC_EDIT9, m_txtsmsdid);
	DDX_Text(pDX, IDC_EDIT3, m_txtrecipient);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMeteorsis_HTTP_sample_VC6Dlg, CDialog)
	//{{AFX_MSG_MAP(CMeteorsis_HTTP_sample_VC6Dlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMeteors_HTTP_sample_VC6Dlg message handlers

BOOL CMeteorsis_HTTP_sample_VC6Dlg::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
	
    // TODO: Add extra initialization here
    m_txtusername="";
    m_txtpassword="";
    m_txtrecipient="85299887766,85299887765";
    m_txtcontent="Meteorsis SMS Testing 123, 慧昇資訊系統有限公司測試"; //004D006500740065006F0072007300200053004D0053002000540065007300740069006E00670020003100320033002C0020616766078CC78A0A7CFB7D7167099650516C53F86E2C8A66
    m_txtlangeng="0";
    m_txtdos="now";
    m_txtsenderid="meteorsis";
    m_txtsmsdid="120000";
    m_txtsendsmsres="";
    m_txtgetsmsdstatusres="";
    m_txtgetusercreditres="";
    m_txtgetserverqueueres="";
    UpdateData(false);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CMeteorsis_HTTP_sample_VC6Dlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		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;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMeteorsis_HTTP_sample_VC6Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CMeteorsis_HTTP_sample_VC6Dlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	CInternetSession session("My Session");
	CHttpConnection* pServer = NULL;
	CHttpFile* pFile = NULL;
	CString strServerName="";
	CString l_url="";
	CString l_unicodecontent;
	INTERNET_PORT nPort;
	DWORD dwRet=0;
	char* szBuff = new char[500];
	
	try
	{
	    UpdateData(true);
		strServerName="www.meteorsis.com";
		if (m_txtlangeng=='0') //unicode
		{
			l_unicodecontent=Text2Unicode(m_txtcontent);
		}
		else
		{
			l_unicodecontent=m_txtcontent;
		}

		l_url="/misweb/f_sendsms.aspx?username=" + m_txtusername + "&password=" + m_txtpassword + "&content=" + l_unicodecontent + "&recipient=" + m_txtrecipient + "&langeng=" + m_txtlangeng + "&dos=" + m_txtdos + "&senderid=" + m_txtsenderid;
		nPort=80;
		pServer = session.GetHttpConnection(strServerName, nPort);
	    pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST,l_url);

	   pFile->SendRequest();
	   pFile->QueryInfoStatusCode(dwRet);

	   if (dwRet == 200)
	   {
		   UINT nRead = pFile->Read(szBuff, 500-1);
		   szBuff[nRead]='\0';
		   m_txtsendsmsres=szBuff;
	   }
	   delete pFile;
	   delete pServer;
	   delete [] szBuff;
	   UpdateData(false);
	}

	catch (CInternetException pEx)
	{
	   //catch errors from WinInet
	}
	session.Close();
}

void CMeteorsis_HTTP_sample_VC6Dlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	CInternetSession session("My Session");
	CHttpConnection* pServer = NULL;
	CHttpFile* pFile = NULL;
	CString strServerName="";
	CString l_url="";
	INTERNET_PORT nPort;
	DWORD dwRet=0;
	char* szBuff = new char[500];
	
	try
	{
	    UpdateData(true);
		strServerName="www.meteorsis.com";
		l_url="/misweb/f_getsmsdstatus.aspx?username=" + m_txtusername + "&password=" + m_txtpassword + "&smsdid=" + m_txtsmsdid;
		nPort=80;
		pServer = session.GetHttpConnection(strServerName, nPort);
	    pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST,l_url);

	   pFile->SendRequest();
	   pFile->QueryInfoStatusCode(dwRet);

	   if (dwRet == 200)
	   {
		   UINT nRead = pFile->Read(szBuff, 500-1);
		   szBuff[nRead]='\0';
		   m_txtgetsmsdstatusres=szBuff;
	   }
	   delete pFile;
	   delete pServer;
	   delete [] szBuff;
	   UpdateData(false);
	}

	catch (CInternetException pEx)
	{
	   //catch errors from WinInet
	}
	session.Close();
}

void CMeteorsis_HTTP_sample_VC6Dlg::OnButton3() 
{
	// TODO: Add your control notification handler code here
	CInternetSession session("My Session");
	CHttpConnection* pServer = NULL;
	CHttpFile* pFile = NULL;
	CString strServerName="";
	CString l_url="";
	INTERNET_PORT nPort;
	DWORD dwRet=0;
	char* szBuff = new char[500];

	try
	{
		UpdateData(true);
		strServerName="www.meteorsis.com";
		l_url="/misweb/f_getusercredit.aspx?username=" + m_txtusername + "&password=" + m_txtpassword;
		nPort=80;
		pServer = session.GetHttpConnection(strServerName, nPort);
	    pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST,l_url);

	   pFile->SendRequest();
	   pFile->QueryInfoStatusCode(dwRet);

	   if (dwRet == 200)
	   {
		   UINT nRead = pFile->Read(szBuff, 500-1);
		   szBuff[nRead]='\0';
		   m_txtgetusercreditres=szBuff;
	   }
	   delete pFile;
	   delete pServer;
	   delete [] szBuff;
	   UpdateData(false);
	}

	catch (CInternetException pEx)
	{
	   //catch errors from WinInet
	}
	session.Close();

}

void CMeteorsis_HTTP_sample_VC6Dlg::OnButton4() 
{
	// TODO: Add your control notification handler code here
	CInternetSession session("My Session");
	CHttpConnection* pServer = NULL;
	CHttpFile* pFile = NULL;
	CString strServerName="";
	CString l_url="";
	INTERNET_PORT nPort;
	DWORD dwRet=0;
	char* szBuff = new char[500];

	try
	{
		UpdateData(true);
		strServerName="www.meteorsis.com";
		l_url="/misweb/f_getserverqueue.aspx?username=" + m_txtusername + "&password=" + m_txtpassword;
		nPort=80;
		pServer = session.GetHttpConnection(strServerName, nPort);
	    pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST,l_url);

	   pFile->SendRequest();
	   pFile->QueryInfoStatusCode(dwRet);

	   if (dwRet == 200)
	   {
		   UINT nRead = pFile->Read(szBuff, 500-1);
		   szBuff[nRead]='\0';
		   m_txtgetserverqueueres=szBuff;
	   }
	   delete pFile;
	   delete pServer;
	   delete [] szBuff;
	   UpdateData(false);
	}

	catch (CInternetException pEx)
	{
	   //catch errors from WinInet
	}
	session.Close();
	
}

CString CMeteorsis_HTTP_sample_VC6Dlg::Text2Unicode(CString msg)
{
   CString retstr;
   
   char aa[500]="";
   int len=msg.GetLength();
   BSTR x=msg. AllocSysString();
   long num=*((long*)x-1);   
   for(int i=0;i<num;i+=1)
   {
    if(i>=len)
     break;
     char cc[10]="";
  if((*x>>8)==0)
  {
   sprintf(aa,"%s00%x",aa,*x);
     msg=msg.Right (msg.GetLength ()-1);
   
  }
  else
  {
   sprintf(aa,"%s%x",aa,*x);
   msg=msg.Right (msg.GetLength ()-2);
   i=i+1;
  }
  SysFreeString(x); 
 
  x=msg. AllocSysString();
   }
  SysFreeString(x);
   char bb[10]="";
   int len1=strlen(aa);
   char txt[500]="";
   if(len1%4!=0)
   {
    memcpy(txt,aa,(len1-3)); 
   }
   else
   {
    memcpy(txt,aa,len1);
   }
   if(num<=0x0f)
   {
    sprintf(bb,"0%x",num);
   }
   else
   {
    sprintf(bb,"%x",num);
   }
   //retstr.Format ("%s%s",bb,txt);
   retstr.Format ("%s",txt);
   retstr.MakeUpper ();
   int msglth=retstr.GetLength();  
   return retstr;
}

