Mercurial > hg > Members > nobuyasu > konoha
changeset 2:f85060c91817
add requet.k
author | Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 17 May 2012 21:37:39 +0900 |
parents | 68307ce6839d |
children | 15c4de0e2db2 |
files | http/request.k |
diffstat | 1 files changed, 86 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/http/request.k Thu May 17 21:37:39 2012 +0900 @@ -0,0 +1,86 @@ +using konoha.io.*; +using konoha.socket.*; + + +class HttpRequest() +{ + int port; + String method, uri; + String host, connection, charset, cacheControl, language; + Socket sock; + InputStream in; + OutputStream out; + HttpRequest() { + port = 80; + method = "GET"; + uri = "index.html"; + host = "google.co.jp"; + host = "google.co.jp"; + charset = "UTF-8"; + connection = "close"; + cacheControl = "no-cache"; + language = "en"; + } + + void setHost(String _host) { + host = _host; + } + + void connectToHost() { + sock = new Socket(host, port); + in = new sock.getInputStream(); + out = new sock.getOutputStream(); + } + + void sendRequest() { + out <<< method + "/" + uri + "HTTP/1.1" <<< EOL; + out <<< "HOST: " + host <<< EOL; + out <<< "Connection: " + connection <<< EOL; + out <<< "Accept-Charset: " + charset <<< EOL; + out <<< "Cache-Control: " + cacheControl <<< EOL; + out <<< "Accept-Language: " + language <<< EOL; + out <<< "" <<< EOL; + } + + void printResponse() { + while ( !in.isClosed() ) { + String ret = in.readLine(); + print ret; + } + } + + +} + + + +void main(String[] args) +{ + int port = 80; // port number of http protocol + String host = "dimolto.cr.ie.u-ryukyu.ac.jp"; + + Socket sock = new Socket(host, port); + InputStream in = sock.getInputStream(); + OutputStream out = sock.getOutputStream(); + + out <<< "GET /index.html HTTP/1.1" <<< EOL; + out <<< "Host: dimolto.cr.ie.u-ryukyu.ac.jp" <<< EOL; + out <<< "Connection: close" <<< EOL; + out <<< "Accept-Charset: UTF-8" <<< EOL; + out <<< "Cache-Control: no-cache" <<< EOL; + out <<< "Accept-Language: en" <<< EOL; + out <<< "" <<< EOL; + out.flush(); + while ( !in.isClosed() ) { + String ret = in.readLine(); + print ret; + } + in.Close(); + out.close(); + + + HttpRequest h = new HttpRequest(); + + + +} \ No newline at end of file