Convert an HTTP header string into a JSONObject. It can be a request header or a response header. A request header will contain { Method: "POST" (for example), "Request-URI": "/" (for example), "HTTP-Version": "HTTP/1.1" (for example) } A response header will contain { "H
(String string)
| 69 | * @throws JSONException |
| 70 | */ |
| 71 | public static JSONObject toJSONObject(String string) throws JSONException { |
| 72 | JSONObject jo = new JSONObject(); |
| 73 | HTTPTokener x = new HTTPTokener(string); |
| 74 | String token; |
| 75 | |
| 76 | token = x.nextToken(); |
| 77 | if (token.toUpperCase().startsWith("HTTP")) { |
| 78 | |
| 79 | // Response |
| 80 | |
| 81 | jo.put("HTTP-Version", token); |
| 82 | jo.put("Status-Code", x.nextToken()); |
| 83 | jo.put("Reason-Phrase", x.nextTo('\0')); |
| 84 | x.next(); |
| 85 | |
| 86 | } else { |
| 87 | |
| 88 | // Request |
| 89 | |
| 90 | jo.put("Method", token); |
| 91 | jo.put("Request-URI", x.nextToken()); |
| 92 | jo.put("HTTP-Version", x.nextToken()); |
| 93 | } |
| 94 | |
| 95 | // Fields |
| 96 | |
| 97 | while (x.more()) { |
| 98 | String name = x.nextTo(':'); |
| 99 | x.next(':'); |
| 100 | jo.put(name, x.nextTo('\0')); |
| 101 | x.next(); |
| 102 | } |
| 103 | return jo; |
| 104 | } |
| 105 | |
| 106 | |
| 107 | /** |