📄 getmessage.java
字号:
connection = openFileConnection(u); } this.connection = connection; return connection; } protected void closeConnection() { if (this.connection instanceof HttpURLConnection) { ((HttpURLConnection)this.connection).disconnect(); } this.connection = null; } protected void doGet() throws IOException {} protected Message getResponse(URL u) throws IOException { Message m = new Message(); m.setHeaders(getResponseHeaders()); String cl = m.getHeader(Constants.MIME.Key.CONTENT_LENGTH); int contentLength = -1; try { contentLength = Integer.valueOf(cl != null ? cl : "-1").intValue(); } catch (NumberFormatException nfe) {} String contentType = m.getHeader(Constants.MIME.Key.CONTENT_TYPE); m.setBody((contentLength == -1 && contentType != null) || contentLength > 0 ? getResponseBody() : ""); m.setReferer(u); return m; } protected URL getLocation(URL base, Message message) { URL u = null; final String LOCATION_PREFIX = ".location='"; final String LOCATION_SUFFIX = "'"; if (base != null && message != null) { String location = message.getHeader(Constants.MIME.Key.LOCATION); if (location != null) { try { u = new URL(base, location); } catch (MalformedURLException mue) { if (VERBOSE) { mue.printStackTrace(); } } } else if (message.getReferer() != null) { String s = message.getBody(); int i = s.indexOf(LOCATION_PREFIX); int j = s.indexOf(LOCATION_SUFFIX, i + LOCATION_PREFIX.length()); if (i > -1 && j > i) { try { u = new URL(message.getReferer(), s.substring(i + LOCATION_PREFIX.length(), j)); } catch (MalformedURLException mue) { if (VERBOSE) { mue.printStackTrace(); } } } } } return u; } // xxx: ugly protected Message resolveFrames(Message response, URL base) { final String prefix = "<frame "; final String prefixCap = prefix.toUpperCase(); final String postfix = ">"; final String target = "src"; final String targetCap = target.toUpperCase(); final String quote = "\""; Message msg = new Message(); int i = -1; int j = -1; int k = -1; int l = -1; int m = -1; String s = null; String r = null; URL u = null; Message reply = null; String key = null; List values = null; StringBuffer sb = new StringBuffer(); if (response != null && response.getBody() != null) { sb.append(response.getBody()); } for (Iterator keys = (response != null ? response.getHeaderKeys() : Collections.EMPTY_MAP.keySet().iterator()); keys.hasNext(); ) { key = (String)keys.next(); values = new ArrayList(); for (Iterator h = response.getHeaders(key); h.hasNext(); ) { values.add((String)h.next()); } msg.setHeader(key, (String)values.get(0)); } while ((i = sb.indexOf(prefix)) > -1 || (j = sb.indexOf(prefixCap)) > -1) { k = (i > j ? i : j); i = j = -1; i = sb.indexOf(target, k); j = sb.indexOf(targetCap, k); l = (i > j ? i : j); m = sb.indexOf(postfix, k); if (k > -1 && l > k && m > l) { s = sb.substring(l, m); i = s.indexOf(quote); j = s.indexOf(quote, i + 1); r = s.substring(i + 1, j).trim(); try { u = new URL(base, r); } catch (MalformedURLException mue) { if (VERBOSE) { mue.printStackTrace(); } } try { reply = new GetMessage(u).dispatch(); } catch (IOException ioe) { if (VERBOSE) { ioe.printStackTrace(); } } sb.replace(k, m + 1, reply.getBody()); } else { break; } i = j = k = l = m = -1; } msg.setBody(sb.toString()); return msg; } private HttpURLConnection openHTTPConnection(URL u) throws IOException { HttpURLConnection connection = (HttpURLConnection)u.openConnection(); connection.setRequestMethod(this.method); String key = null; String value = null; if (this.message != null) { for (Iterator keys = this.message.getHeaderKeys(); keys.hasNext(); ) { key = (String)keys.next(); value = (String)this.message.getHeader(key); connection.setRequestProperty(key, value); } } boolean doOutput = (this.method == Constants.HTTP.POST && this.message != null && this.message.hasBody()); Map defaultHeaders = ((! doOutput) ? Util.getDefaultGetHeaders() : Util.getDefaultPostHeaders()); for (Iterator keys = defaultHeaders.keySet().iterator(); keys.hasNext(); ) { key = (String)keys.next(); if (connection.getRequestProperty(key) == null) { value = (String)defaultHeaders.get(key); connection.addRequestProperty(key, value); } } if (doOutput) { int l = this.message.getBody().getBytes().length; connection.setRequestProperty(Constants.MIME.Key.CONTENT_LENGTH, Integer.toString(l)); } boolean followRedirects = (this.method == Constants.HTTP.GET); connection.setDoOutput(doOutput); connection.setDoInput(true); connection.setUseCaches(false); connection.setFollowRedirects(followRedirects); connection.setInstanceFollowRedirects(followRedirects); return connection; } private URLConnection openFileConnection(URL u) throws IOException { return u.openConnection(); } private Map getResponseHeaders() { Map headers = new HashMap(); Map m = this.connection.getHeaderFields(); headers.putAll(m); headers.remove(null); return headers; } private String getResponseBody() throws IOException { final int BLOCK = 4 * 1024; StringBuffer response = new StringBuffer(); InputStream is = this.connection.getInputStream(); String contentType = this.connection.getContentType(); InputStreamReader isr = ((this.isUnicodeEncoding) ? new InputStreamReader(is, Util.getCharSet(contentType)): new InputStreamReader(is)); BufferedReader reader = new BufferedReader(isr); char[] buf = new char[BLOCK]; int l = 0; CharArrayWriter writer = new CharArrayWriter(); while ((l = reader.read(buf, 0, BLOCK)) > -1) { writer.write(buf, 0, l); } reader.close(); return(this.isUnicodeEncoding ? Util.toUnicodeEncoded(writer.toString()) : writer.toString()); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -