⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 httpmessage.java

📁 java高级使用教程 全书一共分六章
💻 JAVA
字号:
package chat;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. */public class HttpMessage {  URL servlet = null;  Hashtable headers = null;  public HttpMessage(URL servlet) {    this.servlet = servlet;  }  public InputStream sendGetMessage() throws IOException {    return sendGetMessage(null);  }  /**   * Performs a GET request to the servlet, building   * a query string from the supplied properties list.   */  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);    sendHeaders(con);    return con.getInputStream();  }  /** Performs a POST request to the servlet, with no query string.   */  public InputStream sendPostMessage() throws IOException {    return sendPostMessage(null);  }  /** Performs a POST request to the servlet, building   * post data from the supplied properties list.   */  public InputStream sendPostMessage(Properties args) throws IOException {    String argString = "";  // default    if (args != null) {      argString = toEncodedString(args);  // notice no "?"    }    URLConnection con = servlet.openConnection();System.out.println("HttpMessage");    // 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");    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.   */  public InputStream sendPostMessage(Serializable obj) throws IOException {    URLConnection con = servlet.openConnection();    con.setDoInput(true);    con.setDoOutput(true);    con.setUseCaches(false);    con.setRequestProperty(        "Content-Type", "application/x-java-serialized-object");            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();  }  /* 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.   */  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.   */  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.     public void setAuthorization(String name, String password) {    String authorization = Base64Encoder.encode(name + ":" + password);    setHeader("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();  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -