Writing your own HTTP Client


Requirements


Program 1
#include <windows.h>
#include <wininet.h>
#include <stdio.h>
char aa[100];char bb[100];char cc[200];
void *m_hSession;
void abc(char *p)
{
  FILE *fp=fopen("z.txt","a+");
  fprintf(fp,"%s\n",p);
  fclose(fp);
}
long _stdcall zzz (HWND,UINT,WPARAM,LPARAM);
WNDCLASS a;HWND b;MSG c;
int _stdcall WinMain(HINSTANCE i,HINSTANCE j,char *k,int l)
{
  a.lpszClassName="a1";
  a.hInstance=i;
  a.lpfnWndProc=zzz;
  a.hbrBackground=GetStockObject(WHITE_BRUSH);
  RegisterClass(&a);
  b=CreateWindow("a1","aaa",WS_OVERLAPPEDWINDOW,1,1,10,20,0,0,i,0);
  ShowWindow(b,3);
  while ( GetMessage(&c,0,0,0) )
     DispatchMessage(&c);
  return 1;
}
long _stdcall zzz (HWND w,UINT x,WPARAM y,LPARAM z)
{
  if ( x == WM_LBUTTONDOWN)
  {
    m_hSession=::InternetOpen("MSDN ",0,0,
               INTERNET_INVALID_PORT_NUMBER,0);
    sprintf(aa,"InternetOpen m_hSession %p",m_hSession);
    MessageBox(0,aa,aa,0);
    HINTERNET hConnect=InternetConnect(m_hSession,"www.microsoft.com",
    INTERNET_INVALID_PORT_NUMBER,"","",INTERNET_SERVICE_HTTP,0,0) ;
    sprintf(aa,"InternetConnect hConnect %p",hConnect);
    MessageBox(0,aa,aa,0);
    HINTERNET hHttpFile=HttpOpenRequest(hConnect,"GET","",
    HTTP_VERSION,  NULL,0,INTERNET_FLAG_DONT_CACHE,0) ;
    sprintf(aa,"HttpOpenRequest hHttpFile %p",hHttpFile);
    MessageBox(0,aa,aa,0);
    BOOL bSendRequest = ::HttpSendRequest(hHttpFile, NULL, 0, 0, 0);
    sprintf(aa,"HttpSendRequest %p bSendRequest %d",
                        HttpSendRequest,bSendRequest);
    MessageBox(0,aa,aa,0);
    char bufQuery[32] ;DWORD dwFileSize ;
    DWORD dwLengthBufQuery = sizeof (bufQuery);
    BOOL bQuery = ::HttpQueryInfo(hHttpFile,HTTP_QUERY_CONTENT_LENGTH,
                                    bufQuery, &dwLengthBufQuery,NULL) ;
    sprintf(aa,"HttpQueryInfo hHttpFile %p bQuery %d bufQuery %ld",
                                   hHttpFile,bQuery,atol(bufQuery));
    MessageBox(0,aa,aa,0);
    dwFileSize = (DWORD)atol(bufQuery) ;
    char * m_buffer = new char[dwFileSize+1] ;
    DWORD dwBytesRead ;
    BOOL bRead=InternetReadFile(hHttpFile,m_buffer,
                                dwFileSize+1,&dwBytesRead);
    sprintf(aa,"InternetReadFile dwBytesRead %ld bRead %d",
                                        dwBytesRead,bRead);
    MessageBox(0,aa,aa,0);
    abc(m_buffer);
    MessageBox(0,"all over","hi",0);
  }
  if ( x == WM_DESTROY)
      PostQuitMessage(0);
  return DefWindowProc(w,x,y,z);
}

Program 2

#include <windows.h>
#include <urlhlink.h>
#include <urlmon.h>
long _stdcall zzz (HWND,UINT,WPARAM,LPARAM);
WNDCLASS a;HWND b;MSG c;
int _stdcall WinMain(HINSTANCE i,HINSTANCE j,char *k,int l)
{
  a.lpszClassName="a1";
  a.hInstance=i;
  a.lpfnWndProc=zzz;
  a.hbrBackground=GetStockObject(WHITE_BRUSH);
  RegisterClass(&a);
  b=CreateWindow("a1","aaa",WS_OVERLAPPEDWINDOW,1,1,10,20,0,0,i,0);
  ShowWindow(b,3);
  while ( GetMessage(&c,0,0,0) )
    DispatchMessage(&c);
  return 1;
}
long _stdcall zzz (HWND w,UINT x,WPARAM y,LPARAM z)
{
  if ( x == WM_LBUTTONDOWN)
  {
     HlinkSimpleNavigateToString(L"http://www.microsoft.com",
                                                0,0,0,0,0,0,0);
     MessageBox(0,"all over","hi",0);
  }
  if ( x == WM_DESTROY)
      PostQuitMessage(0);
  return DefWindowProc(w,x,y,z);
}

Program 3
#define INITGUID
#include <windows.h>
#include <urlhlink.h>
#include <urlmon.h>
#include <exdisp.h>
#include <stdio.h>
#include <hlink.h>
long _stdcall zzz (HWND,UINT,WPARAM,LPARAM);
WNDCLASS a;HWND b;MSG c;char aa[100];
int _stdcall WinMain(HINSTANCE i,HINSTANCE j,char *k,int l)
{
  a.lpszClassName="a1";
  a.hInstance=i;
  a.lpfnWndProc=zzz;
  a.hbrBackground=GetStockObject(WHITE_BRUSH);
  RegisterClass(&a);
  b=CreateWindow("a1","aaa",WS_OVERLAPPEDWINDOW,1,1,10,20,0,0,i,0);
  ShowWindow(b,3);
  while ( GetMessage(&c,0,0,0) )
    DispatchMessage(&c);
  return 1;
}
HRESULT h; IHlink *p;
long _stdcall zzz (HWND w,UINT x,WPARAM y,LPARAM z)
{
  if ( x == WM_LBUTTONDOWN)
  {
    h=HlinkCreateFromString (L"http://www.microsoft.com",0,0,
                               0,0,0,IID_IUnknown,(void **)&p);
    sprintf(aa,"h=%ld p=%p",h,p);
    MessageBox(0,aa,aa,0);
    MessageBox(0,"all over","hi",0);
  }
  if ( x == WM_DESTROY)
    PostQuitMessage(0);
  return DefWindowProc(w,x,y,z);
}

Program 4
#include <windows.h>
#include <urlhlink.h>
#include <urlmon.h>

class ccc : public IBindStatusCallback
{
  public:
  STDMETHODIMP   QueryInterface(REFIID riid ,void ** ppv)
  { return E_NOINTERFACE;}
  STDMETHODIMP_(ULONG)    AddRef()
  { return 0; }
  STDMETHODIMP_(ULONG)    Release()
  {  return 0;}
  STDMETHODIMP    OnStartBinding(DWORD grfBSCOption, IBinding* pbinding)
  { return 0; }
  STDMETHODIMP    GetPriority(LONG* pnPriority)
  { return 0; }
  STDMETHODIMP    OnLowResource(DWORD dwReserved)
  { return 0; }
  STDMETHOD(OnProgress)(ULONG ulProgress,ULONG ulProgressMax,
                        ULONG ulStatusCode,LPCWSTR szStatusText)
  { return 0; }
  STDMETHODIMP    OnStopBinding(HRESULT hrResult, LPCWSTR szError)
  { return 0; }
  STDMETHODIMP    GetBindInfo(DWORD* pgrfBINDF, BINDINFO* pbindinfo)
  { return 0; }
  STDMETHODIMP    OnDataAvailable(DWORD grfBSCF, DWORD dwSize,
                                       FORMATETC *pfmtetc,STGMEDIUM* pstgmed)
  { return 0; }
  STDMETHODIMP    OnObjectAvailable(REFIID riid, IUnknown* punk)
  { return 0; }
};

long _stdcall zzz (HWND,UINT,WPARAM,LPARAM);
WNDCLASS a;HWND b;MSG c;
int _stdcall WinMain(HINSTANCE i,HINSTANCE j,char *k,int l)
{
  a.lpszClassName="a1";
  a.hInstance=i;
  a.lpfnWndProc=zzz;
  a.hbrBackground=GetStockObject(WHITE_BRUSH);
  RegisterClass(&a);
  b=CreateWindow("a1","aaa",WS_OVERLAPPEDWINDOW,1,1,10,20,0,0,i,0);
  ShowWindow(b,3);
  while ( GetMessage(&c,0,0,0) )
     DispatchMessage(&c);
  return 1;
}
long _stdcall zzz (HWND w,UINT x,WPARAM y,LPARAM z)
{
  if ( x == WM_LBUTTONDOWN)
  {
    ccc *d = new ccc;
    URLDownloadToFile(0,"http://www.microsoft.com/default.htm",
                                              "z.txt",0,d);
    MessageBox(0,"all over","hi",0);
  }
  if ( x == WM_DESTROY)
    PostQuitMessage(0);
  return DefWindowProc(w,x,y,z);
}

Program 5
#define INITGUID
#include <windows.h>
#include <urlhlink.h>
#include <urlmon.h>
#include <exdisp.h>
#include <stdio.h>
long _stdcall zzz (HWND,UINT,WPARAM,LPARAM);
WNDCLASS a;HWND b;MSG c;char aa[100];
int _stdcall WinMain(HINSTANCE i,HINSTANCE j,char *k,int l)
{
  a.lpszClassName="a1";
  a.hInstance=i;
  a.lpfnWndProc=zzz;
  a.hbrBackground=GetStockObject(WHITE_BRUSH);
  RegisterClass(&a);
  b=CreateWindow("a1","aaa",WS_OVERLAPPEDWINDOW,1,1,10,20,0,0,i,0);
  ShowWindow(b,3);
  while ( GetMessage(&c,0,0,0) )
        DispatchMessage(&c);
  return 1;
}
IWebBrowser *m_pIWebBrowser;HRESULT h;

long _stdcall zzz (HWND w,UINT x,WPARAM y,LPARAM z)
{
   if ( x == WM_LBUTTONDOWN)
   {
     h=CoCreateInstance(CLSID_InternetExplorer,0,0,IID_IUnknown,
                                 (void **)  &m_pIWebBrowser);
     sprintf(aa,"CoCreateInstance h=%ld m_pIWebBrowser %p",h,m_pIWebBrowser);
     MessageBox(0,aa,aa,0);
     MessageBox(0,"all over","hi",0);
   }
   if ( x == WM_DESTROY)
        PostQuitMessage(0);
   return DefWindowProc(w,x,y,z);
}


Have any suggestions, comments, ideas, cracked code, feedback. Feel free to get in touch with us.

Move back to the Vijay Mukhi's Technology Cornucopia Page to learn more about the other new Internet Technologies.


Vijay Mukhi's Computer Institute
VMCI, B-13, Everest Building, Tardeo, Bombay 400 034, India
E-mail:vmukhi@giasbm01.vsnl.net.in Tel : 91-22-496 4335 /6/7/8/9 Fax : 91-22-307 28 59
http://www.vijaymukhi.com