News:

Choose a design and let our professionals help you build a successful website   - ITAcumens

Main Menu

HttpURLConnection SetRequestMethod() Example

Started by VelMurugan, Dec 22, 2008, 08:46 PM

Previous topic - Next topic

VelMurugan

HttpURLConnection SetRequestMethod() Example

Set the method for the URL request,

HttpURLConnectionclass SetRequestMethod() method example. This example shows you how to use SetRequestMethod() method.This method Set the method for the URL request, one of: GET POST HEAD OPTIONS PUT DELETE TRACE are legal, subject to protocol restrictions.

Here is the code:-

/*
* @Program that Set the method for the URL request
* SetRequestMethod.java
* Author:-RoseIndia Team
* Date:-25-Jun-2008
*/

import java.net.*;
import javax.annotation.PostConstruct;

public class SetRequestMethod {

    public static void main(String[] args) throws Exception {
        URL url = new URL("http://192.168.10.211:8080");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        //Get the request method.
        String s = connection.getRequestMethod();
        System.out.println("Get the request method: " + s);
        //Set the method for the URL request
        connection.setRequestMethod("POST");
         String s1 = connection.getRequestMethod();
        System.out.println("Get the request method: " + s1);
    }
}


Output of the program:-

Get the request method: GET
Get the request method: POST

Source : codingdiary