Hi,
I have a servlet application that I am trying to write a very basic load
tester for.
There application has several servlets, but all rely on the same session
being used (IE: For logged in users etc).
I am trying to write a little test app to test the workflow of the servlet
application. The problem is I need to keep the same session alive and post
via that session.
I have used HttpURLConnection, but that needs to have the explicit servlet
path on instantiation, so then I tried a Socket connection. I have found
that the socket stays alive. The first post works fine, but after that I
seem to get no response and it does not appear to be writing to my servlet.
Here is a snip of sample code I am using to make the post, just assume I am
passing a different servlet name and parameters each call. This is driving
me nuts and I am sure there is a simple solution.
HELP!
<SNIP>
import java.net.*;
import java.io.*;
/**
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class HttpConnection {
Socket _socket;
public HttpConnection(String host, int port)throws IOException{
InetAddress addr = InetAddress.getByName(host);
_socket = new Socket(addr, port);
}
public static void main(String[] args) {
try {
HttpConnection conn = new HttpConnection("music-box", 8083);
HttpConnection.Parameter param = new
HttpConnection.Parameter("subscriber","i");
//POST 1
String output = conn.post("/login",new
HttpConnection.Parameter[]{param});
System.out.println(output);
//POST 2
output = conn.post("/logoff",new
HttpConnection.Parameter[]{param});
System.out.println(output);
conn.close();
} catch (Exception e) {
}
}
public void close(){
if (_socket!=null){
try {
_socket.close();
}
catch (IOException ex) {
}
}
}
public String post(String servlet, Parameter[] params)throws
IOException{
String data = getDataString(params);
String path = servlet;
BufferedWriter wr = new BufferedWriter(new
OutputStreamWriter(_socket.getOutputStream(), "UTF8"));
wr.write("POST "+path+" HTTP/1.0\r\n");
wr.write("Content-Length: "+data.length()+"\r\n");
wr.write("Content-Type: application/x-www-form-urlencoded\r\n");
wr.write("\r\n");
// Send data
wr.write(data);
wr.flush();
// Get response
String line;
StringBuffer buf = new StringBuffer();
BufferedReader rd = new BufferedReader(new
InputStreamReader(_socket.getInputStream()));
while ((line = rd.readLine()) != null) {
// Process line...
buf.append(line);
}
return buf.toString();
}
private String getDataString(Parameter[] params){
StringBuffer dataBuf = new StringBuffer();
for(int i=0; i < params.length; i++){
if( i!=0){
dataBuf.append("&");
}
dataBuf.append(params[i].toString());
}
return dataBuf.toString();
}
public static class Parameter{
public String name;
public String value;
public Parameter(String name, String value){
this.name = name;
this.value = value;
}
public String toString(){
try {
return URLEncoder.encode(name, "UTF-8") + "=" +
URLEncoder.encode(value, "UTF-8");
}
catch (UnsupportedEncodingException ex) {
return null;
}
}
}
}
</SNIP>