最近本人在業余時間想使用socket寫一個訪問網頁的工具類,用來了解http。
在編寫過程中發現,一個大概是5k的圖片,用代碼去下載時候用了足足30秒,但是在瀏覽器中訪問同一個鏈接的時候只用了3ms,這個中間的差距我實在不能夠接收。我是做了什么,我編寫的代碼這么慢。
經過我在谷歌上搜索發現read()方法當讀取完數據之后就開始阻塞,等待返回-1結束,這個等待的過程占到了總時間的99%。可是如果不等待怎么知道結束了?數據不全的話,會造成解析出錯。
通過查看返回的http頭發現,在http返回頭中Content-length
對應的數字就是消息體的長度,只要通過判斷獲取的長度和頭中的長度是一致的,就可以認為數據已經獲取完畢了,沒有必要等待返回-1來判斷是否結束。
通過以上的修改,下載圖片的速度終于上來了,達到了一秒以內。總算不那么慢了,之前那真是要瘋了。
附上代碼,僅供參考
package com.shxzhlxrr.util.socket;import static org.junit.Assert.assertNotNull;import static org.junit.Assert.assertTrue;import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.PRintWriter;import java.net.InetAddress;import java.net.Socket;import java.net.UnknownHostException;import java.security.KeyManagementException;import java.security.KeyStore;import java.security.NoSuchAlgorithmException;import java.security.cert.CertificateException;import java.security.cert.X509Certificate;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.regex.Pattern;import java.util.zip.GZIPInputStream;import javax.net.ssl.SSLContext;import javax.net.ssl.SSLSocketFactory;import javax.net.ssl.TrustManager;import javax.net.ssl.TrustManagerFactory;import javax.net.ssl.X509TrustManager;import org.apache.log4j.Logger;import com.shxzhlxrr.util.StringUtil;/** * * @author zxr * @date 2017年2月6日 下午6:33:36 */public class HttpClient { /** * 主機 */ private String host; private int port = 80; private String url;// 主機后面的相對的路徑 @SuppressWarnings("unused") private String baseUrl;// 訪問的完整的路徑 private String context; private String protocol; /** * 客戶端的信息 */ private String userAgent; private InputStream contentInputStream; private byte[] contentData; private static Logger log = Logger.getLogger(HttpClient.class); private int status = 200; private Map<String, Object> header = new HashMap<String, Object>(); public Map<String, Object> getHeader() { return header; } public HttpClient() { } /** * 推薦使用四個參數的構造方法,本構造方法對于url的解析存在一些問題<br/> * 可能會給你帶來一些不必要的問題 * * @param url */ public HttpClient(String url) { this.baseUrl = url; this.parseHttpUrl(url); } public HttpClient(String host, int port, String url, String protocol) { this.host = host; this.port = port; this.url = url; this.protocol = protocol; this.baseUrl = this.protocol + "://" + this.host + ":" + this.port + "/" + this.url; } /** * 獲取返回的內容 * * @return * @throws Exception */ public String getContext() { return context; } /** * 發送請求 */ public void send() throws Exception { if (this.protocol == null) { this.protocol = "http"; } if (this.url == null) { this.url = "/"; } validateParam(); getHttpContent(); } public String getHost() { return host; } public void setHost(String host) { this.host = host; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getProtocol() { return protocol; } public void setProtocol(String protocol) { this.protocol = protocol; } public InputStream getContentInputStream() { return contentInputStream; } public byte[] getContentData() { return contentData; } public int getStatus() { return status; } public void setStatus(int status) { this.status = status; } public String getUserAgent() { return userAgent; } public void setUserAgent(String userAgent) { this.userAgent = userAgent; } private void getHttpContent() throws Exception { BufferedReader bf = null; PrintWriter pw = null; Socket soc = null; GZIPInputStream gzin = null; try { soc = getSocket(); if (soc.isConnected()) { pw = new PrintWriter(soc.getOutputStream(), true);// 獲取輸出流 sendReqMsg(pw);// 發送請求 parseRes(soc);// 解析返回的請求數據 } } catch (Exception e) { e.printStackTrace(); throw e; } finally { if (bf != null) { try { bf.close(); } catch (IOException e) { e.printStackTrace(); } } if (pw != null) { pw.close(); } if (soc != null) { try { soc.close(); } catch (IOException e) { e.printStackTrace(); } } if (gzin != null) { gzin.close(); } } } /** * 獲取訪問的socket * * @return * @throws IOException * @throws NoSuchAlgorithmException * @throws UnknownHostException * @throws KeyManagementException */ private Socket getSocket() throws KeyManagementException, UnknownHostException, NoSuchAlgorithmException, IOException { Socket soc = null; if ("https".equals(this.protocol)) { soc = getHttpsSocket(); } else { soc = new Socket(InetAddress.getByName(this.host), this.port); } return soc; } /** * 獲取httpssocket * * @return * @throws UnknownHostException * @throws IOException * @throws NoSuchAlgorithmException * @throws KeyManagementException */ private Socket getHttpsSocket() throws UnknownHostException, IOException, NoSuchAlgorithmException, KeyManagementException { X509TrustManager xtm; try { xtm = new Java2000TrustManager(); TrustManager mytm[] = { xtm }; // 得到上下文 SSLContext ctx = SSLContext.getInstance("SSL", "SunJSSE"); // SSLContext ctx = SSLContext.getInstance("TLS"); // 初始化 ctx.init(null, mytm, new java.security.SecureRandom()); // ctx.init(null, null, null); // 獲得工廠 SSLSocketFactory factory = ctx.getSocketFactory(); // 從工廠獲得Socket連接 Socket socket = factory.createSocket(host, port); return socket; } catch (Exception e) { e.printStackTrace(); } return null; } /** * 解析返回的數據 */ private void parseRes(Socket soc) throws IOException { long start = System.currentTimeMillis(); InputStream in = soc.getInputStream(); byte[] buf = new byte[1024 * 200]; int isClose = 0; ByteArrayOutputStream baos = new ByteArrayOutputStream(); List<String> titles = new ArrayList<String>(); while ((isClose = in.read()) != -1) { baos.write(isClose); if ('/n' == isClose) { String str = new String(baos.toByteArray()); titles.add(str); baos.reset(); if (StringUtil.isNull(str)) {// 這個時候表示到了空行。 parseHeader(titles); break; } } } long end = System.currentTimeMillis(); int count = 0; log.debug("[解析圖片的頭]:" + ((end - start) / 1000)); log.debug("[返回的消息的頭]:" + this.header); if ("chunked".equals(this.header.get("Transfer-Encoding"))) { baos = parseChunk(in); } else { int content_length = Integer.valueOf(String.valueOf(this.header.get("Content-Length"))); int countNum = 0; while ((count = in.read(buf)) != -1) { baos.write(buf, 0, count); countNum = countNum + count; end = System.currentTimeMillis(); log.debug("[解析內容]:" + ((end - start) / 1000)); if (content_length == countNum) { break; } } } end = System.currentTimeMillis(); log.debug("[解析消息的主體]:" + ((end - start) / 1000)); byte[] contentBuf = baos.toByteArray(); if ("gzip".equals(this.header.get("Content-Encoding"))) {// 當是gzip壓縮的時候,進行解壓 contentBuf = ungzip(contentBuf); } this.context = new String(contentBuf); this.contentData = contentBuf; this.contentInputStream = new ByteArrayInputStream(this.contentData); // } end = System.currentTimeMillis(); log.debug("[解析完畢]:" + ((end - start) / 1000)); } /** * 解析chunk編碼的數據 * * @param in * @return */ @SuppressWarnings("resource") private ByteArrayOutputStream parseChunk(InputStream in) { ByteArrayOutputStream out = new ByteArrayOutputStream(); try { out = parseChunk(out, in); } catch (Exception e) { e.printStackTrace(); } return out; } /** * 解析chunk編碼的數據 * * @param out * @param in * @return * @throws IOException */ public ByteArrayOutputStream parseChunk(ByteArrayOutputStream out, InputStream in) throws IOException { ByteArrayOutputStream temp = new ByteArrayOutputStream(); int ch = 0; while ((ch = in.read()) != -1) { if ('/r' == ch) { continue; } if ('/n' == ch) { break; } temp.write(ch); } String tempStr = new String(temp.toByteArray()); if (!"".equals(tempStr.trim())) { int count = Integer.parseInt(new String(temp.toByteArray()).trim(), 16); if (count == 0) { return out; } int num = 0; while (num < count) { int chr = in.read(); out.write(chr); num++; } } out = parseChunk(out, in); return out; } /** * 校驗基本的數據不能為空 */ private void validateParam() { assertNotNull("端口號不能為空", this.port); assertNotNull("主機不能為空", this.host); assertNotNull("訪問的鏈接不能為空", this.url); assertNotNull("訪問的協議不能為空", this.protocol); assertTrue("請設置協議為'http'或'https'", Pattern.compile("http[s]*").matcher(this.protocol).find()); } /** * 發送請求信息 */ private void sendReqMsg(PrintWriter pw) { pw.println("GET " + this.url + " HTTP/1.1"); pw.println("Accept:text/html,image/webp,*/*;q=0.8"); pw.println("Accept-Encoding:gzip, deflate, sdch, br"); pw.println("Accept-Language:zh-CN,zh;q=0.8"); pw.println("Connection:keep-alive"); if ("http".equals(this.protocol)) { pw.println("Host:" + this.host + ":" + this.port); } else { pw.println("Host:" + this.host); } if (this.userAgent != null) { pw.println(this.userAgent); } else { this.userAgent = "User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36"; pw.println(this.userAgent); } pw.println("/r/n"); } /** * 用來解析返回的數據的第一行 * * @param header */ private void parseHeader(List<String> titles) { String header = titles.get(0); String[] haaders = header.split(" "); this.header.put("protocol", haaders[0].trim()); this.header.put("status", haaders[1].trim()); this.status = Integer.valueOf(haaders[1].trim()); titles.remove(0); for (String title : titles) { if (StringUtil.isNull(title)) { continue; } String key = title.substring(0, title.indexOf(":")); String value = title.substring(title.indexOf(":") + 1); this.header.put(key.trim(), value.trim()); } log.debug(this.header); } /** * 從一個url鏈接中解析出 host,端口,url,protocol 等 * * @param url */ private void parseHttpUrl(String url) { // TODO 校驗url是否是正確的 String protocol = "http"; if (url.startsWith("https")) { protocol = "https"; } url = url.substring(url.indexOf("http://") + 2); String host = url; if (url.indexOf("/") < 0) { url = "/"; } else { host = url.substring(0, url.indexOf("/")); url = url.substring(url.indexOf("/")); } int port = 80; if ("https".equals(protocol)) { port = 443; } if (host.contains(":")) { port = Integer.valueOf(host.substring(host.indexOf(":") + 1)); host = host.substring(0, host.indexOf(":")); } this.host = host; this.port = port; this.url = url; this.protocol = protocol; } /** * 將gzip壓縮的字節數據進行解壓 * * @param gzipbuf * @return * @throws IOException */ public byte[] ungzip(byte[] gzipbuf) throws IOException { GZIPInputStream gzin = new GZIPInputStream(new ByteArrayInputStream(gzipbuf)); BufferedInputStream bufis = new BufferedInputStream(gzin); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; int count = 0; while ((count = bufis.read(buf)) != -1) { baos.write(buf, 0, count); } return baos.toByteArray(); }}class Java200TrustManager implements X509TrustManager { X509TrustManager sunJSSEX509TrustManager; Java200TrustManager() throws Exception { // 這里可以進行證書的初始化操作,使用的是jdk自帶的證書 String cerPath = "cer/cacerts"; InputStream in = getClass().getClassLoader().getResourceAsStream(cerPath); KeyStore ks = KeyStore.getInstance("JKS"); ks.load(in, "changeit".toCharArray()); TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509", "SunJSSE"); tmf.init(ks); TrustManager tms[] = tmf.getTrustManagers(); for (int i = 0; i < tms.length; i++) { if (tms[i] instanceof X509TrustManager) { sunJSSEX509TrustManager = (X509TrustManager) tms[i]; return; } } throw new Exception("Couldn't initialize"); } // 檢查客戶端的可信任狀態 public void checkClientTrusted(X509Certificate chain[], String authType) throws CertificateException { // System.out.println("檢查客戶端的可信任狀態..."); // System.out.println("checkClientTrusted-------------------"); // System.out.println("chain:" + chain); // System.out.println("authType:" + authType); sunJSSEX509TrustManager.checkClientTrusted(chain, authType); } // 檢查服務器的可信任狀態 public void checkServerTrusted(X509Certificate chain[], String authType) throws CertificateException { // System.out.println("檢查服務器的可信任狀態"); sunJSSEX509TrustManager.checkServerTrusted(chain, authType); // System.out.println("checkServerTrusted=========="); // System.out.println("chain:" + chain); // System.out.println("authType:" + authType); } // 返回接受的發行商數組 public X509Certificate[] getAcceptedIssuers() { // System.out.println("獲取接受的發行商數組..."); return sunJSSEX509TrustManager.getAcceptedIssuers(); // return null; }}新聞熱點
疑難解答