KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > samplehttp > client > Sender


1
2 package samplehttp.client;
3
4 //Import statements
5
import java.io.*;
6
7 import java.net.*;
8 import java.util.*;
9
10
11
12 /**
13  * This is the class for non https communication
14  */

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     /**
31      * Constructor Sender
32      *
33      *
34      * @param hostname
35      * @param servlet
36      * @param port
37      *
38      */

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     /**
50      * execute: connect to a servlet and send something
51      */

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         // Send
75

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         // Check for response "200 OK"
86
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