multiple HTTP posts from JAVA using single session

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • SPG

    multiple HTTP posts from JAVA using single session

    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 HttpURLConnecti on, 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.get ByName(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("subs criber","i");
    //POST 1
    String output = conn.post("/login",new
    HttpConnection. Parameter[]{param});
    System.out.prin tln(output);
    //POST 2
    output = conn.post("/logoff",new
    HttpConnection. Parameter[]{param});
    System.out.prin tln(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(p arams);
    String path = servlet;
    BufferedWriter wr = new BufferedWriter( new
    OutputStreamWri ter(_socket.get OutputStream(), "UTF8"));
    wr.write("POST "+path+" HTTP/1.0\r\n");
    wr.write("Conte nt-Length: "+data.length() +"\r\n");
    wr.write("Conte nt-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
    InputStreamRead er(_socket.getI nputStream()));

    while ((line = rd.readLine()) != null) {
    // Process line...
    buf.append(line );
    }
    return buf.toString();
    }

    private String getDataString(P arameter[] 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.toStrin g();
    }

    public static class Parameter{
    public String name;
    public String value;
    public Parameter(Strin g name, String value){
    this.name = name;
    this.value = value;
    }

    public String toString(){
    try {
    return URLEncoder.enco de(name, "UTF-8") + "=" +
    URLEncoder.enco de(value, "UTF-8");
    }
    catch (UnsupportedEnc odingException ex) {
    return null;
    }
    }
    }

    }
    </SNIP>




  • Silvio Bierman

    #2
    Re: multiple HTTP posts from JAVA using single session


    "SPG" <steve.goodsell @nopoo.blueyond er.co.uk> wrote in message
    news:z4rUb.7070 $1N4.64841744@n ews-text.cableinet. net...[color=blue]
    > 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 HttpURLConnecti on, 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[/color]
    servlet.[color=blue]
    >
    > Here is a snip of sample code I am using to make the post, just assume I[/color]
    am[color=blue]
    > 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.get ByName(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("subs criber","i");
    > //POST 1
    > String output = conn.post("/login",new
    > HttpConnection. Parameter[]{param});
    > System.out.prin tln(output);
    > //POST 2
    > output = conn.post("/logoff",new
    > HttpConnection. Parameter[]{param});
    > System.out.prin tln(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(p arams);
    > String path = servlet;
    > BufferedWriter wr = new BufferedWriter( new
    > OutputStreamWri ter(_socket.get OutputStream(), "UTF8"));
    > wr.write("POST "+path+" HTTP/1.0\r\n");
    > wr.write("Conte nt-Length: "+data.length() +"\r\n");
    > wr.write("Conte nt-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
    > InputStreamRead er(_socket.getI nputStream()));
    >
    > while ((line = rd.readLine()) != null) {
    > // Process line...
    > buf.append(line );
    > }
    > return buf.toString();
    > }
    >
    > private String getDataString(P arameter[] 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.toStrin g();
    > }
    >
    > public static class Parameter{
    > public String name;
    > public String value;
    > public Parameter(Strin g name, String value){
    > this.name = name;
    > this.value = value;
    > }
    >
    > public String toString(){
    > try {
    > return URLEncoder.enco de(name, "UTF-8") + "=" +
    > URLEncoder.enco de(value, "UTF-8");
    > }
    > catch (UnsupportedEnc odingException ex) {
    > return null;
    > }
    > }
    > }
    >
    > }
    > </SNIP>
    >
    >
    >
    >[/color]

    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
    HttpURLConnecti on 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


    Comment

    Working...