001: package samplehttp.client;
002:
003: //Import statements
004: import java.io.*;
005:
006: import java.net.*;
007: import java.util.*;
008:
009: /**
010: * This is the class for non https communication
011: */
012: public class Sender {
013:
014: String hostname;
015: String servlet;
016: int port;
017: String timetowait;
018: int flgejb;
019:
020: final String HTTP = "HTTP/1.1";
021: final String METHOD = "POST ";
022: final String CONTENT_TYPE = "Content-type: application/x-www-form-urlencoded";
023: final String CONTENT_LENGTH = "Content-length: ";
024:
025: /**
026: * Constructor Sender
027: *
028: *
029: * @param hostname
030: * @param servlet
031: * @param port
032: *
033: */
034: public Sender(String hostname, String servlet, int port,
035: String timetowait, int flgejb) {
036:
037: this .hostname = hostname;
038: this .servlet = servlet;
039: this .port = port;
040: this .timetowait = timetowait;
041: this .flgejb = flgejb;
042: }
043:
044: /**
045: * execute: connect to a servlet and send something
046: */
047: public void execute() throws IOException {
048:
049: StringBuffer b = new StringBuffer("http://");
050:
051: b.append(hostname);
052: b.append(':');
053: b.append(port);
054: b.append('/');
055: b.append(servlet);
056:
057: URL url = new URL(b.toString());
058: HttpURLConnection conn = (HttpURLConnection) url
059: .openConnection();
060:
061: conn.setRequestProperty("Connection", "Close");
062: conn.setDoOutput(true);
063: conn.setDoInput(true);
064: conn.setRequestMethod("POST");
065:
066: PrintWriter stream = new PrintWriter(conn.getOutputStream(),
067: true);
068:
069: // Send
070:
071: stream.println("param= " + timetowait + " " + flgejb);
072: System.out.println("timetowait " + timetowait);
073:
074: stream.close();
075:
076: BufferedReader reader = new BufferedReader(
077: new InputStreamReader(conn.getInputStream()));
078:
079: // Check for response "200 OK"
080: boolean ok = false;
081:
082: System.err.println("Reading response");
083: StringBuffer response = new StringBuffer();
084: String r = null;
085:
086: for (;;) {
087: r = reader.readLine();
088:
089: if (r == null) {
090: break;
091: }
092:
093: response.append(r);
094: if (r.indexOf("OK") != -1) {
095: ok = true;
096:
097: break;
098: }
099: }
100:
101: System.err.println("Response received");
102: reader.close();
103: System.err.println("Connection closed");
104: if (!ok) {
105: System.out.println(">> Non OK");
106: }
107:
108: }
109: }
|