Mercurial > hg > Members > nobuyasu > konoha
view http/url.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 | f78fb53ef7b6 |
line wrap: on
line source
using konoha.socket.*; using konoha.io.*; class HttpRequest { int keySize = 0; String[] keys; Map<String,String> property = {}; String host, method, uri; String body; Socket socket; OutputStream out; InputStream in; HttpRequest() { property = {}; this.method = "GET"; } ~HttpRequest() { print("destructor"); } void setUri(String uri) { this.uri = uri; } void setMethod(String method) { this.method = method; } void openConnection(String host, int port=80) { this.host = host; this.socket = new Socket(host, port); this.out = this.socket.getOutputStream(); this.in = this.socket.getInputStream(); } void setRequestProperty(String key, String value) { String[] tmpKey = new String[keySize+1]; for (int i=0; i < keySize; i++) { tmpKey[i] = keys[i]; } tmpKey[keySize] = key; keys = tmpKey; keySize++; property.set(key, value); } void printRequestProperty() { foreach (String key in keys) OUT << key +": " + property[key] << EOL; } void setBody(String body) { this.body = body; } void appendBody() { String } void sendRequest() { out <<< this.method + " /" + this.uri + " HTTP/1.1" <<< EOL; out <<< "HOST: " + this.host <<< EOL; for (String key : keys) { out <<< key +": " + property[key] <<< EOL; } out <<< EOL; out <<< body <<< EOL; out.flush(); } OutputStream getOutputStream() { return this.out; } InputStream getInputStream() { return this.in; } void close() { out.close(); in.close(); } } void printResponse(InputStream in) { print("print Response"); while ( !in.isClosed() ) { String ret = in.readLine(); OUT << ret << EOL; } } void main(String[] args) { HttpRequest r = new HttpRequest(); r.openConnection("localhost"); r.setMethod("POST"); r.setUri("index.html"); r.setRequestProperty("Connection","close"); r.setRequestProperty("Accept-Charset","UTF-8"); r.setRequestProperty("Cache-Control","no-cache"); r.setRequestProperty("Accept-Language","en"); r.sendRequest(); InputStream in = r.getInputStream(); printResponse(in); r.close(); /* String http = "http://dimolto.cr.ie.u-ryukyu.ac.jp"; String split = http.split("http://")[1]; print split; */ }