java中原生的发送http请求(无任何的jar包导入)

  1 package com.teamsun.pay.wxpay.util;
  2 
  3 import java.io.BufferedReader;
  4 import java.io.IOException;
  5 import java.io.InputStreamReader;
  6 import java.io.PrintWriter;
  7 import java.net.URL;
  8 import java.net.URLConnection;
  9 import java.util.List;
 10 import java.util.Map;
 11 
 12 public class HttpRequest {
 13     /**
 14      * 向指定URL发送GET方法的请求
 15      * 
 16      * @param url
 17      *            发送请求的URL
 18      * @param param
 19      *            请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
 20      * @return URL 所代表远程资源的响应结果
 21      */
 22     public static String sendGet(String url, String param) {
 23         String result = "";
 24         BufferedReader in = null;
 25         try {
 26             String urlNameString = url + "?" + param;
 27             URL realUrl = new URL(urlNameString);
 28             // 打开和URL之间的连接
 29             URLConnection connection = realUrl.openConnection();
 30             // 设置通用的请求属性
 31             connection.setRequestProperty("accept", "*/*");
 32             connection.setRequestProperty("connection", "Keep-Alive");
 33             connection.setRequestProperty("user-agent",
 34                     "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
 35             //setConnectTimeout:设置连接主机超时(单位:毫秒)
 36             connection.setConnectTimeout(2000);//2秒超时
 37             //setReadTimeout:设置从主机读取数据超时(单位:毫秒)
 38             //connection.setReadTimeout(2000);
 39             
 40             // 建立实际的连接
 41             connection.connect();
 42             // 获取所有响应头字段
 43             Map<String, List<String>> map = connection.getHeaderFields();
 44             // 遍历所有的响应头字段
 45             for (String key : map.keySet()) {
 46                 System.out.println(key + "--->" + map.get(key));
 47             }
 48             // 定义 BufferedReader输入流来读取URL的响应
 49             in = new BufferedReader(new InputStreamReader(
 50                     connection.getInputStream()));
 51             String line;
 52             while ((line = in.readLine()) != null) {
 53                 result += line;
 54             }
 55         } catch (Exception e) {
 56             System.out.println("发送GET请求出现异常!" + e);
 57             e.printStackTrace();
 58         }
 59         // 使用finally块来关闭输入流
 60         finally {
 61             try {
 62                 if (in != null) {
 63                     in.close();
 64                 }
 65             } catch (Exception e2) {
 66                 e2.printStackTrace();
 67             }
 68         }
 69         return result;
 70     }
 71     
 72     /**
 73      * 向指定 URL 发送POST方法的请求
 74      * 
 75      * @param url
 76      *            发送请求的 URL
 77      * @param param
 78      *            请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
 79      * @return 所代表远程资源的响应结果
 80      */
 81     public static String sendPost(String url, String param) {
 82         PrintWriter out = null;
 83         BufferedReader in = null;
 84         String result = "";
 85         try {
 86             URL realUrl = new URL(url);
 87             // 打开和URL之间的连接
 88             URLConnection conn = realUrl.openConnection();
 89             // 设置通用的请求属性
 90             conn.setRequestProperty("accept", "*/*");
 91             conn.setRequestProperty("connection", "Keep-Alive");
 92             conn.setRequestProperty("user-agent",
 93                     "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
 94             // 发送POST请求必须设置如下两行
 95             conn.setDoOutput(true);
 96             conn.setDoInput(true);
 97             // 获取URLConnection对象对应的输出流
 98             out = new PrintWriter(conn.getOutputStream());
 99             // 发送请求参数
100             out.print(param);
101             // flush输出流的缓冲
102             out.flush();
103             // 定义BufferedReader输入流来读取URL的响应
104             in = new BufferedReader(
105                     new InputStreamReader(conn.getInputStream()));
106             String line;
107             while ((line = in.readLine()) != null) {
108                 result += line;
109             }
110         } catch (Exception e) {
111             System.out.println("发送 POST 请求出现异常!"+e);
112             e.printStackTrace();
113         }
114         //使用finally块来关闭输出流、输入流
115         finally{
116             try{
117                 if(out!=null){
118                     out.close();
119                 }
120                 if(in!=null){
121                     in.close();
122                 }
123             }
124             catch(IOException ex){
125                 ex.printStackTrace();
126             }
127         }
128         return result;
129     }
130     
131     
132     public static void main(String[] args) {
133         
134 
135     }
136 }
137 
138     
原文地址:https://www.cnblogs.com/guoyansi19900907/p/7590605.html