Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use HttpURLConnection in java

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "how to use HttpURLConnection in java". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The code only needs jdk and does not need to rely on third-party jar packages

Package com.sean; import sun.net.www.protocol.http.HttpURLConnection; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URL; public class HttpURLConnectionTest {public static void main (String [] args) {sendRequestWithHttpURLConnection () } private static void sendRequestWithHttpURLConnection () {new Thread (new Runnable () {@ Override public void run () {HttpURLConnection connection = null) BufferedReader reader = null; try {URL url = new URL ("http://127.0.0.1:10000/service/sean?desc=description"); Connection = (HttpURLConnection) url.openConnection (); connection.setRequestMethod ("GET"); connection.setConnectTimeout (8000); connection.setReadTimeout (8000) / / what is obtained at this time is byte stream InputStream in = connection.getInputStream () / / A pair of acquired input streams are read reader = new BufferedReader (new InputStreamReader (in)); / / convert the byte stream into a character stream StringBuilder response = new StringBuilder () String line; while ((line = reader.readLine ())! = null) {response.append (line) } System.out.println (response.toString ());} catch (Exception e) {e.printStackTrace () } finally {if (reader! = null) {try {reader.close () } catch (IOException e) {e.printStackTrace () }} if (connectionmakers = null) {connection.disconnect () }}). Start ();}} "how to use HttpURLConnection in java" is introduced here. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report