简体   繁体   English

我可以使用相同的 HttpURLConnection 多次发送相同的 HTTP 请求吗?

[英]can I send same HTTP request with same HttpURLConnection multiple times?

In order to send a string data to the server once, I do as below:为了向服务器发送一次字符串数据,我执行以下操作:

  1. Make “HttpURLConnection” to my URL address and open it将“HttpURLConnection”添加到我的 URL 地址并打开它

  2. Set the required headers设置所需的标题

  3. for the connection I Set setDoOutput to True对于我将 setDoOutput 设置为 True 的连接

  4. new a DataOutputStream from my connection and finally write my string data to it.从我的连接中新建一个 DataOutputStream,最后将我的字符串数据写入它。

     HttpURLConnection myConn = (HttpURLConnection); myUrl.openConnection(); myConn.setRequestProperty("Accept", "application/json, text/plain, */*"); myConn.setDoOutput(true); DataOutputStream my_output = new DataOutputStream(myConn.getOutputStream()); my_output.write(myData.getBytes("UTF-8"));

But what to do if I want to send exactly the same data with same URl and headers multiple times?但是,如果我想用相同的 URl 和标头多次发送完全相同的数据怎么办? Can I write to it multiple times?(I mean that is it possible to use the last line of code multiple times?) Or should I repeat the above steps and try it with a new connection?我可以多次写入吗?(我的意思是可以多次使用最后一行代码吗?)或者我应该重复上述步骤并尝试使用新连接吗? And if yes should I wait for some second or millisecond before sending the next one?如果是,我应该在发送下一个之前等待几秒钟或几毫秒吗? I also searched for some other alternatives such as “HttpClient” Http API and making synchronous Http request which as far as I got can help me setting the headers only once.我还搜索了其他一些替代方案,例如“HttpClient” Http API 并发出同步 Http 请求,据我所知,这只能帮助我设置一次标题。 At the end, I appreciate your help and support and any other alternatives would be welcome.最后,我感谢您的帮助和支持,欢迎任何其他替代方案。 Thanks a million.太感谢了。

I understand that the question has be answered in the comments, but I am leaving this here so that future viewers can see it.我知道评论中已经回答了这个问题,但我把它留在这里,以便未来的观众可以看到它。

An HTTP request contains 3 main parts: HTTP 请求包含 3 个主要部分:

  • Request Line: Method, Path, Protocol请求行: Method, Path, Protocol
  • Headers: Key-Pairs标头: Key-Pairs
  • Body: Data正文: Data

Running my_output.write() will just add bytes to the body until my_output.flush() has been executed.运行my_output.write()只会将字节添加到正文中,直到执行my_output.flush()为止。 Flushing the stream will write the data to the server.刷新 stream 会将数据写入服务器。

Because HTTP requests are usually closed by the server once all data has been sent/received, whether or not you create a new connection or just add on to the body depends on your intentions .因为 HTTP 请求通常会在所有数据发送/接收后被服务器关闭,所以您是否创建新连接或只是添加到主体取决于您的意图 Typically, clients will create a new connection for each request because each response should be handled individually, rather than sending a repetitive body.通常,客户端将为每个请求创建一个新连接,因为每个响应都应该单独处理,而不是发送重复的正文。 This will vary though because some servers choose to hold a connection (such as WebSockets).但是,这会有所不同,因为某些服务器选择保持连接(例如 WebSockets)。

thanks to paulsm4 and null who gave me a better insight toward http connection persistence and life cycle,?感谢 paulsm4 和 null 让我更好地了解 http 连接持久性和生命周期,? but about the second part of my question, any other alternative to send the request to the server with the same 3 main parts.但关于我问题的第二部分,任何其他替代方法都可以使用相同的 3 个主要部分将请求发送到服务器。 since making new connections and setting headers take times which decrease my performance at this part of code?因为建立新连接和设置标头需要时间,这会降低我在这部分代码中的性能? I want to send them as fast as possible.我想尽快发送它们。 would using asynchronous http request helpful at this predicament?使用异步 http 请求对这种困境有帮助吗? Thanks all.谢谢大家。

If you are open to external libraries, you may find this chart insightful:如果您对外部库持开放态度,您可能会发现此图表很有见地: HTTP 库图表

AsyncHttpClient would be a good fit for your intentions. AsyncHttpClient将非常适合您的意图。

Alternatively, you can use cURL by running a terminal command with Runtime.getRuntime().exec() .或者,您可以通过运行带有Runtime.getRuntime().exec()的终端命令来使用cURL More information about using cURL with POST Requests can be found here .更多关于使用cURL和 POST 请求的信息可以在这里找到。 While cURL is efficient, you have to depend on the fact that your OS supports the command (though usually all devices that can run Java have this command).虽然cURL是有效的,但您必须依赖于您的操作系统支持该命令的事实(尽管通常所有可以运行 Java 的设备都有此命令)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android HttpUrlConnection使用相同的键发送多个参数 - Android HttpUrlConnection send multiple parameters with same key 如何使用HttpURLConnection以与Apache HttpClient lib相同的方式发出POST请求 - How can I make a POST request in the same manner as Apache HttpClient lib, using HttpURLConnection 在HttpURLConnection中发送PUT,DELETE HTTP请求 - Send PUT, DELETE HTTP request in HttpURLConnection 如何在 HttpURLConnection 中发送 PUT、DELETE HTTP 请求? - How to send PUT, DELETE HTTP request in HttpURLConnection? 为什么 HttpURLConnection 不发送 HTTP 请求 - Why does HttpURLConnection not send the HTTP request 我可以使用与不同请求体相同的请求映射创建多个HTTP POST方法 - Can I create multiple HTTP POST methods with same request mapping with different requestbody 由于多次创建/更新请求,同一文档多次插入 - Same Document inserted multiple times due to multiple request of creation/updation 如何使用 OpenAPI 3.0 创建同一请求的多个版本? - How can I create multiple versions of the same request with OpenAPI 3.0? Java是否可以触发同一事件多次并同时运行? - Java Can the same event be triggerd multiple times and be running at the same time? 如何在JAVA中的HttpURLConnection中发送PUT,DELETE HTTP请求 - How to send PUT, DELETE HTTP request in HttpURLConnection in JAVA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM