HTTP Abfrage von Plapperkatze am 31.Januar 2006 um 22:17 zurück zur Kategorie "Tutorials"
#include <windows.h>
#include <winsock2.h>
#include <stdio.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
const char szAppName[] = "port 80 http abfrage";
HWND hEdit;
long rc;
SOCKET s;
SOCKADDR_IN addr;
char buf[256];
WSADATA wsa;
void myprint(char *txt)
{
int len = GetWindowTextLength(hEdit);
char* buf;
buf = (char*)GlobalAlloc(GPTR, len + strlen(txt) + 1);
if(!len)buf[0]='\0';
GetWindowText(hEdit,buf,len);
strcat(buf,txt);
SetWindowText(hEdit,buf);
GlobalFree(buf);
}
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
MSG msg;
HWND hWnd;
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wc.lpszClassName = szAppName;
wc.lpszMenuName = NULL;
RegisterClass(&wc);
hWnd = CreateWindow( szAppName,
szAppName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hWnd, iCmdShow);
UpdateWindow(hWnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
{
RECT r;
GetClientRect(hWnd,&r);
hEdit = CreateWindowEx(WS_EX_CLIENTEDGE,
"EDIT",
"",
WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_MULTILINE,
0,
0,
r.right-r.left,
r.bottom-r.top,
hWnd,
NULL,
GetModuleHandle(NULL),
NULL);
if(hEdit == NULL)MessageBox(hWnd, "Could not create edit box", "Error", MB_OK | MB_ICONERROR);
if(WSAStartup(MAKEWORD(2,0),&wsa)!=0)
{
MessageBox(hWnd, "Could not Startup WSA", "Error", MB_OK | MB_ICONERROR);
PostQuitMessage(0);
}
s=socket(AF_INET,SOCK_STREAM,0);
if(s==INVALID_SOCKET)
{
WSACleanup();
MessageBox(hWnd,"Could not create socket","Error",MB_OK|MB_ICONERROR);
PostQuitMessage(0);
}
memset(&addr,0,sizeof(SOCKADDR_IN)); // auf 0 setzen
addr.sin_family=AF_INET;
addr.sin_port=htons(80); // port
HOSTENT* he;
he=gethostbyname("www.dead-men.de");
memcpy(&(addr.sin_addr),he->h_addr_list[0],4);
rc=connect(s,(SOCKADDR*)&addr,sizeof(SOCKADDR));
if(rc==SOCKET_ERROR)
{
closesocket(s);
WSACleanup();
MessageBox(hWnd, "Could not connect", "Error", MB_OK | MB_ICONERROR);
PostQuitMessage(0);
}
myprint("connected!\n");
sprintf(buf,"GET / HTTP/1.1\nHost: www.dead-men.de\n\r\n\r");
rc=send(s,buf,strlen(buf),0);
if(rc==SOCKET_ERROR)
{
closesocket(s);
WSACleanup();
MessageBox(hWnd, "Could not send", "Error", MB_OK | MB_ICONERROR);
PostQuitMessage(0);
}
do
{
rc=recv(s,buf,255,0);
buf[rc]='\0';
myprint(buf);
}
while(rc!=0&&rc!=SOCKET_ERROR);
}
case WM_SIZE:
{
RECT r;
GetClientRect(hWnd,&r);
MoveWindow(hEdit,0,0,r.right-r.left,r.bottom-r.top,TRUE);
return 0;
}
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
}
return DefWindowProc(hWnd, message, wParam, lParam);
} |
grüsse, die plapperkatz
zurück zur Kategorie "Tutorials" [0 Kommentare]
|