?? httpmessage.java
字號(hào):
/*This file is part of Socks via HTTP.This package is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2 of the License, or(at your option) any later version.Socks via HTTP is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with Socks via HTTP; if not, write to the Free SoftwareFoundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*/// Title : HttpMessage.java// Version : 0.40// Copyright : Copyright (c) 2001// Author : Florent CUETO (fcueto@wanadoo.fr)// Description : Http Message Datapackage socks4;import java.io.*;import java.net.*;import java.util.*;/** * A class to simplify HTTP applet-server communication. It abstracts * the communication into messages, which can be either GET or POST. * <p> * It can be used like this: * <blockquote><pre> * URL url = new URL(getCodeBase(), "/servlet/ServletName"); * * HttpMessage msg = new HttpMessage(url); * * // Parameters may optionally be set using java.util.Properties * Properties props = new Properties(); * props.put("name", "value"); * * // Headers, cookies, and authorization may be set as well * msg.setHeader("Accept", "image/png"); // optional * msg.setCookie("JSESSIONID", "9585155923883872"); // optional * msg.setAuthorization("guest", "try2gueSS"); // optional * * InputStream in = msg.sendGetMessage(props); * </pre></blockquote> * <p> */public class HttpMessage { URL servlet = null; Hashtable headers = null; /** * Constructs a new HttpMessage that can be used to communicate with the * servlet at the specified URL. * * @param servlet the server resource (typically a servlet) with which * to communicate */ public HttpMessage(URL servlet) { this.servlet = servlet; } /** * Performs a GET request to the servlet, with no query string. * * @return an InputStream to read the response * @exception IOException if an I/O error occurs */ public InputStream sendGetMessage() throws IOException { return sendGetMessage(null); } /** * Performs a GET request to the servlet, building * a query string from the supplied properties list. * * @param args the properties list from which to build a query string * @return an InputStream to read the response * @exception IOException if an I/O error occurs */ public InputStream sendGetMessage(Properties args) throws IOException { String argString = ""; // default if (args != null) { argString = "?" + toEncodedString(args); } URL url = new URL(servlet.toExternalForm() + argString); // Turn off caching URLConnection con = url.openConnection(); con.setUseCaches(false); // Send headers sendHeaders(con); return con.getInputStream(); } /** * Performs a POST request to the servlet, with no query string. * * @return an InputStream to read the response * @exception IOException if an I/O error occurs */ public InputStream sendPostMessage() throws IOException { return sendPostMessage(null); } /** * Performs a POST request to the servlet, building * post data from the supplied properties list. * * @param args the properties list from which to build the post data * @return an InputStream to read the response * @exception IOException if an I/O error occurs */ public InputStream sendPostMessage(Properties args) throws IOException { String argString = ""; // default if (args != null) { argString = toEncodedString(args); // notice no "?" } URLConnection con = servlet.openConnection(); // Prepare for both input and output con.setDoInput(true); con.setDoOutput(true); // Turn off caching con.setUseCaches(false); // Work around a Netscape bug con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // Send headers sendHeaders(con); // Write the arguments as post data DataOutputStream out = new DataOutputStream(con.getOutputStream()); out.writeBytes(argString); out.flush(); out.close(); return con.getInputStream(); } /** * Performs a POST request to the servlet, uploading a serialized object. * <p> * The servlet can receive the object in its <tt>doPost()</tt> method * like this: * <pre> * ObjectInputStream objin = * new ObjectInputStream(req.getInputStream()); * Object obj = objin.readObject(); * </pre> * The type of the uploaded object can be determined through introspection. * * @param obj the serializable object to upload * @return an InputStream to read the response * @exception IOException if an I/O error occurs */ public InputStream sendPostMessage(Serializable obj) throws IOException { URLConnection con = servlet.openConnection(); // Prepare for both input and output con.setDoInput(true); con.setDoOutput(true); // Turn off caching con.setUseCaches(false); // Set the content type to be application/x-java-serialized-object con.setRequestProperty("Content-Type", "application/x-java-serialized-object"); // Send headers sendHeaders(con); // Write the serialized object as post data ObjectOutputStream out = new ObjectOutputStream(con.getOutputStream()); out.writeObject(obj); out.flush(); out.close(); return con.getInputStream(); } public InputStream sendGZippedPostMessage(Serializable obj) throws IOException { URLConnection con = servlet.openConnection(); // Prepare for both input and output con.setDoInput(true); con.setDoOutput(true); // Turn off caching con.setUseCaches(false); // Set the content type to be application/x-java-serialized-object con.setRequestProperty("Content-Type", "application/x-java-serialized-object"); // Send headers sendHeaders(con); // Write the serialized object as post data java.util.zip.GZIPOutputStream zos = new java.util.zip.GZIPOutputStream(con.getOutputStream()); ObjectOutputStream out = new ObjectOutputStream(zos); out.writeObject(obj); out.flush(); out.close(); return con.getInputStream(); } /** * Sets a request header with the given name and value. The header * persists across multiple requests. The caller is responsible for * ensuring there are no illegal characters in the name and value. * * @param name the header name * @param value the header value */ public void setHeader(String name, String value) { if (headers == null) { headers = new Hashtable(); } headers.put(name, value); } // Send the contents of the headers hashtable to the server private void sendHeaders(URLConnection con) { if (headers != null) { Enumeration enum = headers.keys(); while (enum.hasMoreElements()) { String name = (String) enum.nextElement(); String value = (String) headers.get(name); con.setRequestProperty(name, value); } } } /** * Sets a request cookie with the given name and value. The cookie * persists across multiple requests. The caller is responsible for * ensuring there are no illegal characters in the name and value. * * @param name the header name * @param value the header value */ public void setCookie(String name, String value) { if (headers == null) { headers = new Hashtable(); } String existingCookies = (String) headers.get("Cookie"); if (existingCookies == null) { setHeader("Cookie", name + "=" + value); } else { setHeader("Cookie", existingCookies + "; " + name + "=" + value); } } /** * Sets the authorization information for the request (using BASIC * authentication via the HTTP Authorization header). The authorization * persists across multiple requests. * * @param name the user name * @param name the user password */ public void setAuthorization(String name, String password) { String authorization = Base64Encoder.encode(name + ":" + password); setHeader("Authorization", "Basic " + authorization); } public void setProxyAuthorization(String name, String password) { String authorization = Base64Encoder.encode(name + ":" + password); setHeader("Proxy-Authorization", "Basic " + authorization); } /* * Converts a properties list to a URL-encoded query string */ private String toEncodedString(Properties args) { StringBuffer buf = new StringBuffer(); Enumeration names = args.propertyNames(); while (names.hasMoreElements()) { String name = (String) names.nextElement(); String value = args.getProperty(name); buf.append(URLEncoder.encode(name) + "=" + URLEncoder.encode(value)); if (names.hasMoreElements()) buf.append("&"); } return buf.toString(); }}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -