HTTP Client Program

From Web (09.06.1997)

Table of Contents

1. A Visual C++ Project

Create a Project (Application) in Visual C++ (we have used MSVC 4.0)

The files in the project

  • a1.cpp
  • wsock32.lib

1.1. First Step - a simple window

//a1.cpp

#include <windows.h>
#include <stdio.h>
void abc(char *p)
{
        FILE *fp=fopen("z.txt","a+");
        fprintf(fp,"%s\n",p);
        fclose(fp);
}
WNDCLASS a;HWND b;MSG c;

long _stdcall zzz (HWND,UINT,WPARAM,LPARAM);
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)
        {
                MessageBox(0,"hi","hi",0);
        }
        if ( x == WM_DESTROY)
                PostQuitMessage(0);
        return DefWindowProc(w,x,y,z);
}

1.2. Connect to IP

//a1.cpp

#include <windows.h>
#include <stdio.h>
void abc(char *p)
{
        FILE *fp=fopen("z.txt","a+");
        fprintf(fp,"%s\n",p);
        fclose(fp);
}
WNDCLASS a;HWND b;MSG c;int d;char aa[100];
WSAData ws; SOCKET s;struct sockaddr_in A;
long _stdcall zzz (HWND,UINT,WPARAM,LPARAM);
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)
        {
                WSAStartup(0x0101,&ws);
                s = socket(AF_INET,SOCK_STREAM,0);
                A.sin_family = AF_INET;
                A.sin_port = htons(80);
                A.sin_addr.s_addr = inet_addr("198.105.232.5");
                d=connect(s,(struct sockaddr *)&A,sizeof(A));
                sprintf(aa,"d = %ld",d);
                abc(aa);
                MessageBox(0,"all","over",0);
        }
        if ( x == WM_DESTROY)
                PostQuitMessage(0);
        return DefWindowProc(w,x,y,z);
}

1.3. Get default.htm

//a1.cpp

#include <windows.h>
#include <stdio.h>
void abc(char *p)
{
        FILE *fp=fopen("z.txt","a+");
        fprintf(fp,"%s\n",p);
        fclose(fp);
}
WNDCLASS a;HWND b;MSG c;int d;char aa[100];char bb[100];char cc[8000];
WSAData ws; SOCKET s;struct sockaddr_in A;int ii;
long _stdcall zzz (HWND,UINT,WPARAM,LPARAM);
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)
        {
                WSAStartup(0x0101,&ws);
                s = socket(AF_INET,SOCK_STREAM,0);
                A.sin_family = AF_INET;
                A.sin_port = htons(80);
                A.sin_addr.s_addr = inet_addr("198.105.232.5");
                d=connect(s,(struct sockaddr *)&A,sizeof(A));
                sprintf(aa,"d = %ld",d);
                abc(aa);
                strcpy(bb,"GET default.htm");
                d=send(s,bb,strlen(bb),0);
                sprintf(aa,"d=%ld",d);
                abc(aa);
                ii=1;
                while(ii !=0)
                {
                        ii=recv(s,cc,100,0);
                        abc(cc);
                }
                MessageBox(0,"all","over",0);
        }
        if ( x == WM_DESTROY)
                PostQuitMessage(0);
        return DefWindowProc(w,x,y,z);
}

1.4. Receive datas from server

//a1.cpp

#include <windows.h>
#include <stdio.h>
void abc(char *p)
{
        FILE *fp=fopen("z.txt","a+");
        fprintf(fp,"%s\n",p);
        fclose(fp);
}
WNDCLASS a;HWND b;MSG c;int d;char aa[100];char bb[100];
WSAData ws; SOCKET s;struct sockaddr_in A;int ii;long gg;
char cc[100];
long _stdcall zzz (HWND,UINT,WPARAM,LPARAM);
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)
{
        gg=WSAStartup(0x0101,&ws);
        sprintf(aa,"WSAStartup ..%ld",gg);
        MessageBox(0,aa,aa,0);
        abc(aa);
        s = socket(AF_INET,SOCK_STREAM,0);
        sprintf(aa,"socket s = %ld",s);
        abc(aa);
        MessageBox(0,aa,aa,0);
        A.sin_family = AF_INET;
        A.sin_port = htons(80);
        A.sin_addr.s_addr = inet_addr("198.105.232.5");
        d=connect(s,(struct sockaddr *)&A,sizeof(A));
        sprintf(aa,"d = %ld",d);
        abc(aa);
        MessageBox(0,aa,aa,0);
        strcpy(bb,"GET default.htm\r\n");
        d=send(s,bb,strlen(bb),0);
        sprintf(aa,"d=%ld",d);
        abc(aa);
        ii=1;
        while(ii !=0)
        {
                strset(cc,' ');
                ii=recv(s,cc,sizeof(cc),0);
                abc(cc);
        }
        MessageBox(0,"all","over",0);
}
if ( x == WM_DESTROY)
      PostQuitMessage(0);
return DefWindowProc(w,x,y,z);
}

1.5. Send User-Agent

//a1.cpp

#include <windows.h>
#include <stdio.h>
void abc(char *p)
{
        FILE *fp=fopen("z.txt","a+");
        fprintf(fp,"%s\n",p);
        fclose(fp);
}
WNDCLASS a;HWND b;MSG c;int d;char aa[100];char bb[100];
WSAData ws; SOCKET s;struct sockaddr_in A;int ii;long gg;
char cc[100];
char hh[]="HTTP/1.0\r\n""User-Agent:AuthClient\r\n""Accept:*/*\r\n";

long _stdcall zzz (HWND,UINT,WPARAM,LPARAM);
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)
        {
                gg=WSAStartup(0x0101,&ws);
                sprintf(aa,"WSAStartup ..%ld",gg);
                MessageBox(0,aa,aa,0);
                abc(aa);
                s = socket(AF_INET,SOCK_STREAM,0);
                sprintf(aa,"socket s = %ld",s);
                abc(aa);
                MessageBox(0,aa,aa,0);
                A.sin_family = AF_INET;
                A.sin_port = htons(80);
                A.sin_addr.s_addr = inet_addr("198.105.232.5");
                d=connect(s,(struct sockaddr *)&A,sizeof(A));
                sprintf(aa,"d = %ld",d);
                abc(aa);
                MessageBox(0,aa,aa,0);
                strcpy(bb,"GET default.htm ");
                strcat(bb,hh);
                strcat(bb,"\r\n");
                d=send(s,bb,strlen(bb),0);
                sprintf(aa,"d=%ld",d);
                abc(aa);
                ii=1;
                while(ii !=0)
                {
                        strset(cc,' ');
                        ii=recv(s,cc,sizeof(cc),0);
                        abc(cc);
                }
                MessageBox(0,"all","over",0);
        }
        if ( x == WM_DESTROY)
                PostQuitMessage(0);
        return DefWindowProc(w,x,y,z);
}

1.6. Our HTTP-Client (remote IP)

//a1.cpp

#include <windows.h>
#include <stdio.h>
void abc(char *p)
{
        FILE *fp=fopen("z.txt","a+");
        fprintf(fp,"%s\n",p);
        fclose(fp);
}
WNDCLASS a;HWND b;MSG c;int d;char aa[100];char bb[100];char cc[8000];
WSAData ws; SOCKET s;struct sockaddr_in A;int ii;long gg;
char Headers[]="HTTP/1.0\r\n""User-Agent:AuthClient\r\n""Accept:*/*\r\n";
char R[8*1024];
long _stdcall zzz (HWND,UINT,WPARAM,LPARAM);
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)
        {
                gg=WSAStartup(0x0101,&ws);
                sprintf(aa,"WSAStartup ..%ld",gg);
                MessageBox(0,aa,aa,0);
                abc(aa);
                s = socket(AF_INET,SOCK_STREAM,0);
                sprintf(aa,"socket s = %ld",s);
                abc(aa);
                MessageBox(0,aa,aa,0);
                A.sin_family = AF_INET;
                A.sin_port = htons(80);
                A.sin_addr.s_addr = inet_addr("198.105.232.5");
                d=connect(s,(struct sockaddr *)&A,sizeof(A));
                sprintf(aa,"d = %ld",d);
                abc(aa);
                MessageBox(0,aa,aa,0);
                strcpy(bb,"GET");strcat(bb," ");strcat(bb,"default.htm");
                strcat(bb," ");strcat(bb,Headers);strcat(bb,"\r\n");
                d=send(s,bb,strlen(bb),0);
                sprintf(aa,"d=%ld",d);
                abc(aa);
                ii=1;
                while(ii !=0)
                {
                        ii=recv(s,R,sizeof(R),0);
                        abc(R);
                }
                MessageBox(0,"all","over",0);
        }
        if ( x == WM_DESTROY)
                PostQuitMessage(0);
        return DefWindowProc(w,x,y,z);
}

1.7. Our HTTP-Client (local IP)

//a1.cpp

#include <windows.h>
#include <stdio.h>
void abc(char *p)
{
  FILE *fp=fopen("z.txt","a+");
  fprintf(fp,"%s\n",p);
  fclose(fp);
}
WNDCLASS a;HWND b;MSG c;int d;char aa[100];char bb[100];
WSAData ws; SOCKET s;struct sockaddr_in A;int ii;long gg;
char cc[100];
char hh[]="HTTP/1.0\r\n""User-Agent:AuthClient\r\n""Accept:*/*\r\n";

long _stdcall zzz (HWND,UINT,WPARAM,LPARAM);
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)
  {
    gg=WSAStartup(0x0101,&ws);
    sprintf(aa,"WSAStartup ..%ld",gg);
    MessageBox(0,aa,aa,0);
    abc(aa);
    s = socket(AF_INET,SOCK_STREAM,0);
    sprintf(aa,"socket s = %ld",s);
    abc(aa);
    MessageBox(0,aa,aa,0);
    A.sin_family = AF_INET;
    A.sin_port = htons(80);
    A.sin_addr.s_addr = inet_addr("127.0.0.1");
    d=connect(s,(struct sockaddr *)&A,sizeof(A));
    sprintf(aa,"d = %ld",d);
    abc(aa);
    MessageBox(0,aa,aa,0);
    strcpy(bb,"GET default.htm ");
    strcat(bb,hh);
    strcat(bb,"\r\n");
    d=send(s,bb,strlen(bb),0);
    sprintf(aa,"d=%ld",d);
    abc(aa);
    ii=1;
    while(ii !=0)
    {
          strset(cc,' ');
          ii=recv(s,cc,sizeof(cc),0);
          abc(cc);
    
    }
    MessageBox(0,"all","over",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.neca.com/~vmis