Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / February 2004

Tip: Looking for answers? Try searching our database.

Multiple HTTP posts from JAVA

Thread view: 
SPG - 05 Feb 2004 13:18 GMT
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
Tor Iver Wilhelmsen - 05 Feb 2004 19:03 GMT
> 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.

Did you specify "keepalive" in the request headers?

>         wr.write("Content-Length: "+data.length()+"\r\n");
>         wr.write("Content-Type: application/x-www-form-urlencoded\r\n");

No, you didn't.

Go read the HTTP specs again.
SPG - 05 Feb 2004 19:15 GMT
Cheers Tor,

I used socket.getKeepAlive(true), but didn;t realise I needed to put it in
th eheader too..

Thanks.

> > 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
[quoted text clipped - 9 lines]
>
> Go read the HTTP specs again.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.