annotate http/request.k @ 17:746ef742e586 draft default tip

commit
author Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
date Sun, 03 Jun 2012 01:38:30 +0900
parents f85060c91817
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 using konoha.io.*;
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 using konoha.socket.*;
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
3
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
4
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 class HttpRequest()
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 {
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 int port;
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 String method, uri;
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 String host, connection, charset, cacheControl, language;
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 Socket sock;
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 InputStream in;
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 OutputStream out;
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 HttpRequest() {
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 port = 80;
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 method = "GET";
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 uri = "index.html";
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 host = "google.co.jp";
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 host = "google.co.jp";
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 charset = "UTF-8";
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 connection = "close";
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 cacheControl = "no-cache";
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 language = "en";
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 }
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
24
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
25 void setHost(String _host) {
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
26 host = _host;
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 }
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
28
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
29 void connectToHost() {
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
30 sock = new Socket(host, port);
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
31 in = new sock.getInputStream();
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 out = new sock.getOutputStream();
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
33 }
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
34
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
35 void sendRequest() {
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
36 out <<< method + "/" + uri + "HTTP/1.1" <<< EOL;
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
37 out <<< "HOST: " + host <<< EOL;
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
38 out <<< "Connection: " + connection <<< EOL;
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
39 out <<< "Accept-Charset: " + charset <<< EOL;
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
40 out <<< "Cache-Control: " + cacheControl <<< EOL;
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
41 out <<< "Accept-Language: " + language <<< EOL;
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
42 out <<< "" <<< EOL;
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
43 }
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
44
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
45 void printResponse() {
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
46 while ( !in.isClosed() ) {
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
47 String ret = in.readLine();
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
48 print ret;
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
49 }
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
50 }
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
51
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
52
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
53 }
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
54
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
55
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
56
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
57 void main(String[] args)
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
58 {
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
59 int port = 80; // port number of http protocol
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
60 String host = "dimolto.cr.ie.u-ryukyu.ac.jp";
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
61
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
62 Socket sock = new Socket(host, port);
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
63 InputStream in = sock.getInputStream();
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
64 OutputStream out = sock.getOutputStream();
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
65
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
66 out <<< "GET /index.html HTTP/1.1" <<< EOL;
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
67 out <<< "Host: dimolto.cr.ie.u-ryukyu.ac.jp" <<< EOL;
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
68 out <<< "Connection: close" <<< EOL;
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
69 out <<< "Accept-Charset: UTF-8" <<< EOL;
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
70 out <<< "Cache-Control: no-cache" <<< EOL;
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
71 out <<< "Accept-Language: en" <<< EOL;
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
72 out <<< "" <<< EOL;
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
73 out.flush();
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
74 while ( !in.isClosed() ) {
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
75 String ret = in.readLine();
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
76 print ret;
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
77 }
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
78 in.Close();
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
79 out.close();
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
80
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
81
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
82 HttpRequest h = new HttpRequest();
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
83
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
84
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
85
f85060c91817 add requet.k
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
86 }