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 <pre
(String string)
| 83 | * string. |
| 84 | */ |
| 85 | public static JSONObject toJSONObject(String string) throws JSONException { |
| 86 | JSONObject jo = new JSONObject(); |
| 87 | HTTPTokener x = new HTTPTokener(string); |
| 88 | String token; |
| 89 | |
| 90 | token = x.nextToken(); |
| 91 | if (token.toUpperCase().startsWith("HTTP")) { |
| 92 | |
| 93 | // Response |
| 94 | |
| 95 | jo.put("HTTP-Version", token); |
| 96 | jo.put("Status-Code", x.nextToken()); |
| 97 | jo.put("Reason-Phrase", x.nextTo('\0')); |
| 98 | x.next(); |
| 99 | |
| 100 | } else { |
| 101 | |
| 102 | // Request |
| 103 | |
| 104 | jo.put("Method", token); |
| 105 | jo.put("Request-URI", x.nextToken()); |
| 106 | jo.put("HTTP-Version", x.nextToken()); |
| 107 | } |
| 108 | |
| 109 | // Fields |
| 110 | |
| 111 | while (x.more()) { |
| 112 | String name = x.nextTo(':'); |
| 113 | x.next(':'); |
| 114 | jo.put(name, x.nextTo('\0')); |
| 115 | x.next(); |
| 116 | } |
| 117 | return jo; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Convert a JSONObject into an HTTP header. A request header must contain |