コード例 #1
0
ファイル: HTTPUtils.java プロジェクト: harishgupta111/SOA
  /**
   * Close all IO connections that were opened during post()
   *
   * @param cs Client socket items to populate.
   */
  private static void cleanUp(ClientSocket cs) throws IOException {

    Debug.log(Debug.MSG_LIFECYCLE, "HTTPUtils: closing IO socket connections");

    if (cs.in != null) {
      try {
        cs.in.close();

        cs.in = null;
      } catch (Exception e) {
        Debug.log(Debug.ALL_WARNINGS, e.toString());
      }
    }

    if (cs.out != null) {
      try {
        cs.out.close();

        cs.out = null;
      } catch (Exception e) {
        Debug.log(Debug.ALL_WARNINGS, e.toString());
      }
    }

    if (cs.s != null) {
      try {
        cs.s.close();

        cs.s = null;
      } catch (Exception e) {
        Debug.log(Debug.ALL_WARNINGS, e.toString());
      }
    }
  }
コード例 #2
0
ファイル: HTTPUtils.java プロジェクト: harishgupta111/SOA
  /**
   * POST something to the given URL. The headers are put in as HTTP headers, the content length is
   * calculated and the content is sent as content. Duh.
   *
   * @param cs Client socket items.
   * @param url the url to post to
   * @param headers additional headers to send as HTTP headers
   * @param contentType type of the content
   * @param content the body of the post
   * @exception IOException if post fails
   */
  private static Response post(
      ClientSocket cs, URL url, Hashtable headers, String contentType, String content)
      throws IOException {

    // If the port is not specified in the URL, use the default http port.
    int port = url.getPort();
    if (port == -1) port = 80;

    cs.s = new Socket(url.getHost(), +port);
    cs.out = new PrintWriter(cs.s.getOutputStream());
    cs.in = new BufferedReader(new InputStreamReader(cs.s.getInputStream()));

    // Create the HTTP header
    cs.out.print(HTTP_POST + " " + url.getFile() + " HTTP/" + HTTP_VERSION + "\r\n");
    cs.out.print(HEADER_HOST + ": " + url.getHost() + ':' + port + "\r\n");
    cs.out.print(HEADER_CONTENT_TYPE + ": " + contentType + "\r\n");
    cs.out.print(HEADER_CONTENT_LENGTH + ": " + content.length() + "\r\n");

    for (Enumeration e = headers.keys(); e.hasMoreElements(); ) {
      Object key = e.nextElement();
      cs.out.print(key + ": " + headers.get(key) + "\r\n");
    }

    // According to HTTP1.1 need another CRLF after the header
    cs.out.print("\r\n");

    Debug.log(Debug.MSG_LIFECYCLE, "HTTPUtils: Writing content to the socket... " + content);

    cs.out.println(content);

    cs.out.flush();

    /* read the status line */
    int statusCode = 0;
    String statusString = null;
    StringTokenizer st = new StringTokenizer(cs.in.readLine());
    st.nextToken(); // ignore version part
    statusCode = Integer.parseInt(st.nextToken());

    StringBuffer sb = new StringBuffer();

    while (st.hasMoreTokens()) {
      sb.append(st.nextToken());
      if (st.hasMoreTokens()) sb.append(" ");
    }

    statusString = sb.toString();

    /* get the headers */
    Hashtable respHeaders = new Hashtable();
    int respContentLength = -1;
    String respContentType = null;
    String line = null;

    while ((line = cs.in.readLine()) != null) {

      if (line.length() == 0) break;

      int colonIndex = line.indexOf(':');
      String fieldName = line.substring(0, colonIndex);
      String fieldValue = line.substring(colonIndex + 1).trim();

      if (fieldName.equals(HEADER_CONTENT_LENGTH)) respContentLength = Integer.parseInt(fieldValue);
      else if (fieldName.equals(HEADER_CONTENT_TYPE)) respContentType = fieldValue;
      else respHeaders.put(fieldName, fieldValue);
    }

    Response response =
        new Response(
            statusCode, statusString, respHeaders, respContentLength, respContentType, cs.in);

    return response;
  }