通过java.net.URLConnection发送HTTP请求的方法

  1 package cn.smartercampus.cloud.core.util;
  2 
  3 import java.io.BufferedReader;
  4 import java.io.InputStream;
  5 import java.io.InputStreamReader;
  6 import java.io.OutputStream;
  7 import java.io.OutputStreamWriter;
  8 import java.net.HttpURLConnection;
  9 import java.net.URL;
 10 import java.net.URLConnection;
 11 
 12 
 13 
 14 public class myHttpUtil {
 15     
 16     public static void setUserInfo() throws Exception {
 17         doGet("http://localhost/api/setUserInfo_test");
 18     }
 19     
 20     public static void main(String[] args) throws Exception {
 21         System.out.println(doGet("http://localhost/api/queryForList/prepare.getMyCollectBeike"));
 22     }
 23     
 24     
 25     /**
 26      * Post Request
 27      * @return
 28      * @throws Exception
 29      */
 30     public static String doPost(String url) throws Exception {
 31         String parameterData = "test=test&2=2";
 32         
 33         URL localURL = new URL(url);
 34         URLConnection connection = localURL.openConnection();
 35         HttpURLConnection httpURLConnection = (HttpURLConnection)connection;
 36         
 37         httpURLConnection.setDoOutput(true);
 38         httpURLConnection.setRequestMethod("POST");
 39         httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");
 40         httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
 41         httpURLConnection.setRequestProperty("Content-Length", String.valueOf(parameterData.length()));
 42         
 43         OutputStream outputStream = null;
 44         OutputStreamWriter outputStreamWriter = null;
 45         InputStream inputStream = null;
 46         InputStreamReader inputStreamReader = null;
 47         BufferedReader reader = null;
 48         StringBuffer resultBuffer = new StringBuffer();
 49         String tempLine = null;
 50         
 51         try {
 52             outputStream = httpURLConnection.getOutputStream();
 53             outputStreamWriter = new OutputStreamWriter(outputStream);
 54             
 55             outputStreamWriter.write(parameterData.toString());
 56             outputStreamWriter.flush();
 57             
 58             if (httpURLConnection.getResponseCode() >= 300) {
 59                 throw new Exception("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
 60             }
 61             
 62             inputStream = httpURLConnection.getInputStream();
 63             inputStreamReader = new InputStreamReader(inputStream);
 64             reader = new BufferedReader(inputStreamReader);
 65             
 66             while ((tempLine = reader.readLine()) != null) {
 67                 resultBuffer.append(tempLine);
 68             }
 69             
 70         } finally {
 71             
 72             if (outputStreamWriter != null) {
 73                 outputStreamWriter.close();
 74             }
 75             
 76             if (outputStream != null) {
 77                 outputStream.close();
 78             }
 79             
 80             if (reader != null) {
 81                 reader.close();
 82             }
 83             
 84             if (inputStreamReader != null) {
 85                 inputStreamReader.close();
 86             }
 87             
 88             if (inputStream != null) {
 89                 inputStream.close();
 90             }
 91             
 92         }
 93 
 94         return resultBuffer.toString();
 95     }
 96 
 97     
 98     /**
 99      * Get Request
100      * @return
101      * @throws Exception
102      */
103     public static String doGet(String url) throws Exception {
104         URL localURL = new URL(url);
105         URLConnection connection = localURL.openConnection();
106         HttpURLConnection httpURLConnection = (HttpURLConnection)connection;
107         
108         httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");
109         httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
110         
111         InputStream inputStream = null;
112         InputStreamReader inputStreamReader = null;
113         BufferedReader reader = null;
114         StringBuffer resultBuffer = new StringBuffer();
115         String tempLine = null;
116         
117         if (httpURLConnection.getResponseCode() >= 300) {
118             throw new Exception("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
119         }
120         
121         try {
122             inputStream = httpURLConnection.getInputStream();
123             inputStreamReader = new InputStreamReader(inputStream);
124             reader = new BufferedReader(inputStreamReader);
125             
126             while ((tempLine = reader.readLine()) != null) {
127                 resultBuffer.append(tempLine);
128             }
129             
130         } finally {
131             
132             if (reader != null) {
133                 reader.close();
134             }
135             
136             if (inputStreamReader != null) {
137                 inputStreamReader.close();
138             }
139             
140             if (inputStream != null) {
141                 inputStream.close();
142             }
143             
144         }
145         
146         return resultBuffer.toString();
147     }
148     
149 }

猜你喜欢

转载自www.cnblogs.com/remember-forget/p/9429363.html