1 import java.io.BufferedReader; 2 import java.io.DataOutputStream; 3 import java.io.IOException; 4 import java.io.InputStreamReader; 5 import java.net.HttpURLConnection; 6 import java.net.URL; 7 import java.util.Map; 8 9 /** 10 * HTTP请求类 11 * @author 12 */ 13 public class HttpInvoker { 14 15 /** 16 * GET请求 17 * @param getUrl 18 * @throws IOException 19 * @return 提取HTTP响应报文包体,以字符串形式返回 20 */ 21 public static String httpGet(String getUrl) throws IOException { 22 //1 把字符串形式的网址转成 java.net.URL对象 23 URL getURL = new URL(getUrl); 24 /* 25 * 2 通过java.net.URL打开一个连接(获取一个代表与远程对象的URLConnection对象),并转为 HttpURLConnection , 26 Returns a URLConnection object that represents a connection 27 to the remote object referred to by the URL. 28 */ 29 HttpURLConnection connection = (HttpURLConnection) getURL.openConnection(); 30 //3 通过HttpURLConnection对象真正获取连接 31 connection.connect(); 32 //4 连接后那么这个远程连接对象就可以通过自身的一些方法读取到连接的信息了,这里是把流转成字符串 33 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream())); 34 StringBuilder sbStr = new StringBuilder(); 35 String line; 36 while ((line = bufferedReader.readLine()) != null) { 37 sbStr.append(line); 38 } 39 bufferedReader.close(); 40 connection.disconnect(); 41 return new String(sbStr.toString().getBytes(),"utf-8"); 42 } 43 44 45 public static void main(String[] args) throws IOException { 46 String url = "https://www.hao123.com/?tn=99682755_hao_pg"; 47 String rtn_json = httpGet(url); 48 System.out.println(""+rtn_json); 49 } 50 51 /** 52 * POST请求 53 * @param postUrl 54 * @param postHeaders 55 * @param postEntity 56 * @throws IOException 57 * @return 提取HTTP响应报文包体,以字符串形式返回 58 */ 59 public static String httpPost(String postUrl,Map<String, String> postHeaders, String postEntity) throws IOException { 60 61 URL postURL = new URL(postUrl); 62 HttpURLConnection httpURLConnection = (HttpURLConnection) postURL.openConnection(); 63 httpURLConnection.setDoOutput(true); 64 httpURLConnection.setDoInput(true); 65 httpURLConnection.setRequestMethod("POST"); 66 httpURLConnection.setUseCaches(false); 67 httpURLConnection.setInstanceFollowRedirects(true); 68 httpURLConnection.setRequestProperty(" Content-Type ", " application/x-www-form-urlencoded "); 69 70 if(postHeaders != null) { 71 for(String pKey : postHeaders.keySet()) { 72 httpURLConnection.setRequestProperty(pKey, postHeaders.get(pKey)); 73 } 74 } 75 if(postEntity != null) { 76 DataOutputStream out = new DataOutputStream(httpURLConnection.getOutputStream()); 77 out.writeBytes(postEntity); 78 out.flush(); 79 out.close(); // flush and close 80 } 81 //connection.connect(); 82 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream())); 83 StringBuilder sbStr = new StringBuilder(); 84 String line; 85 while ((line = bufferedReader.readLine()) != null) { 86 sbStr.append(line); 87 } 88 bufferedReader.close(); 89 httpURLConnection.disconnect(); 90 return new String(sbStr.toString().getBytes(),"utf-8"); 91 } 92 93 }
时间: 2024-10-06 07:38:26