view http/request.k @ 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
children
line wrap: on
line source

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();



}