Sending a POST Request with Parameters From a Java Class : URLConnection « Network Protocol « Java






Sending a POST Request with Parameters From a Java Class

      

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;

public class Main {

  public static void main(String[] args) throws Exception {
    URL url = new URL("http://www.java2s.com");
    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);
    OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());

    writer.write("value=1&anotherValue=1");
    writer.flush();
    String line;
    BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    while ((line = reader.readLine()) != null) {
      System.out.println(line);
    }
    writer.close();
    reader.close();

  }
}

   
    
    
    
    
    
  








Related examples in the same category

1.Demonstrate URLConnection.
2.Call a servlet from a Java command line application
3.A CGI POST Example
4.Http authentication header
5.URLConnection.setRequestProperty
6.File size from URL
7.Sending a POST Request Using a URL
8.Check if a file was modified on the server
9.Getting Text from a URL
10.Getting an Image from a URL
11.Downloading a web page using URL and URLConnection classes
12.Get response header from HTTP request
13.Read / download webpage content
14.Read a GIF or CLASS from an URL save it locally
15.Zip URLConnection
16.Zip URLStream Handler
17.Http Parser
18.Http Constants
19.Get URLConnection Expiration
20.Locating files by path or URL
21.Download from URL
22.This program demonstrates how to use the URLConnection class for a POST request.
23.Connects to an URL and displays the response header data and the first 10 lines of the requested data.Connects to an URL and displays the response header data and the first 10 lines of the requested data.
24.Load content from URL to string