JAVA 解析响应HTTP请求

  1 //http请求类
  2 import java.io.BufferedReader;
  3 import java.io.IOException;
  4 import java.io.InputStream;
  5 import java.io.InputStreamReader;
  6 import java.net.HttpURLConnection;
  7 import java.net.URL;
  8 import java.nio.charset.Charset;
  9 import java.util.Map;
 10 import java.util.Vector;
 11 /**
 12  * HTTP请求
 13  */
 14 public class HttpRequester
 15 {
 16     /**
 17      * 默认响应字符集
 18      */
 19     private String defaultContentEncoding;
 20     public HttpRequester()
 21     {
 22         this.defaultContentEncoding = Charset.defaultCharset().name();
 23     }
 24     /**
 25      * 发送GET请求
 26      * @param urlString URL地址
 27      * @return 响应对象
 28      * @throws IOException
 29      */
 30     public HttpRespons sendGet(String urlString) throws IOException
 31     {
 32         return this.send(urlString, "GET", null, null);
 33     }
 34     /**
 35      * 发送GET请求
 36      * @param urlString URL地址
 37      * @param params 参数集合
 38      * @return 响应对象
 39      * @throws IOException
 40      */
 41     public HttpRespons sendGet(String urlString, Map<String, String> params) throws IOException
 42     {
 43         return this.send(urlString, "GET", params, null);
 44     }
 45     /**
 46      * 发送GET请求
 47      * @param urlString URL地址
 48      * @param params 参数集合
 49      * @param propertys 请求属性
 50      * @return 响应对象
 51      * @throws IOException
 52      */
 53     public HttpRespons sendGet(String urlString, Map<String, String> params, Map<String, String> propertys) throws IOException
 54     {
 55         return this.send(urlString, "GET", params, propertys);
 56     }
 57     /**
 58      * 发送POST请求
 59      * @param urlString URL地址
 60      * @return 响应对象
 61      * @throws IOException
 62      */
 63     public HttpRespons sendPost(String urlString) throws IOException
 64     {
 65         return this.send(urlString, "POST", null, null);
 66     }
 67     /**
 68      * 发送POST请求
 69      * @param urlString URL地址
 70      * @param params 参数集合
 71      * @return 响应对象
 72      * @throws IOException
 73      */
 74     public HttpRespons sendPost(String urlString, Map<String, String> params) throws IOException
 75     {
 76         return this.send(urlString, "POST", params, null);
 77     }
 78     /**
 79      * 发送POST请求
 80      * @param urlString URL地址
 81      * @param params 参数集合
 82      * @param propertys 请求属性
 83      * @return 响应对象
 84      * @throws IOException
 85      */
 86     public HttpRespons sendPost(String urlString, Map<String, String> params, Map<String, String> propertys) throws IOException
 87     {
 88         return this.send(urlString, "POST", params, propertys);
 89     }
 90     /**
 91      * 发送HTTP请求
 92      * @param urlString
 93      * @return 响应对象
 94      * @throws IOException
 95      */
 96     private HttpRespons send(String urlString, String method, Map<String, String> parameters, Map<String, String> propertys) throws IOException
 97     {
 98         HttpURLConnection urlConnection = null;
 99         if (method.equalsIgnoreCase("GET") && parameters != null)
100         {
101             StringBuffer param = new StringBuffer();
102             int i = 0;
103             for (String key : parameters.keySet())
104             {
105                 if (i == 0)
106                     param.append("?");
107                 else
108                     param.append("&");
109                 param.append(key).append("=").append(parameters.get(key));
110                 // param.append(parameters.get(key));
111                 i++;
112             }
113             urlString += param;
114         }
115         URL url = new URL(urlString);
116         urlConnection = (HttpURLConnection) url.openConnection();
117         urlConnection.setRequestMethod(method);
118         urlConnection.setDoOutput(true);
119         urlConnection.setDoInput(true);
120         urlConnection.setUseCaches(false);
121         if (propertys != null)
122         {
123             for (String key : propertys.keySet())
124             {
125                 urlConnection.addRequestProperty(key, propertys.get(key));
126             }
127         }
128         if (method.equalsIgnoreCase("POST") && parameters != null)
129         {
130             StringBuffer param = new StringBuffer();
131             for (String key : parameters.keySet())
132             {
133                 param.append("&");
134                 param.append(key).append("=").append(parameters.get(key));
135                 // param.append(parameters.get(key));
136             }
137             urlConnection.getOutputStream().write(param.toString().getBytes());
138             urlConnection.getOutputStream().flush();
139             urlConnection.getOutputStream().close();
140         }
141         return this.makeContent(urlString, urlConnection);
142     }
143     /**
144      * 得到响应对象
145      * @param urlConnection
146      * @return 响应对象
147      * @throws IOException
148      */
149     private HttpRespons makeContent(String urlString, HttpURLConnection urlConnection) throws IOException
150     {
151         HttpRespons httpResponser = new HttpRespons();
152         try
153         {
154             InputStream in = urlConnection.getInputStream();
155             BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
156             httpResponser.contentCollection = new Vector<String>();
157             StringBuffer temp = new StringBuffer();
158             String line = bufferedReader.readLine();
159             while (line != null)
160             {
161                 httpResponser.contentCollection.add(line);
162                 temp.append(line).append("\r\n");
163                 line = bufferedReader.readLine();
164             }
165             bufferedReader.close();
166             String ecod = urlConnection.getContentEncoding();
167             if (ecod == null)
168                 ecod = this.defaultContentEncoding;
169             httpResponser.urlString = urlString;
170             httpResponser.defaultPort = urlConnection.getURL().getDefaultPort();
171             httpResponser.file = urlConnection.getURL().getFile();
172             httpResponser.host = urlConnection.getURL().getHost();
173             httpResponser.path = urlConnection.getURL().getPath();
174             httpResponser.port = urlConnection.getURL().getPort();
175             httpResponser.protocol = urlConnection.getURL().getProtocol();
176             httpResponser.query = urlConnection.getURL().getQuery();
177             httpResponser.ref = urlConnection.getURL().getRef();
178             httpResponser.userInfo = urlConnection.getURL().getUserInfo();
179             httpResponser.content = new String(temp.toString().getBytes(), ecod);
180             httpResponser.contentEncoding = ecod;
181             httpResponser.code = urlConnection.getResponseCode();
182             httpResponser.message = urlConnection.getResponseMessage();
183             httpResponser.contentType = urlConnection.getContentType();
184             httpResponser.method = urlConnection.getRequestMethod();
185             httpResponser.connectTimeout = urlConnection.getConnectTimeout();
186             httpResponser.readTimeout = urlConnection.getReadTimeout();
187             return httpResponser;
188         }
189         catch (IOException e)
190         {
191             throw e;
192         }
193         finally
194         {
195             if (urlConnection != null)
196                 urlConnection.disconnect();
197         }
198     }
199     /**
200      * 默认的响应字符集
201      */
202     public String getDefaultContentEncoding()
203     {
204         return this.defaultContentEncoding;
205     }
206     /**
207      * 设置默认的响应字符集
208      */
209     public void setDefaultContentEncoding(String defaultContentEncoding)
210     {
211         this.defaultContentEncoding = defaultContentEncoding;
212     }
213 }
214  
  1 import java.util.Vector;
  2 /**
  3  * HTTP响应处理类
  4  */
  5 public class HttpRespons
  6 {
  7     String urlString;
  8     int defaultPort;
  9     String file;
 10     String host;
 11     String path;
 12     int port;
 13     String protocol;
 14     String query;
 15     String ref;
 16     String userInfo;
 17     String contentEncoding;
 18     String content;
 19     String contentType;
 20     public int code;
 21     String message;
 22     String method;
 23     int connectTimeout;
 24     int readTimeout;
 25     Vector<String> contentCollection;
 26     public static final int CODE_OK = 200;
 27     public String getContent()
 28     {
 29         return content;
 30     }
 31     public String getContentType()
 32     {
 33         return contentType;
 34     }
 35     public int getCode()
 36     {
 37         return code;
 38     }
 39     public String getMessage()
 40     {
 41         return message;
 42     }
 43     public Vector<String> getContentCollection()
 44     {
 45         return contentCollection;
 46     }
 47     public String getContentEncoding()
 48     {
 49         return contentEncoding;
 50     }
 51     public String getMethod()
 52     {
 53         return method;
 54     }
 55     public int getConnectTimeout()
 56     {
 57         return connectTimeout;
 58     }
 59     public int getReadTimeout()
 60     {
 61         return readTimeout;
 62     }
 63     public String getUrlString()
 64     {
 65         return urlString;
 66     }
 67     public int getDefaultPort()
 68     {
 69         return defaultPort;
 70     }
 71     public String getFile()
 72     {
 73         return file;
 74     }
 75     public String getHost()
 76     {
 77         return host;
 78     }
 79     public String getPath()
 80     {
 81         return path;
 82     }
 83     public int getPort()
 84     {
 85         return port;
 86     }
 87     public String getProtocol()
 88     {
 89         return protocol;
 90     }
 91     public String getQuery()
 92     {
 93         return query;
 94     }
 95     public String getRef()
 96     {
 97         return ref;
 98     }
 99     public String getUserInfo()
100     {
101         return userInfo;
102     }
103 }
104  
 1 //测试客户端
 2 import java.io.IOException;
 3 public class TestHttp
 4 {
 5     public static void main(String[] args) throws IOException
 6     {
 7         HttpRequester request = new HttpRequester();
 8         HttpRespons hr = request.sendGet("http://www.12306.cn/mormhweb/");
 9         String rsContent = hr.getContent();
10         System.out.println(rsContent);
11     }
12 }
原文地址:https://www.cnblogs.com/liwei45212/p/3026369.html