@@ -51,130 +51,139 @@ public class ResendPostBody {
51
51
52
52
static class Server extends Thread {
53
53
54
- InputStream in ;
55
- OutputStream out ;
56
- Socket sock ;
57
- StringBuffer response ;
58
- ServerSocket server ;
59
-
60
- Server (ServerSocket s ) throws IOException
61
- {
54
+ private InputStream in ;
55
+ private OutputStream out ;
56
+ private Socket sock ;
57
+ private StringBuffer response ;
58
+ private ServerSocket server ;
59
+
60
+ Server (ServerSocket s ) throws IOException {
62
61
server = s ;
63
62
}
64
63
65
- void waitFor (String s ) throws IOException
66
- {
67
- byte [] w = s .getBytes ();
68
- for (int c =0 ; c <w .length ; c ++ ) {
64
+ void waitFor (String s ) throws IOException {
65
+ byte [] w = s .getBytes ();
66
+ for (int c = 0 ; c < w .length ; c ++) {
69
67
byte expected = w [c ];
70
68
int b = in .read ();
71
69
if (b == -1 ) {
72
- acceptConn ();
70
+ acceptConn ();
73
71
}
74
- if ((byte )b != expected ) {
72
+ if ((byte ) b != expected ) {
75
73
c = 0 ;
76
74
}
77
75
}
78
76
}
79
77
80
- boolean done = false ;
78
+ private boolean done = false ;
81
79
82
- public synchronized boolean finished () {
80
+ public synchronized boolean finished () {
83
81
return done ;
84
82
}
85
83
86
- public synchronized void setFinished (boolean b ) {
84
+ public synchronized void setFinished (boolean b ) throws IOException {
87
85
done = b ;
86
+ this .closeConn ();
87
+ server .close ();
88
+ }
89
+
90
+ void acceptConn () throws IOException {
91
+ sock = server .accept ();
92
+ in = sock .getInputStream ();
93
+ out = sock .getOutputStream ();
88
94
}
89
95
90
- void acceptConn () throws IOException
91
- {
92
- sock = server .accept ();
93
- in = sock .getInputStream ();
94
- out = sock .getOutputStream ();
96
+ void closeConn () throws IOException {
97
+ in .close ();
98
+ out .close ();
99
+ sock .close ();
95
100
}
96
101
97
- public void run () {
102
+ public void run () {
98
103
try {
99
- response = new StringBuffer (1024 );
100
- acceptConn ();
101
- waitFor ("POST" );
102
- waitFor ("ZZZ" );
103
- Thread .sleep (500 );
104
- sock .close ();
105
- acceptConn ();
106
- waitFor ("POST" );
107
- waitFor ("ZZZ" );
108
- response .append ("HTTP/1.1 200 OK\r \n " );
109
- response .append ("Server: Microsoft-IIS/5.0" );
110
- response .append ("Date: Wed, 26 Jul 2000 14:17:04 GMT\r \n \r \n " );
111
- out .write (response .toString ().getBytes ());
104
+ response = new StringBuffer (1024 );
105
+ acceptConn ();
106
+ waitFor ("POST" );
107
+ waitFor ("ZZZ" );
108
+ Thread .sleep (500 );
109
+ sock .close ();
110
+ acceptConn ();
111
+ waitFor ("POST" );
112
+ waitFor ("ZZZ" );
113
+ response .append ("HTTP/1.1 200 OK\r \n " );
114
+ response .append ("Server: Microsoft-IIS/5.0" );
115
+ response .append ("Date: Wed, 26 Jul 2000 14:17:04 GMT\r \n \r \n " );
116
+ out .write (response .toString ().getBytes ());
117
+ out .flush ();
112
118
while (!finished ()) {
113
- Thread .sleep (1000 );
119
+ Thread .sleep (1000 );
114
120
}
115
- out .close ();
116
121
} catch (Exception e ) {
117
- System .err .println ("Server Exception: " + e );
122
+ if (!done ) {
123
+ System .err .println ("Server Exception: " + e );
124
+ }
118
125
} finally {
119
- try { server .close (); } catch (IOException unused ) {}
126
+ try {
127
+ closeConn ();
128
+ } catch (IOException ioe ) {
129
+ if (!done ) {
130
+ ioe .printStackTrace ();
131
+ }
132
+ }
120
133
}
121
134
}
122
135
}
123
136
124
- ServerSocket ss ;
125
- Server server ;
137
+ private ServerSocket ss ;
138
+ private Server server ;
126
139
127
140
public static void main (String [] args ) throws Exception {
128
- try {
129
- if (args .length == 1 && args [0 ].equals ("-i" )) {
130
- System .out .println ("Press return when ready" );
131
- System .in .read ();
132
- System .out .println ("Done" );
133
- }
134
- ResendPostBody t = new ResendPostBody ();
135
- t . execute ();
136
- } catch (IOException e ) {
137
- System .out .println ("IOException" );
141
+ if (args .length == 1 && args [0 ].equals ("-i" )) {
142
+ System .out .println ("Press return when ready" );
143
+ System .in .read ();
144
+ System .out .println ("Done" );
138
145
}
146
+ ResendPostBody t = new ResendPostBody ();
147
+ t .execute ();
139
148
}
140
149
141
- public void execute () throws Exception {
142
-
150
+ public void execute () throws Exception {
143
151
byte b [] = "X=ABCDEFGHZZZ" .getBytes ();
144
152
145
153
ss = new ServerSocket (0 , 0 , InetAddress .getLoopbackAddress ());
146
- server = new Server (ss );
147
- server .start ();
154
+ server = new Server (ss );
155
+ server .start ();
156
+
148
157
/* Get the URL */
149
158
URL url = URIBuilder .newBuilder ()
150
159
.scheme ("http" )
151
160
.loopback ()
152
161
.port (ss .getLocalPort ())
153
162
.path ("/test" )
154
163
.toURL ();
155
- HttpURLConnection conURL = (HttpURLConnection )url .openConnection ();
164
+ HttpURLConnection conURL = (HttpURLConnection ) url .openConnection (Proxy . NO_PROXY );
156
165
157
166
conURL .setDoOutput (true );
158
167
conURL .setDoInput (true );
159
168
conURL .setAllowUserInteraction (false );
160
169
conURL .setUseCaches (false );
161
170
conURL .setRequestProperty ("Content-Type" , "application/x-www-form-urlencoded" );
162
- conURL .setRequestProperty ("Content-Length" , "" + b .length );
171
+ conURL .setRequestProperty ("Content-Length" , "" + b .length );
163
172
conURL .setRequestProperty ("Connection" , "Close" );
164
173
165
174
/* POST some data */
166
-
167
175
DataOutputStream OutStream = new DataOutputStream (conURL .getOutputStream ());
168
- OutStream .write (b , 0 , b .length );
176
+ OutStream .write (b , 0 , b .length );
169
177
OutStream .flush ();
170
178
OutStream .close ();
171
179
172
180
/* Read the response */
181
+ int resp = conURL .getResponseCode ();
173
182
174
- int resp = conURL . getResponseCode ();
175
- server .setFinished ( true );
183
+ server . setFinished ( true ); // Set finished and close ServerSocket
184
+ server .join (); // Join server thread
176
185
177
186
if (resp != 200 )
178
- throw new RuntimeException ("Response code was not 200: " + resp );
179
- }
187
+ throw new RuntimeException ("Response code was not 200: " + resp );
188
+ }
180
189
}
0 commit comments