1 2 package samplehttp.client; 3 4 import java.io.*; 6 7 import java.net.*; 8 import java.util.*; 9 10 11 12 15 public class Sender { 16 17 18 String hostname; 19 String servlet; 20 int port; 21 String timetowait; 22 int flgejb; 23 24 final String HTTP = "HTTP/1.1"; 25 final String METHOD = "POST "; 26 final String CONTENT_TYPE = "Content-type: application/x-www-form-urlencoded"; 27 final String CONTENT_LENGTH = "Content-length: "; 28 29 30 39 public Sender(String hostname, String servlet, 40 int port, String timetowait, int flgejb) { 41 42 this.hostname = hostname; 43 this.servlet = servlet; 44 this.port = port; 45 this.timetowait = timetowait; 46 this.flgejb = flgejb; 47 } 48 49 52 public void execute() throws IOException{ 53 54 StringBuffer b = new StringBuffer("http://"); 55 56 b.append(hostname); 57 b.append(':'); 58 b.append(port); 59 b.append('/'); 60 b.append(servlet); 61 62 URL url = new URL(b.toString()); 63 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 64 65 66 conn.setRequestProperty("Connection","Close"); 67 conn.setDoOutput(true); 68 conn.setDoInput(true); 69 conn.setRequestMethod("POST"); 70 71 PrintWriter stream = new PrintWriter(conn.getOutputStream(), 72 true); 73 74 76 stream.println("param= " + timetowait+ " "+flgejb ); 77 System.out.println("timetowait "+timetowait); 78 79 stream.close(); 80 81 BufferedReader reader = 82 new BufferedReader(new InputStreamReader(conn 83 .getInputStream())); 84 85 boolean ok = false; 87 88 System.err.println("Reading response"); 89 StringBuffer response = new StringBuffer(); 90 String r = null; 91 92 for (;;) { 93 r = reader.readLine(); 94 95 if (r == null) { 96 break; 97 } 98 99 response.append(r); 100 if (r.indexOf("OK") != -1) { 101 ok = true; 102 103 break; 104 } 105 } 106 107 System.err.println("Response received"); 108 reader.close(); 109 System.err.println("Connection closed"); 110 if (!ok) { 111 System.out.println(">> Non OK"); 112 } 113 114 } 115 } 116 117 | Popular Tags |