1

I need to create a simple HTTP client program with Java.

I've not found any example of implementation in Java that allows calling the OPTIONS method to get the Allow header with the allowed methods on the server.

I tried using:

HttpURLConnection http = (HttpURLConnection) url.openConnection();
System.out.println(http.getHeaderFields());

But the field Allow: GET, POST ... is not included.

approxiblue
  • 6,624
  • 16
  • 47
  • 56
bcndrass
  • 13
  • 1
  • 3
  • Possible duplicate of [Using java.net.URLConnection to fire and handle HTTP requests](http://stackoverflow.com/questions/2793150/using-java-net-urlconnection-to-fire-and-handle-http-requests) – ArtKorchagin Nov 01 '15 at 22:11
  • In the case, my request message was not correct edited. I've edited the request and now it's working well. Thanks! – bcndrass Nov 07 '15 at 18:27

1 Answers1

3

The connection object fires a GET request by default. You need to set the request method to OPTIONS.

HttpURLConnection conn = (HttpURLConnection) url.openConnection();
System.out.println(conn.getRequestMethod()); // GET
conn.setRequestMethod("OPTIONS");
System.out.println(conn.getHeaderField("Allow")); // depends
approxiblue
  • 6,624
  • 16
  • 47
  • 56