Java tutorial
/* * Copyright yz 2016-01-14 Email:admin@javaweb.org. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.javaweb.utils; import org.apache.commons.collections.map.CaseInsensitiveMap; import org.javaweb.net.HttpRequest; import org.javaweb.net.HttpResponse; import org.javaweb.net.HttpURLRequest; import org.javaweb.net.MultipartRequest; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.net.*; import java.nio.charset.Charset; import java.nio.charset.IllegalCharsetNameException; import java.util.LinkedHashMap; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; public class HttpRequestUtils { private static final Pattern HTML_CHARSET_PATTERN = Pattern .compile("(?i)<meta.*\\bcharset\\s*=\\s*(?:\"|')?([^\\s,;\"']*)"); /** * ?HTTP,?URI? * * @param url * @return */ public static String getFileType(URL url) { String path = "".equals(url.getPath()) ? "/" : url.getPath(); String file = path.substring(path.lastIndexOf("/")); return file.substring(file.lastIndexOf(".") + 1); } /** * ??Map?HTTP QueryString * * @param args * @param encoding * @return */ public static String toURLParameterString(Map<String, String> args, String encoding) { StringBuilder sb = new StringBuilder(); if (args != null) { int i = 0; for (String a : args.keySet()) { try { if (i > 0) { sb.append("&"); } i++; sb.append(a).append("=").append(URLEncoder.encode(args.get(a), encoding)); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } } return sb.toString(); } /** * HTML meta ????(charset) * * @param html * @return */ public static String getCharsetFromHTMLBody(String html) { String encoding = null; if (html != null) { Matcher m = HTML_CHARSET_PATTERN.matcher(html); if (m.find()) { String charset = m.group(1).trim(); charset = charset.replace("charset=", ""); if (charset.length() == 0) { return null; } try { if (Charset.isSupported(charset)) { encoding = charset.toUpperCase(Locale.ENGLISH); } } catch (IllegalCharsetNameException e) { return null; } } } return encoding; } /** * ?HTML? ??HTML????meta?charset? * ??????org.jsoup.helper.DataUtil??? * * @param bodyByte * @return */ public static String parseHTMLCharset(byte[] bodyByte) { String encoding = HttpRequestUtils.getCharsetFromHTMLBody(new String(bodyByte)); if (encoding == null) { int code = new BytesEncodingDetect().detectEncoding(bodyByte); encoding = BytesEncodingDetect.htmlname[code]; } return encoding != null ? encoding : "UTF-8"; } /** * HTTP HTTP????User-Agent?Referer * * @param httpURLConnection * @param request * @throws IOException */ public static void setRequestProperties(HttpURLConnection httpURLConnection, HttpRequest request) throws IOException { httpURLConnection.setConnectTimeout(request.getTimeout()); httpURLConnection.setReadTimeout(request.getTimeout()); HttpURLConnection.setFollowRedirects(request.isFollowRedirects()); if (StringUtils.isNotEmpty(request.getMethod()) && !(request instanceof MultipartRequest)) { // ?GETDoInput?DoOutput if (!"GET".equalsIgnoreCase(request.getMethod())) { httpURLConnection.setDoInput(true); httpURLConnection.setDoOutput(true); } httpURLConnection.setRequestMethod(request.getMethod().toUpperCase()); } // Cookie if (StringUtils.isNotEmpty(request.getCookie())) { httpURLConnection.setRequestProperty("Cookie", request.getCookie()); } // User-Agent if (StringUtils.isNotEmpty(request.getUserAgent())) { httpURLConnection.setRequestProperty("User-Agent", request.getUserAgent()); } // Referer if (StringUtils.isNotEmpty(request.getReferer())) { httpURLConnection.setRequestProperty("Referer", request.getReferer()); } } /** * Http? * * @param httpURLConnection * @param request * @param data * @throws IOException */ public static void setRequestData(HttpURLConnection httpURLConnection, HttpURLRequest request, String data) throws IOException { // HTTP Map<String, String> headers = request.getRequestHeader(); for (String key : headers.keySet()) { httpURLConnection.setRequestProperty(key, headers.get(key)); } // HTTP?GET? if (StringUtils.isNotEmpty(request.getMethod()) && !"GET".equalsIgnoreCase(request.getMethod())) { if (data != null || request.getRequestBytes() != null || request.getRequestInputStream() != null) { OutputStream out = httpURLConnection.getOutputStream(); // ?,:byte[]->InputStream->String if (request.getRequestBytes() != null) { out.write(request.getRequestBytes()); } else if (request.getRequestInputStream() != null) { InputStream in = request.getRequestInputStream(); byte[] bytes = new byte[2048]; int a = 0; while ((a = in.read(bytes)) != -1) { out.write(bytes, 0, a); } } else { out.write(data.getBytes(request.getCharset())); } out.flush(); out.close(); } } } /** * HTTP????HttpResponse * * @param httpURLConnection * @param response * @throws IOException */ public static void setResponse(HttpURLConnection httpURLConnection, HttpResponse response) throws IOException { response.setStatusCode(httpURLConnection.getResponseCode()); response.setStatusMessage(httpURLConnection.getResponseMessage()); response.setContentType(httpURLConnection.getContentType()); response.setHeader(new CaseInsensitiveMap(httpURLConnection.getHeaderFields())); response.setLastModified(httpURLConnection.getLastModified()); // ?Cookie setCookies(response); } /** * Cookie * * @param response */ private static void setCookies(HttpResponse response) { String cookieString = response.getRequest().getCookie(); if (StringUtils.isNotEmpty(cookieString)) { // Cookie?Cookie?Cookie Map String[] cookies = cookieString.split(";\\s?"); Map<String, String> cookieMap = new LinkedHashMap<String, String>(); for (String cookie : cookies) { String[] temp = cookie.split("="); if (temp.length == 2) { String key = temp[0]; String val = temp[1]; cookieMap.put(key, val); } else if (temp.length == 1) { cookieMap.put(temp[0], ""); } } // HttpCookie response.setCookies(cookieMap); } if (response.getHeader() == null) { return; } // ??Cookie, for (String name : response.getHeader().keySet()) { if (StringUtils.isEmpty(name) || !"Set-Cookie".equalsIgnoreCase(name)) { continue; } List<String> values = response.getHeader().get(name); for (String value : values) { if (value == null) { continue; } String[] strs = value.split(";"); if (strs.length > 0) { String[] temp = strs[0].split("="); if (temp.length == 2) { String key = temp[0]; String val = temp[1]; if (val.equalsIgnoreCase("deleted")) { response.removeCookie(key);// Cookie } else { response.cookie(key, val);// Cookie } } else if (temp.length == 1) { response.cookie(temp[0], "");// Cookie } } } } } /** * ??HTTP,??HTTP??DNS,? * * @param request * @return */ public static HttpResponse httpRequest(HttpURLRequest request) { HttpURLConnection httpURLConnection = null; InputStream in = null; HttpResponse response = new HttpResponse(request.getUrl()); try { response.setRequestTime(System.currentTimeMillis());// try { response.dnsParse();// DNS? String protocol = request.getUrl().getProtocol();// ??? if (!protocol.equals("http") && !protocol.equals("https")) { throw new MalformedURLException("?? http & https ??."); } else if ("https".equalsIgnoreCase(protocol)) { SslUtils.ignoreSsl(); } String data = null; if (request.getRequestDataMap() != null && request.getRequestDataMap().size() > 0) { data = HttpRequestUtils.toURLParameterString(request.getRequestDataMap(), request.getCharset()); } else { data = request.getRequestData(); } URL url = request.getUrl(); if ("GET".equalsIgnoreCase(request.getMethod()) && StringUtils.isNotEmpty(data)) { url = new URL(request.getUrl() + (StringUtils.isNotEmpty(request.getUrl().getQuery()) ? "&" : "?") + data); } httpURLConnection = (HttpURLConnection) url.openConnection(); setRequestProperties(httpURLConnection, request);// ? setRequestData(httpURLConnection, request, data);// ? httpURLConnection.connect(); response.setRequest(request); setResponse(httpURLConnection, response);// HTTP?? // ?HTTP? try { in = httpURLConnection.getInputStream(); } catch (IOException e) { in = httpURLConnection.getErrorStream(); } if (in != null) { response.setBodyBytes(IOUtils.toByteArray(in)); } } catch (UnknownHostException e) { response.setException(e); } } catch (IOException e) { response.setException(e); } finally { IOUtils.closeQuietly(in); if (httpURLConnection != null) { httpURLConnection.disconnect(); } response.setResponseTime(System.currentTimeMillis());// ? } return response; } }