Simple http server & client implementation by pure j2se.
http - client:
package eric.j2se.net.http; import java.io.IOException; import java.io.InputStreamReader; import java.io.LineNumberReader; import java.io.PrintWriter; import java.net.Socket; /** * <p> * A simple http client. * </p> * * @author eric * @date Jan 6, 2014 4:29:54 PM */ public class SimpleHttpClient { /** * send http request & get response, * * @param host * hostname or ip * @param path * path after host, format: "/xxx" * @param port * @return response string * @throws IOException * @throws InterruptedException */ public static String httpRequest(String host, String path, int port) throws IOException, InterruptedException { Socket client = new Socket(host, port); StringBuffer requestInfo = new StringBuffer(""); StringBuffer responseInfo = new StringBuffer(""); // prepare request info, requestInfo.append("GET " + path + " HTTP/1.1\n"); requestInfo.append("Host: " + host + "\n"); requestInfo.append("Connection: Close\n"); // send request, PrintWriter pw = new PrintWriter(client.getOutputStream(), true); pw.println(requestInfo.toString()); System.out.println("****** request - start ******"); System.out.println(requestInfo.toString()); System.out.println("****** request - end ******"); // get response info, LineNumberReader lnr = new LineNumberReader(new InputStreamReader(client.getInputStream())); String line; while ((line = lnr.readLine()) != null) { responseInfo.append(line + "\n"); } System.out.println("****** response - start ******"); System.out.println(responseInfo.toString()); System.out.println("****** response - end ******"); pw.close(); lnr.close(); client.close(); return responseInfo.toString(); } /****** test - start ******/ /** * test http request, */ public static void testHttpRequest() { try { httpRequest("localhost", "/index.html", 80); // sendRequest("www.google.com.hk", "/", 80); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } public static void main(String[] args) { } /****** test - end ******/ }
http - server:
package eric.j2se.net.http; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Map; /** * <p> * A simple http server. * </p> * * @author eric * @date Jan 9, 2014 10:10:48 PM */ public interface HttpServer { /** * start server, */ void start(); /** * parse, get params, * * @param is * @return params * @throws IOException */ Map<String, String> parse(InputStream is) throws IOException; /** * read param from param string, * * @param paramStr * params string, format: name1=value1&name2=value2\ * @param isBody * whether the params from body, * * @return map of param key/value, */ Map<String, String> parseParam(String paramStr, boolean isBody); /** * send response * * @param os * @param paramMap */ void response(OutputStream os, Map<String, String> paramMap); /** * shutdown server, * * @throws IOException */ void terminate() throws IOException; }
package eric.j2se.net.http; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.LineNumberReader; import java.io.OutputStream; import java.io.PrintWriter; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; import java.net.UnknownHostException; import java.util.Date; import java.util.HashMap; import java.util.Map; import org.apache.commons.lang.StringUtils; /** * <p> * A simple http server implementation. * </p> * TODO ... add more feature * * @author eric * @date Jan 10, 2014 2:53:50 AM */ public class SimpleHttpServer implements HttpServer { private ServerSocket server; public SimpleHttpServer(int port, int backlog, String host) throws UnknownHostException, IOException { server = new ServerSocket(port, backlog, InetAddress.getByName(host)); } @Override public void start() { Socket socket = null; InputStream inStream = null; OutputStream outStream = null; Map<String, String> paramMap = null; while (true) { try { socket = server.accept(); // get input inStream = socket.getInputStream(); paramMap = parse(inStream); // paramMap = new HashMap(); // write output outStream = socket.getOutputStream(); response(outStream, paramMap); // close socket, this indicate the client that the response is finished, socket.close(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (inStream != null) { inStream.close(); } if (outStream != null) { outStream.close(); } if (socket != null) { socket.close(); } } catch (IOException e) { e.printStackTrace(); } } } } @Override public Map<String, String> parse(InputStream is) throws IOException { Map<String, String> paramMap = new HashMap<String, String>(); LineNumberReader lr = new LineNumberReader(new InputStreamReader(is)); String inputLine = null; String method = null; String httpVersion = null; String uri = null; // read request line inputLine = lr.readLine(); String[] requestCols = inputLine.split("\\s"); method = requestCols[0]; uri = requestCols[1]; httpVersion = requestCols[2]; System.out.println("http version:\t" + httpVersion); // parse GET param if (uri.contains("?")) { paramMap.putAll(parseParam(uri.split("\\?", 2)[1], false)); } // read header while (StringUtils.isNotBlank(inputLine = lr.readLine())) { System.out.println("post header line:\t" + inputLine); } // read body - POST method if (method.toUpperCase().equals("POST")) { StringBuffer bodySb = new StringBuffer(); char[] bodyChars = new char[1024]; int len; // ready() make sure it will not block, while (lr.ready() && (len = lr.read(bodyChars)) > 0) { bodySb.append(bodyChars, 0, len); } paramMap.putAll(parseParam(bodySb.toString(), true)); System.out.println("post body:\t" + bodySb.toString()); } return paramMap; } @Override public Map<String, String> parseParam(String paramStr, boolean isBody) { String[] paramPairs = paramStr.trim().split("&"); Map<String, String> paramMap = new HashMap<String, String>(); String[] paramKv; for (String paramPair : paramPairs) { if (paramPair.contains("=")) { paramKv = paramPair.split("="); if (isBody) { // replace '+' to ' ', because in body ' ' is replaced by '+' automatically when post, paramKv[1] = paramKv[1].replace("+", " "); } paramMap.put(paramKv[0], paramKv[1]); } } return paramMap; } @Override public void response(OutputStream os, Map<String, String> paramMap) { String name = StringUtils.isBlank(paramMap.get("name")) ? "xxx" : paramMap.get("name"); PrintWriter pw = null; pw = new PrintWriter(os); pw.println("HTTP/1.1 200 OK"); pw.println("Content-type: text/html; Charset=UTF-8"); pw.println(""); pw.println("<h1>Hi <span style='color: #FFF; background: #000;'>" + name + "</span> !</h1>"); pw.println("<h4>current date: " + new Date() + "</h4>"); pw.println("<p>you can provide your name via a param called <span style='color: #F00; background: yellow;'>\"name\"</span>, in both GET and POST method.</p>"); pw.flush(); } @Override public void terminate() throws IOException { server.close(); } /****** test - start ******/ public static void main(String[] args) { testHttpServer(); } // test http server, public static void testHttpServer() { try { HttpServer server = new SimpleHttpServer(9090, 1, "localhost"); server.start(); } catch (IOException e) { e.printStackTrace(); } } /****** test - end ******/ }
相关推荐
Simple TCP client server.
Client Identifier +-------+-------+ | -+----(A)-- & Redirection URL ---->| | | User- | | Authorization | | Agent -+----(B)-- User authenticates --->| Server | | | | | | -+----(C)-- Authorization...
android 服务器的简单搭建,包括客户端
서버 : simple-rtc-signaling-server setting 서버 주소를 G.kt파일에서 수정. val SIGNAL_SERVER_ADDR = "ws://192.168.35.192:12345" MVVM Notify 방법은 RxJava를 이용 Dependency Injection Kodein 이용
codeproject 《A Simple IOCP Server/Client Class》 整理修改版
一个简单的IOCP服务器/客户端类(A simple IOCP Server/Client Class)
A java RMI server client math operator. Simple project for the network services class.
perl-HTTP-Tiny-0.033-3.el7.noarch.rpm perl-libs-5.16.3-294.el7_6.x86_64.rpm perl-macros-5.16.3-294.el7_6.x86_64.rpm perl-parent-0.225-244.el7.noarch.rpm perl-PathTools-3.40-5.el7.x86_64.rpm perl-Pod-...
Simple Chat Application. Multiple clients may connect and communicate.
RTSP-Client-Server Introduction Streaming videos are ubiquitous in the web today. The internet, originally designed for simple text/document based transfers using protocols such as the HTTP, is the ...
来自教程 Java Networking :: Simple server-client protocol program with DataInput/OutputStreams! 一个第一和第二终端 java Session
Simple Client-Server Demo 简单的客户端服务器模型 用C语言写的,用套接字建立连接。
Simple Client-server Interactions using C# By BobJanova C#封装的简单的客户机/服务器Socket通讯程序,非常棒!
npm install simple-signal-client --save 如果没有Browserify,请使用 。 例子 一种常见的信令方案是通过让一个客户端“呼叫”另一个客户端的ID来连接两个客户端。 服务器: const signalServer = require ( '...
JAVA-简单-回显客户端服务器 具有其他类的简单多线程Java回显客户端-服务器,该类使用文件dane.txt中的数据创建对象 注释和波兰语中的javadoc。
vue-cli的模板 vue-cli的脚手架项目模板有webpack-simple 和 webpack 两种的区别在于webpack-simple 没有包括Eslint 检查等功能 vue-cli的项目结构 . |-- build // 项目构建(webpack)相关代码...| |-- dev-server.js
ping - for simple pinging the server close - to close the connection everything else will send an example JSON message to the server 通过命令行 根据需要调整Message和ProcessMessageHandler
UDP sockets - UDP sockets code udpserver.c - Simple server using a UDP socket. udpclient.c - Simple client using a UDP socket.
简单的客户服务器模型的通信工具
Simple-Web-Server:使用C ++ 11和Boost.Asio实现的非常简单,快速,多线程,独立于平台的HTTP和HTTPS服务器和客户端库。 创建是使REST资源可从C ++应用程序获得的简单方法