By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,384 Members | 714 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,384 IT Pros & Developers. It's quick & easy.

multiple HTTP posts from JAVA using single session

P: n/a
SPG
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>


Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a

"SPG" <st************@nopoo.blueyonder.co.uk> wrote in message
news:z4*********************@news-text.cableinet.net...
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>


What is the problem with URLConnection? I do not understand what problem you
solve by using plain sockets. A servlet communicates through HTTP so the
HttpURLConnection is the way to go. It would be pointless to implement your
own HTTP (including sessions). HTTP is in essence an offline protocol
(except for the 1.1 behaviour that allows multiple requests per connection
but that should not be used to keep a user session connected since that
would severely hurt the scalability of the servlet container) so using a
plain socket makes no sense.

Silvio Bierman
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.