Java 发送http请求 HttpURLConnection
2017-10-20 17:40
671 查看
package tag; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; public class HttpURLConnectionUtil { public static void sendGet() { String path = "http://localhost/news/getlist"; try { // 1. 得到访问地址的URL URL url = new URL(path); // 2. 得到网络访问对象java.net.HttpURLConnection HttpURLConnection connection = (HttpURLConnection) url .openConnection(); /* 3. 设置请求参数(过期时间,输入、输出流、访问方式),以流的形式进行连接 */ // 设置是否向HttpURLConnection输出 connection.setDoOutput(false); // 设置是否从httpUrlConnection读入 connection.setDoInput(true); // 设置请求方式 connection.setRequestMethod("GET"); // 设置是否使用缓存 connection.setUseCaches(true); // 设置此 HttpURLConnection 实例是否应该自动执行 HTTP 重定向 connection.setInstanceFollowRedirects(true); // 设置超时时间 connection.setConnectTimeout(3000); // 连接 connection.connect(); // 4. 得到响应状态码的返回值 responseCode int code = connection.getResponseCode(); // 5. 如果返回值正常,数据在网络中是以流的形式得到服务端返回的数据 String msg = ""; if (code == 200) { // 正常响应 // 从流中读取响应信息 BufferedReader reader = new BufferedReader( new InputStreamReader(connection.getInputStream())); String line = null; while ((line = reader.readLine()) != null) { // 循环从流中读取 msg += line + "\n"; } reader.close(); // 关闭流 } // 6. 断开连接,释放资源 connection.disconnect(); // 显示响应结果 System.out.println(msg); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void sendPOST() { try { // 1. 获取访问地址URL URL url = new URL("http://localhost/news/getlist"); // 2. 创建HttpURLConnection对象 HttpURLConnection connection = (HttpURLConnection) url .openConnection(); /* 3. 设置请求参数等 */ // 请求方式 connection.setRequestMethod("POST"); // 超时时间 connection.setConnectTimeout(3000); // 设置是否输出 connection.setDoOutput(true); // 设置是否读入 connection.setDoInput(true); // 设置是否使用缓存 connection.setUseCaches(false); // 设置此 HttpURLConnection 实例是否应该自动执行 HTTP 重定向 connection.setInstanceFollowRedirects(true); // 设置使用标准编码格式编码参数的名-值对 connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // 连接 connection.connect(); /* 4. 处理输入输出 */ // 写入参数到请求中 String params = "pageSize=1"; OutputStream out = connection.getOutputStream(); out.write(params.getBytes()); out.flush(); out.close(); // 从连接中读取响应信息 String msg = ""; int code = connection.getResponseCode(); if (code == 200) { BufferedReader reader = new BufferedReader( new InputStreamReader(connection.getInputStream())); String line; while ((line = reader.readLine()) != null) { msg += line + "\n"; } reader.close(); } // 5. 断开连接 connection.disconnect(); // 处理结果 System.out.println(msg); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { // sendGet(); sendPOST(); } }
相关文章推荐
- Java发送http请求(httpUrlConnection)安卓客户端也可以使用
- java-用HttpURLConnection发送Http请求.
- Java 利用HttpURLConnection发送http请求
- 【JAVA】通过URLConnection/HttpURLConnection发送HTTP请求的方法(一)
- JAVA发送Https请求(HttpsURLConnection和HttpURLConnection)区别
- 关于JAVA发送Https请求(HttpsURLConnection和HttpURLConnection)
- JAVA中使用HttpURLConnection发送XML参数乱码问题
- JAVA使用原始HttpURLConnection发送POST数据
- 在JAVA中使用HttpURLConnection发送POST请求
- 通过java.net.URLConnection发送HTTP请求的方法
- 使用java.net.URLConnection发送http请求
- Android中使用HttpURLConnection和HttpClient发送Http请求
- Android 使用HttpURLConnection发送HTTP请求
- java发送post和get请求(1)---HttpURLConnection方式
- java httpurlconnection 发送cookie时,cookie要在Post前发送
- 谈谈Java利用原始HttpURLConnection发送POST数据
- 关于JAVA发送Https请求(HttpsURLConnection和HttpURLConnection)
- 通过java.net.URLConnection发送HTTP请求的方法
- Java利用HttpURLConnection发送请求
- java HttpURLConnection 模拟发送浏览器请求