Duplicate :
|
|
Relates :
|
Name: rm29839 Date: 12/16/97 1 make HTTP URLConnection send request server reply right respose send second request server close keep-alive connection HTTP URLConnection automatically resend request but does not send message body 2 demo source (including client & server) import java.io.*; import java.net.*; public class Http3 { static final String HOST = "localhost"; static final int PORT = 1000; static final int MAX = 30; static final int SIZE = 10000; static class Server extends Thread { Server() { } public void run() { ServerSocket ss; try { ss = new ServerSocket(PORT); } catch (IOException e) { System. out.println("ServerSocket IOException"); return; } try { Socket socket = ss.accept(); DataInputStream dis = new DataInputStream(socket.getInputStream()); PrintWriter pw = new PrintWriter(socket.getOutputStream()); String head; for (;;) { head = dis.readLine(); System.out.println(head); if (head.equals("")) break; } String str = dis.readLine(); System.out.println(str); pw.print("HTTP/1.0 200 OK\r\n"); pw.print("Connection: keep-alive\r\n"); pw.print("Content-length: 12\r\n"); pw.print("\r\n"); pw.print("0123456789\r\n"); pw.flush(); head = dis.readLine(); System.out.println(head); dis.close(); socket = ss.accept(); dis = new DataInputStream(socket.getInputStream()); pw = new PrintWriter(socket.getOutputStream()); head = dis.readLine(); if (head == null) { System.out.println("head == null"); dis.close(); ss.close(); return; } System.out.println(head); for (;;) { head = dis.readLine(); System.out.println(head); if (head.equals("")) break; } str = dis.readLine(); System.out.println(str); pw.print("HTTP/1.0 200 OK\r\n"); pw.print("Connection: keep-alive\r\n"); pw.print("Content-length: 12\r\n"); pw.print("\r\n"); pw.print("0123456789\r\n"); pw.flush(); dis.close(); ss.close(); } catch (IOException e) { System. out.println("IOException"); } } } public static void main(String args[]) { Server server = new Server(); server.setDaemon(true); server.start(); try { URL url = new URL("http", HOST, PORT, "/file"); URLConnection uc = url.openConnection(); uc.setDoOutput(true); OutputStream os = uc.getOutputStream(); PrintWriter pw = new PrintWriter(os); pw.print("ABCDEFGH\r\n"); pw.close(); System.out.println("os.close"); BufferedReader br = new BufferedReader(new InputStreamReader(uc.getInputStream())); System.out.println("getInputStream"); String str = br.readLine(); System.out.println(str); br.close(); uc = url.openConnection(); uc.setDoOutput(true); os = uc.getOutputStream(); pw = new PrintWriter(os); pw.print("ABCDEFGH\r\n"); pw.close(); System.out.println("os.close"); br = new BufferedReader(new InputStreamReader(uc.getInputStream())); System.out.println("getInputStream"); str = br.readLine(); System.out.println(str); br.close(); } catch (MalformedURLException e) { System.out.println("MalformedURLException"); } catch (IOException e) { System.out.println("IOException"); } } } 3 no error message server waits for message body! 4 no 5 no (Review ID: 21946) ======================================================================