Re: Post using Query String

From: Nikk & Jak Anderson (nikk_and_jak_at_btinternet.com)
Date: 03/17/04

  • Next message: John Parker: "Re: OS independent application possibility"
    Date: 17 Mar 2004 12:06:53 -0800
    
    

    To POST from your java application is fairly straigtforward.
    It is very similar to a get, except you must set the method (get/post)
    the in request header. Then you need to write the post data out to a
    stream.

    Example code:
    /*
    * _url is the url of the server
    * _request is the data you want to post
    */
    public String getResponse (URL _url, String _request)throws Exception{
      
      HttpURLConnection c = null;
      InputStream is = null;
      OutputStream os = null;
      // b is to collect the repsonse from the server
      StringBuffer b = new StringBuffer( );

      // open the connection
       c = (HttpURLConnection)_url.openConnection();
       // set the http headers
       c.setRequestProperty("Content-Type",
    "application/x-www-form-urlencoded");
       c.setRequestProperty("Content-Length", "" + Integer.toString
    (_request.getBytes().length) );
        c.setDoOutput(true);
        c.setDoInput(true);
        // set the method (i.e. GET or POST)
        c.setRequestMethod("POST");

        // get the output stream - this is for writing the post data to
        os = c.getOutputStream();
        // write the data
        os.write(_request.getBytes());
        os.flush();
        os.close();
        // Get HTTP response
        is = c.getInputStream( );
        System.out.println("Response code = "+ c.getResponseCode());
        int ch;
        while ((ch = is.read( )) != -1){
          b.append((char) ch);
        }
        is.close();
        c.disconnect();
        // b contains the data returned from the server
        return(b.toString());

    }


  • Next message: John Parker: "Re: OS independent application possibility"

    Relevant Pages

    • Re: Fastest way to search a string for the occurance of a word??
      ... Of course this depends on the length of your string, ... C/C++'s strstr function (uses CPU-specific optimizations) ... using real test data. ... > I need to check the request header of every request that is hitting my web ...
      (microsoft.public.dotnet.languages.csharp)
    • Re: Cannot post string "union select" php form
      ... i am trying to figure it out why i cannot post string "union select" ... every time i try to post data which content union and select .. ... i guess the server i am using has some kind of filter and if a post ...
      (comp.lang.php)
    • SQL Connection Problem
      ... An error has occurred while establishing a connection to the server. ... database location within the applications App_Data directory. ... Boolean& failoverDemandDone, String host, String failoverPartner, String ... user, String password, Boolean trusted, String connectionString) +68 ...
      (microsoft.public.dotnet.framework.aspnet)
    • server-side JavaScript: Prototypes of built-in classes, objects and functins
      ... Session object (disk-based session variables for data persistence ... File class (manipulation of files on server, ie. open, close, read, ... //Methods Cgi.queryCgi.postCgi.anyby default return an empty string if requested var not found ...
      (comp.lang.javascript)
    • Re: App_data - ASPNETDB.MDF
      ... is only because I want my site work, without exception. ... Server Error in '/' Application. ... serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, Int64 ...
      (microsoft.public.dotnet.framework.aspnet)