Talk to a cgi-bin on WEB in POST method not GET

Question:
I know how to fetch content of a URL address in Java, but what do I to write to a CGI script using the HTTP POST method?

Answer:
Passing arguments or data to a CGI script via the POST method is not immediately obvious to the Java programmer attempting to do this for the first time. By default, the URLConnection class uses the GET method, in which case you pass your arguments as part of the URL. To use the POST method, you have to call setDoOutput with an argument of true, get the connection’s output stream, and write your POST data to the stream. After you close the stream, you can start reading from connection’s input stream to obtain the output of the CGI script. The code listing shows how to do this. It takes a URL of a CGI script as an argument, connects to the URL, and reads the POST data from standard input until it encounters the end of file (you can use Ctrl-D). After you’re done typing in the POST data, the example program fetches the output from the CGI script and prints it to standard output.

import java.io.*;import java.net.*;public final class Post {  public static final void main(String args[]) {    String input;    URL url;    URLConnection connection;    InputStream result;    BufferedReader reader;    BufferedWriter writer;    byte[] buffer = new byte[1024];    int bytes;    if(args.length != 1) {      System.err.println("usage: Post ");      return;    }    try {      url = new URL(args[0]);    } catch(MalformedURLException e) {      e.printStackTrace();      return;    }    try {      connection = url.openConnection();      // This tells the connection to use the POST method      connection.setDoOutput(true);      writer =	new BufferedWriter(		   new OutputStreamWriter(connection.getOutputStream()));      // Read the POST data from standard input until EOF      reader = new BufferedReader(new InputStreamReader(System.in));      while((input = reader.readLine()) != null) {	writer.write(input, 0, input.length());	writer.write("
");      }      writer.flush();      writer.close();      // Now that we've written the POST data, we can fetch the contents      // of the URL.      result = connection.getInputStream();      while((bytes = result.read(buffer)) > -1)	System.out.write(buffer, 0, bytes);      System.out.flush();    } catch(IOException e) {      e.printStackTrace();      return;    }  }}
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS