473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

multiple HTTP posts from JAVA using single session

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
1 8548

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: jing_li | last post by:
Hi, you all, I am a newbee for php and I need your help. One of my coworker and I are both developing a webpage for our project using php. We have a copy of the same files in different location...
4
by: john | last post by:
How do u guys handle multiple sessions?? i.e, opening different browser windows by running iexplore.exe or clicking IE icons and opening the application. My sessions are mixing up. what i mean is...
16
by: noah | last post by:
Does PHP have a feature to associate Cookie sessions with a persistent database connection that will allow a single transaction across multiple HTTP requests? Here is how I imagine my process: I...
11
by: Ohaya | last post by:
Hi, I'm trying to understand a situation where ASP seems to be "blocking" of "queuing" requests. This is on a Win2K Advanced Server, with IIS5. I've seen some posts (e.g.,...
6
by: Shashi | last post by:
I have developed ASP.Net application using .Net 1.1 Framework. When the user clicks image file through Java script I am using my search window as below. QueryString =...
4
by: Jeff | last post by:
We have multiple ASP.Net web apps in development. As a standard we are looking to go with SQL Server to hold state information. Can we have the multiple apps all point to a single State DB? Or...
1
by: sneha123 | last post by:
There will be some 20 questions and for each question there will be 4 choices.what i want to do is to select multiple answers by clicking the checkbox. i m using asp.net,vb.net pls help me we...
6
by: Joseph Geretz | last post by:
I have the following class which I am serializing and passing back and forth between my Web Service application and the client. public class Token : SoapHeader { public string SID; public...
35
by: keerthyragavendran | last post by:
hi i'm downloading a single file using multiple threads... how can i specify a particular range of bytes alone from a single large file... for example say if i need only bytes ranging from...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.