commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jenkins, David" <jenki...@fish.govt.nz>
Subject Re: httpClient help - for Oleg
Date Wed, 01 Oct 2003 20:04:16 GMT
Hi Oleg,

I'll include some code fragments that I currently use and code fragments
that I've tried using http client. Maybe you can see where my problems
lie.
****
      HttpURLConnection  connection =
(HttpURLConnection)url.openConnection();
      connection.setDoInput(true);
      connection.setDoOutput(true);
      connection.setUseCaches(false);
      connection.setRequestProperty("Content-Type",
                             "application/x-www-form-urlencoded");

      ObjectOutputStream oStream =
          new ObjectOutputStream(connection.getOutputStream());
      oStream.writeObject(proxy.getClass().getName());
      oStream.writeObject(method.getName());
      if (args != null) {
        for (int i = 0; i < args.length; i++) {
          oStream.writeObject(args[i]);
        }
      }
      oStream.flush();
      oStream.close();

      /* Connect and recover any return args*/
      connection.connect();
****
I then go on to collect any returned values/exceptions from the
response.


This code written for http client simulates a single call 

****
  HttpClient httpClient = new HttpClient();
  HostConfiguration hostConfiguration  = new HostConfiguration();
  HttpConnectionManager httpConnectionManager;

  public Main() {
    hostConfiguration.setHost("localhost",8080,"http");
    httpClient.setHostConfiguration(hostConfiguration);
    httpConnectionManager = httpClient.getHttpConnectionManager();
    doit();
  }


  public void doit() {
    PostMethod post = null;
    try {      
      post = new PostMethod("/fgdam_tst/rPCAction.do");
      post.setDoAuthentication(true);

      Header header = new
Header("content-type",PostMethod.FORM_URL_ENCODED_CONTENT_TYPE);
      post.setRequestHeader(header);

      HttpState state = new HttpState();

      HttpConnection connection =
          httpConnectionManager.getConnection(hostConfiguration);

      ObjectOutputStream oos =
          new ObjectOutputStream(connection.getRequestOutputStream());

      oos.writeObject("");
      oos.writeObject("findAllClassifications");
      oos.flush();
      oos.close();

      post.execute(state,connection); 

      if (post.getStatusCode()==HttpStatus.SC_OK) {
        ObjectInputStream ois =
            new ObjectInputStream(connection.getResponseInputStream());
        Object object = ois.readObject();
        System.out.println(object);
      }
      else{
        System.out.println(post.getStatusText());
      }

      post.releaseConnection();

    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
    finally {
      post.releaseConnection();
    }
  }

****
This code results in IOExceptions at the server when trying to read the
request input stream.

So far I've not found any code in the samples that does exactly what I
want. Or more likely I've just not noticed the code that does exactly
what I want.

cheers

Dave




---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message