Пример #1
0
  /**
   * Convert an HTTP header string into a JSONObject. It can be a request header or a response
   * header. A request header will contain
   *
   * <pre>{
   *    Method: "POST" (for example),
   *    "Request-URI": "/" (for example),
   *    "HTTP-Version": "HTTP/1.1" (for example)
   * }</pre>
   *
   * A response header will contain
   *
   * <pre>{
   *    "HTTP-Version": "HTTP/1.1" (for example),
   *    "Status-Code": "200" (for example),
   *    "Reason-Phrase": "OK" (for example)
   * }</pre>
   *
   * In addition, the other parameters in the header will be captured, using the HTTP field names as
   * JSON names, so that
   *
   * <pre>
   *    Date: Sun, 26 May 2002 18:06:04 GMT
   *    Cookie: Q=q2=PPEAsg--; B=677gi6ouf29bn&b=2&f=s
   *    Cache-Control: no-cache</pre>
   *
   * become
   *
   * <pre>{...
   *    Date: "Sun, 26 May 2002 18:06:04 GMT",
   *    Cookie: "Q=q2=PPEAsg--; B=677gi6ouf29bn&b=2&f=s",
   *    "Cache-Control": "no-cache",
   * ...}</pre>
   *
   * It does no further checking or conversion. It does not parse dates. It does not do '%'
   * transforms on URLs.
   *
   * @param string An HTTP header string.
   * @return A JSONObject containing the elements and attributes of the XML string.
   * @throws JSONException
   */
  public static JSONObject toJSONObject(String string) throws JSONException {
    JSONObject o = new JSONObject();
    HTTPTokener x = new HTTPTokener(string);
    String t;

    t = x.nextToken();
    if (t.toUpperCase().startsWith("HTTP")) {

      // Response

      o.put("HTTP-Version", t);
      o.put("Status-Code", x.nextToken());
      o.put("Reason-Phrase", x.nextTo('\0'));
      x.next();

    } else {

      // Request

      o.put("Method", t);
      o.put("Request-URI", x.nextToken());
      o.put("HTTP-Version", x.nextToken());
    }

    // Fields

    while (x.more()) {
      String name = x.nextTo(':');
      x.next(':');
      o.put(name, x.nextTo('\0'));
      x.next();
    }
    return o;
  }
Пример #2
0
 public static JSONObject toJSONObject(String string) throws JSONException {
   JSONObject jo = new JSONObject();
   HTTPTokener x = new HTTPTokener(string);
   Object token = x.nextToken();
   if (token.toUpperCase().startsWith("HTTP")) {
     jo.put("HTTP-Version", token);
     jo.put("Status-Code", x.nextToken());
     jo.put("Reason-Phrase", x.nextTo('\u0000'));
     x.next();
   } else {
     jo.put("Method", token);
     jo.put("Request-URI", x.nextToken());
     jo.put("HTTP-Version", x.nextToken());
   }
   while (x.more()) {
     String name = x.nextTo(':');
     x.next(':');
     jo.put(name, x.nextTo('\u0000'));
     x.next();
   }
   return jo;
 }