2015-11-01 78 views
0

我需要使用Java创建一个简单的HTTP客户端程序。使用Java的HTTP OPTIONS方法

我还没有在Java中找到实现的任何实例,它允许调用OPTIONS方法来获取Allow头和服务器上允许的方法。

我试着使用:

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

但不包括外地Allow: GET, POST ...

+0

[使用java.net.URLConnection中的火灾和处理HTTP请求(的可能的复制http://stackoverflow.com/questions/2793150/using-java-net-urlconnection-to-fire-and-处理http请求) –

+0

在这种情况下,我的请求消息编辑不正确。我编辑了这个请求,现在它运行良好。谢谢! – bcndrass

回答

1

默认情况下,连接对象会触发GET请求。您需要set the request method到选项。

HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
System.out.println(conn.getRequestMethod()); // GET 
conn.setRequestMethod("OPTIONS"); 
System.out.println(conn.getHeaderField("Allow")); // depends 
相关问题