Results 1 to 5 of 5

Thread: Sending a POST Request in Java

  1. #1
    Join Date
    Nov 2009
    Posts
    877

    Sending a POST Request in Java

    Hello, I am learning java programming language and while learning it I want to know the details about the Sending a POST Request in java. I have search this on internet and got some details which are of no use. If you are having some useful contents then please reply me about the same. I will be thankful to you.

  2. #2
    Join Date
    May 2008
    Posts
    2,012

    Re: Sending a POST Request in Java

    For sending a POST Request in java you need to use the code below :
    Code:
    try { 
    String str = URLEncoder.encode("KeyOne", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8"); 
    str += "&" + URLEncoder.encode("KeyTwo", "UTF-8") + "=" + URLEncoder.encode("value2", "UTF-8"); 
    URL url = new URL("http://hostname:80/cgi"); 
    URLConnection urlconnection = url.openConnection(); 
    urlconnection.setDoOutput(true); 
    OutputStreamWriter outputstreamwriter = new OutputStreamWriter(urlconnection.getOutputStream()); 
    outputstreamwriter.outputstreamwriterite(str); outputstreamwriter.flush(); 
    BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(urlconnection.getInputStream())); 
    String strline; 
    while ((strline = bufferedreader.readLine()) != null) 
    { 
    } 
    outputstreamwriter.close(); 
    bufferedreader.close(); 
    } 
    catch (Exception e) 
    {
     }

  3. #3
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Sending a POST Request in Java

    The Hypertext Transfer Protocol (HTTP) is the most ubiquitous of the Internet protocols. Although seen primarily as a means to fetch pages of Hypertext Markup Language (HTML) content for display in a web browser, HTTP is really a general-purpose transport for any type of data. That versatility is why HTTP support is a fundamental feature of the Mobile Information Device Profile (MIDP). In fact, on many devices HTTP is the only network protocol available. HTTP is a request-response protocol. Various types of requests are possible, each identified by a different HTTP method. MIDP supports two of those methods, GET and POST. In general, you'll use POST requests to transfer data from a MIDP client to a web server, because the web server doesn't log the data you send, and you can send binary data - handy for transferring byte arrays or serialized Java objects. You also avoid the limits on URL length that GET requests face if they include too many query parameters.

  4. #4
    Join Date
    May 2008
    Posts
    2,297

    Re: Sending a POST Request in Java

    Hey, you can simply make use of the code below for post request in java:
    Code:
    import com.POSTsoft.*;
    public class postRequest {
      static {
        try {
            System.loadLibrary("POST");
        } catch (UnsatisfiedLinkError e) {
          System.err.println("Code Error.\n" + e);
          System.exit(1);
        }
      }
      public static void main(String argv[])
      {
        CkHttpRequest request = new CkHttpRequest();
        CkHttp checkHttp = new CkHttp();
        boolean complete;
        complete = checkHttp.UnlockComponent("All");
        if (complete != true) {
            System.out.println(checkHttp.lastErrorText());
            return;
        }
        request.UsePost();
        request.put_Path("/testPostHandler.asp");
        request.AddParam("arg1","Value for arg1.");
        request.AddParam("arg2","Value for arg2.");
        request.AddParam("arg3","Value for arg3.");
        String domain;
        int port;
        boolean ssl;
        domain = "www.TechArena.in";
        port = 80;
        ssl = false;
        CkHttpResponse resp;
        resp = checkHttp.SynchronousRequest(domain,port,ssl,request);
        if (resp == null ) {
            System.out.println(checkHttp.lastErrorText());
        }
        else {
            System.out.println(resp.bodyStr());
        }
    
      }
    }

  5. #5
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Sending a POST Request in Java

    Hello, while searching on the sending a POST Request in java I have got the code below. You will get the information from it:
    Code:
    import org.apache.commons.httphttpclient.HttpClient;
    import org.apache.commons.httphttpclient.HttpStatus;
    import org.apache.commons.httphttpclient.postmethods.PostMethod;
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    public class PostMethodExample {
      public static void main(String args[]) {
        HttpClient httpclient = new HttpClient();
        httpclient.getParams().setParameter("http.useragent", "Test Client");
        BufferedReader bufferedreader = null;
        PostMethod postmethod = new PostMethod("http://<site name>");
        postmethod.addParameter("p", "\"TA\"");
        try{
          int rCode = httpclient.executeMethod(postmethod);
          if(rCode == HttpStatus.SC_NOT_IMPLEMENTED) {
            System.err.println("The Post postmethod is not implemented by this URI");
            postmethod.getResponseBodyAsString();
          } else {
            bufferedreader = new BufferedReader(new InputStreamReader(postmethod.getResponseBodyAsStream()));
            String readLine;
            while(((readLine = bufferedreader.readLine()) != null)) {
              System.err.println(readLine);
          }
          }
        } catch (Exception e) {
          System.err.println(e);
        } finally {
          postmethod.releaseConnection();
          if(bufferedreader != null) try { bufferedreader.close(); 
    } 
    catch (Exception fe)
     {}
        }
    
      }
    }

Similar Threads

  1. Replies: 3
    Last Post: 20-01-2012, 08:46 PM
  2. Error sending end of post message to ME on Toshiba
    By Zared in forum Portable Devices
    Replies: 3
    Last Post: 23-11-2010, 11:08 AM
  3. Sending SOAP Request in java
    By samualres in forum Software Development
    Replies: 5
    Last Post: 09-02-2010, 06:55 PM
  4. JSP form - error in sending request
    By Vodka in forum Software Development
    Replies: 5
    Last Post: 18-01-2010, 02:04 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,219,296.98384 seconds with 17 queries