1.获取HttpURLConnection实例,一般new出一个URL对象并且传入目标网络地址,再调用openConnection();
URL url = new URL("http://localhost:8080/TestHttpURLConnectionPro/index.jsp");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
2.设置Get/Post
connection.setRequestMethod("POST");
3.设置链接超时、读取超时毫秒数,消息头等
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
4.getInputStream()获取服务器返回的输入流
InputStream in = connection.getInputStream();
5.关闭HTTP链接
connection.disConnect();
网友评论