如何从Java发送HTTP请求

如何从Java发送HTTP请求,java,http,Java,Http,我正在创建java模块来解析JSON文件 要接收文件,我需要发送HTTP请求。使用curl时,我的请求如下所示: curl -X GET "https://***" -H "accept: application/json" -H "apikey: ***" HttpClient workingClient = new HttpClient(); workingClient.setReq

我正在创建java模块来解析JSON文件

要接收文件,我需要发送HTTP请求。使用curl时,我的请求如下所示:

curl -X GET "https://***" -H "accept: application/json" -H "apikey: ***"
            HttpClient workingClient = new HttpClient();
            workingClient.setRequestProperty("accept", "application/json;charset=UTF-8");
            workingClient.setRequestProperty("apikey", "***");
            workingClient.setConnectionUrl("https://***");
            ByteBuffer buffer = 
            workingClient.sendHttpRequestForBinaryResponse(HttpMethod.GET);
            
            //or of your API returns contents of file as a string
            String jsonStr = workingClient.sendHttpRequest(HttpMethod.GET);
HttpRequest request2 = HttpRequest.newBuilder()
  .uri(new URI("some url"))
  .header("someHeader", "value1")
  .header("anotherHeader", "value2")
  .GET()
  .build();

如果您使用的是Spring,那么如何从Java发送等效的HTTP请求呢?请尝试WebClient——这在begging中有点难理解(至少比RestTemplate难),但它值得,因为RestTemplate将被中断

你可以在这里找到一个例子

@GetMapping(value=“/tweets非阻塞”,
products=MediaType.TEXT\u事件\u流\u值)
公共流量getTweetsNonBlocking(){
log.info(“启动非阻塞控制器!”);
Flux tweetFlux=WebClient.create()
.get()
.uri(getSlowServiceUri())
.retrieve()
.bodyToFlux(Tweet.class);
订阅(tweet->log.info(tweet.toString());
log.info(“正在退出非阻塞控制器!”);
回流通量;
}

请注意,这是一种非阻塞(例如异步)解决方案,因此您不会立即获得响应,但您可以订阅请求,然后在响应可用时处理响应。WebClient中也有阻塞选项,Java有自己的类,允许您发送HTTP请求。见课堂。但是,我建议使用3d party库,这样可以大大简化此任务。好的图书馆应该是或。我还可以让您使用另一个具有HTTP客户端的开源库。它叫MgntUtils库,是我写的。在这种情况下,您的代码如下所示:

curl -X GET "https://***" -H "accept: application/json" -H "apikey: ***"
            HttpClient workingClient = new HttpClient();
            workingClient.setRequestProperty("accept", "application/json;charset=UTF-8");
            workingClient.setRequestProperty("apikey", "***");
            workingClient.setConnectionUrl("https://***");
            ByteBuffer buffer = 
            workingClient.sendHttpRequestForBinaryResponse(HttpMethod.GET);
            
            //or of your API returns contents of file as a string
            String jsonStr = workingClient.sendHttpRequest(HttpMethod.GET);
HttpRequest request2 = HttpRequest.newBuilder()
  .uri(new URI("some url"))
  .header("someHeader", "value1")
  .header("anotherHeader", "value2")
  .GET()
  .build();

之后,ByteBuffer缓冲区或字符串jsonStr将保存JSON文件的内容。现在你可以用它做任何你需要的事情。上课时间到了。MgntUtils库可以作为maven工件或在(包括源代码和Javadoc)上获得。

Java有很多选项可以使用HTTP

选项1

自Java9以来,就有一个内置的HTTP客户机。因此,您可以使用它创建请求,而无需任何第三方库

一个简单的例子如下:

curl -X GET "https://***" -H "accept: application/json" -H "apikey: ***"
            HttpClient workingClient = new HttpClient();
            workingClient.setRequestProperty("accept", "application/json;charset=UTF-8");
            workingClient.setRequestProperty("apikey", "***");
            workingClient.setConnectionUrl("https://***");
            ByteBuffer buffer = 
            workingClient.sendHttpRequestForBinaryResponse(HttpMethod.GET);
            
            //or of your API returns contents of file as a string
            String jsonStr = workingClient.sendHttpRequest(HttpMethod.GET);
HttpRequest request2 = HttpRequest.newBuilder()
  .uri(new URI("some url"))
  .header("someHeader", "value1")
  .header("anotherHeader", "value2")
  .GET()
  .build();
有关更多示例,请参见

选项2

使用第三方图书馆,有很多:,更多的“老派”

选项3

如果使用Spring,可以考虑使用Spring的WebCclipse。spring中也有类似RestTemplate的包装器,可以很方便地使用,但这取决于您希望使用什么

许多客户端都附带了应该正确设置的http连接池。
此外,在您的示例中,我看到您使用https—所有这些客户端都支持https,但应该正确设置它。

如果您使用本机Java,这可能会对您有所帮助,例如,如果您使用Spring boot,这会很容易!另请看一下
restemplate
,它可能会帮助您的esttemplate停止使用,因此如果您计划使用此长期解决方案,请尝试Spring的WebClient。这是Spring特有的解决方案。OP要求提供一个非常通用的选项来从Java发送HTTP请求。@MichaelGantman他没有说不要使用任何框架,尽管如此,为什么要使用downvote,有很多选项,spring是其中之一您是正确的-spring是其中一个选项。如果他使用弹簧,那么是的,这将是一种方法。但如果他不这么做,那么仅仅使用Spring发出一个简单的HTTP请求就太过分了。另外,你的回答有误导性,让他认为如果他需要发送HTTP请求,Spring将是第一个考虑的选项。发送HTTP请求有更简单的方法。因此,投票被否决。(请不要认为这是针对个人的)。@MichaelGantman我不这么认为。我认为Mark Bramnik的答案是最好的,因为它提供了多种方法,这只是我想到的第一个答案,我没有想到OP可能会被误导,认为他需要第三方框架,所以我将把这一点带到我未来的答案中。实际上,我自己对Mark Bramnik的答案投了更高的票。。。。