HttpURLConnection
2016-04-24 23:15
519 查看
HttpURLConnection类的作用是通过HTTP协议向服务器发送请求,并可以获取服务器发回的数据。
HttpURLConnection来自于jdk,它的完整名称为:java.net.HttpURLConnection
HttpURLConnection类,没有公开的构造方法,但我们可以通过java.net.URL的openConnection方法获取一个URLConnection的实例,而HttpURLConnection是它的子类。
URL url = new URL("http://localhost:8080");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
方法:
conn.getResponseCode():获取响应码
conn.getResponseMessage():获取响应码描述
conn.getHeaderField("Server"):获取响应头
conn.getInputStream():获取正文输入流
示例:示例简单获取服务器数据
//建立与服务器的URL对像
URL url = new URL("http://localhost:9999/day05/servlet/Servlet1");
//打开连接
HttpURLConnection con = (HttpURLConnection)url.openConnection();
//获取服务器的输入流
InputStream in = con.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String str = "";
while((str=br.readLine())!=null){
System.err.println(str);
}
con.disconnect();
示例:向服务器发消息默认请求到doGet方式
URL url = new URL("http://localhost:9999/day05/index.jsp");
HttpURLConnection con = (HttpURLConnection)url.openConnection();
//1、打开可以向服务器发消息
con.setDoOutput(true);
conn.setRequestProperty("xxx", "yyy");//发送请求头
OutputStream out = con.getOutputStream();
out.write(“name=wzhting”.getBytes());//发送正文数据
//2、获取状态码,以表示完成请求
int code = con.getResponseCode();
System.err.println(code);
示例:用POST方法
URL url = new URL("http://localhost:9999/day05/servlet/TestConnection");
HttpURLConnection con = (HttpURLConnection)url.openConnection();
//1、设置请求方式为post
con.setRequestMethod("POST");
//可以向服务器发消息
con.setDoOutput(true);
OutputStream out = con.getOutputStream();
out.write("name=wzhting".getBytes));
//获取状态码,以表示完成请求
int code = con.getResponseCode();
System.err.println(code);
HttpURLConnection来自于jdk,它的完整名称为:java.net.HttpURLConnection
HttpURLConnection类,没有公开的构造方法,但我们可以通过java.net.URL的openConnection方法获取一个URLConnection的实例,而HttpURLConnection是它的子类。
URL url = new URL("http://localhost:8080");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
方法:
conn.getResponseCode():获取响应码
conn.getResponseMessage():获取响应码描述
conn.getHeaderField("Server"):获取响应头
conn.getInputStream():获取正文输入流
示例:示例简单获取服务器数据
//建立与服务器的URL对像
URL url = new URL("http://localhost:9999/day05/servlet/Servlet1");
//打开连接
HttpURLConnection con = (HttpURLConnection)url.openConnection();
//获取服务器的输入流
InputStream in = con.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String str = "";
while((str=br.readLine())!=null){
System.err.println(str);
}
con.disconnect();
示例:向服务器发消息默认请求到doGet方式
URL url = new URL("http://localhost:9999/day05/index.jsp");
HttpURLConnection con = (HttpURLConnection)url.openConnection();
//1、打开可以向服务器发消息
con.setDoOutput(true);
conn.setRequestProperty("xxx", "yyy");//发送请求头
OutputStream out = con.getOutputStream();
out.write(“name=wzhting”.getBytes());//发送正文数据
//2、获取状态码,以表示完成请求
int code = con.getResponseCode();
System.err.println(code);
示例:用POST方法
URL url = new URL("http://localhost:9999/day05/servlet/TestConnection");
HttpURLConnection con = (HttpURLConnection)url.openConnection();
//1、设置请求方式为post
con.setRequestMethod("POST");
//可以向服务器发消息
con.setDoOutput(true);
OutputStream out = con.getOutputStream();
out.write("name=wzhting".getBytes));
//获取状态码,以表示完成请求
int code = con.getResponseCode();
System.err.println(code);
相关文章推荐
- 20159302《网络攻击与防范》第八周学习总结
- 网络七层协议的形象说明
- T-02. 网络测试工具 IxChariot 6.7 下载与安装
- 【Android-HTTP】关于HTTP方面的总结
- 诊断篇(5.2) 03. 外网接口性能测试 ❀ 飞塔 (Fortinet) 防火墙
- 数据提取----httpclient,htmlparser,xpath
- 诊断篇(5.2) 02. 内网接口性能测试 ❀ 飞塔 (Fortinet) 防火墙
- socket发送http请求
- TCP/IP、Http、Socket的区别
- TCP/IP SOCKET HTTP及HTTPS之间的关系及各自特性之总结
- C# 网络通信大小端转换类
- stm32 网络编程
- TCP/IP和http全称和概述
- 基于SNMP协议发现网络中路由器、交换机和打印机的简单方法
- http请求和消息
- ACM-东北大学程序设计竞赛-网络赛(2016.04.16)
- Linux 网络层实现
- restful使用不同的http访问方法请求URL
- 软考之路(一)——网络知识
- 分析移动端APP的网络请求抓包