예제 #1
0
 @Override
 public String getHeaderFieldKey(int pos) {
   try {
     getInputStream();
   } catch (IOException e) {
     // ignore
   }
   if (null == resHeader) {
     return null;
   }
   return resHeader.getKey(pos);
 }
예제 #2
0
  private byte[] createRequest() throws IOException {
    StringBuilder output = new StringBuilder(256);
    output.append(method);
    output.append(' ');
    output.append(requestString());
    output.append(' ');
    output.append("HTTP/1."); // $NON-NLS-1$
    if (httpVersion == 0) {
      output.append("0\r\n"); // $NON-NLS-1$
    } else {
      output.append("1\r\n"); // $NON-NLS-1$
    }
    // add user-specified request headers if any
    boolean hasContentLength = false;
    for (int i = 0; i < reqHeader.length(); i++) {
      String key = reqHeader.getKey(i);
      if (key != null) {
        String lKey = key.toLowerCase();
        if ((os != null && !os.isChunked())
            || (!lKey.equals("transfer-encoding")
                && !lKey //$NON-NLS-1$
                    .equals("content-length"))) { // $NON-NLS-1$
          output.append(key);
          String value = reqHeader.get(i);
          /*
           * duplicates are allowed under certain conditions see
           * http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2
           */
          if (lKey.equals("content-length")) { // $NON-NLS-1$
            hasContentLength = true;
            /*
             * if both setFixedLengthStreamingMode and
             * content-length are set, use fixedContentLength first
             */
            if (fixedContentLength >= 0) {
              value = String.valueOf(fixedContentLength);
            }
          }
          if (value != null) {
            output.append(": "); // $NON-NLS-1$
            output.append(value);
          }
          output.append("\r\n"); // $NON-NLS-1$
        }
      }
    }
    if (fixedContentLength >= 0 && !hasContentLength) {
      output.append("content-length: "); // $NON-NLS-1$
      output.append(String.valueOf(fixedContentLength));
      output.append("\r\n"); // $NON-NLS-1$
    }

    if (reqHeader.get("User-Agent") == null) { // $NON-NLS-1$
      output.append("User-Agent: "); // $NON-NLS-1$
      String agent = getSystemProperty("http.agent"); // $NON-NLS-1$
      if (agent == null) {
        output.append("Java"); // $NON-NLS-1$
        output.append(getSystemProperty("java.version")); // $NON-NLS-1$
      } else {
        output.append(agent);
      }
      output.append("\r\n"); // $NON-NLS-1$
    }
    if (reqHeader.get("Host") == null) { // $NON-NLS-1$
      output.append("Host: "); // $NON-NLS-1$
      output.append(url.getHost());
      int port = url.getPort();
      if (port > 0 && port != defaultPort) {
        output.append(':');
        output.append(Integer.toString(port));
      }
      output.append("\r\n"); // $NON-NLS-1$
    }
    if (reqHeader.get("Accept") == null) { // $NON-NLS-1$
      output.append("Accept: *; */*\r\n"); // $NON-NLS-1$
    }
    if (httpVersion > 0 && reqHeader.get("Connection") == null) { // $NON-NLS-1$
      output.append("Connection: Keep-Alive\r\n"); // $NON-NLS-1$
    }

    // if we are doing output make sure the appropriate headers are sent
    if (os != null) {
      if (reqHeader.get("Content-Type") == null) { // $NON-NLS-1$
        output.append("Content-Type: application/x-www-form-urlencoded\r\n"); // $NON-NLS-1$
      }
      if (os.isCached()) {
        if (reqHeader.get("Content-Length") == null) { // $NON-NLS-1$
          output.append("Content-Length: "); // $NON-NLS-1$
          output.append(Integer.toString(os.size()));
          output.append("\r\n"); // $NON-NLS-1$
        }
      } else if (os.isChunked()) {
        output.append("Transfer-Encoding: chunked\r\n"); // $NON-NLS-1$
      }
    }
    // end the headers
    output.append("\r\n"); // $NON-NLS-1$
    return output.toString().getBytes("ISO8859_1"); // $NON-NLS-1$
  }