view http/postRequest.k @ 4:108812b08e75

add http/postRequest.k http/url.k
author Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
date Sun, 20 May 2012 21:09:03 +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() {
		this.port = 80;
		this.method = "POST";
		this.uri = "index.html";
		this.host = "";
		this.charset = "UTF-8";
		this.connection = "close";
		this.cacheControl = "no-cache";
		this.language = "en";
	}
	void setMethod(String method) {
		this.method = method;
	}
	void setHost(String host) {
		this.host = host;
	}
	void setUri(String uri) {
		this.uri = uri;
	}
	void connectToHost() {
		print("connect to "+ host);
		this.sock = new Socket(this.host, port);
		in = this.sock.getInputStream();
		out = this.sock.getOutputStream();
	}

	void sendRequest() {
		out <<< method + " /" + uri + " HTTP/1.1" <<< EOL;
		out <<< "HOST: " + this.host <<< EOL;
		out <<< "Connection: " + connection <<< EOL;
		out <<< "Accept-Charset: " + charset <<< EOL;
		out <<< "Cache-Control: " + cacheControl <<< EOL;
		out <<< "Accept-Language: " + language <<< EOL;
		out <<< EOL;
		out.flush();
	}

	void printRequest() {
		print("printRequest");
		print(method + " /" + uri + " HTTP/1.1");
		print("HOST: " + host);
		print("Connection: " + connection);
		print("Accept-Charset: " + charset);
		print("Cache-Control: " + cacheControl);
		print("Accept-Language: " + language);
		print("");
	}

	void printResponse() {
		print("print Response");
		while ( !in.isClosed() ) {
			String ret = in.readLine();
			print(ret);
		}
	}

}



void main(String[] args)
{
	HttpRequest h = new HttpRequest();
	h.setHost("dimolto.cr.ie.u-ryukyu.ac.jp");
	h.setMethod("POST");
	h.connectToHost();
	h.sendRequest();
	h.printRequest();
	h.printResponse();
}