HHTP CLIENT EXAMPLE

Chia sẻ: TRẦN THỊ THANH HẰNG | Ngày: | Loại File: PDF | Số trang:1

0
57
lượt xem
10
download

HHTP CLIENT EXAMPLE

Mô tả tài liệu
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

HHTP CLIENT EXAMPLE *HTTP RFC SP: space (khoang trang) CRLF: \r\n REQUEST SYNTAX: Request = Request-Line *( general-header | request-header | entity-header ) CRLF [ message-body ]

Chủ đề:
Lưu

Nội dung Text: HHTP CLIENT EXAMPLE

  1. /* strcat(buffer,"HOST: HHTP CLIENT EXAMPLE ");strcat(buffer,host);strcat(buffer,"\r\n"); *HTTP RFC strcat(buffer,"CONNECTION: CLOSE\r\n\r\n"); SP: space (khoang trang) } CRLF: \r\n REQUEST SYNTAX: u_long LookupAddress(const char* pcHost); void error(char * msg); Request = Request-Line *( general-header int main(int argc, char* argv[]) | request-header { | entity-header ) WSADATA wsaData; CRLF WSAStartup(MAKEWORD(1,1),&wsaData); [ message-body ] /////////////////////////// printf("Client..."); SOCKET wsk=socket(AF_INET,SOCK_STREAM,0); Request-Line = Method SP Request-URI SP HTTP-Version CRLF struct hostent *hostname=gethostbyname(SERVER_ADDR); Method = "OPTIONS" ///////////////////////////// | "GET" struct sockaddr_in server_addr; | "HEAD" | "POST" server_addr.sin_family = AF_INET; | "PUT" server_addr.sin_addr.S_un.S_addr=LookupAddre | "DELETE" ss(SERVER_ADDR); | "TRACE" server_addr.sin_port = htons(MY_PORT); | extension-method if (connect(wsk,(struct sockaddr*)&server_addr,sizeof(server_addr)) < 0) Request-URI = "*" | absoluteURI | error("ERROR connecting"); abs_path printf("connected!\n"); //Send REQUEST Ex : absoluteURI: char buffer[1024];int n; http://www.w3.org/pub/WWW/TheProject.html makeRequestStr(buffer,URL,SERVER_ADDR); abs_path: send(wsk,buffer,strlen(buffer),0); /pub/WWW/TheProject.html //GET RESPONSE printf("Http content: "); HTTP-Version = "HTTP/1.1" n = recv(wsk,buffer,1024,0); | "HTTP/1.0" buffer[n]='\0'; printf("%s\n",buffer); EX: closesocket(wsk); GET /////////////////////////// http://www.w3.org/pub/WWW/TheProject.html HTTP/1.1 WSACleanup(); Host: www.w3.org return 0; or: } GET /pub/WWW/TheProject.html HTTP/1.1 void error(char * msg) */ { printf("%s\n",msg); #include exit(0); #include } ////// #define MY_PORT 80 #define SERVER_ADDR "www3.tuoitre.com.vn" u_long LookupAddress(const char* pcHost) #define URL { "http://www3.tuoitre.com.vn/transweb/tygia.htm" u_long nRemoteAddr = inet_addr(pcHost); if (nRemoteAddr == INADDR_NONE) { void makeRequestStr(char * buffer,const char * // pcHost isn't a dotted IP, so resolve it url, const char * host) through DNS { hostent* pHE = gethostbyname(pcHost); /* if (pHE == 0) { Example: return INADDR_NONE; s = "GET } http://www3.tuoitre.com.vn/transweb/tygia.htm nRemoteAddr = *((u_long*)pHE- HTTP/1.1\r\n"; >h_addr_list[0]); s += "Host: www3.tuoitre.com.vn\r\n"; } s += "Connection: Close\r\n\r\n"; */ return nRemoteAddr; strcpy(buffer,"GET ");strcat(buffer,url); } strcat(buffer," HTTP/1.1\r\n");
Đồng bộ tài khoản