org.json - HTTP

La classe HTTP fournit des méthodes statiques pour convertir le texte d'en-tête du navigateur Web en un JSONObject, et vice versa.

Les méthodes suivantes sont couvertes dans l'exemple.

  • toJSONObject(String) - Convertit un texte d'en-tête en objet JSONObject.

  • toString(JSONObject) - Convertit un JSONObject en texte d'en-tête.

Exemple

import org.json.HTTP;
import org.json.JSONObject;

public class JSONDemo {
   public static void main(String[] args) { 
      JSONObject jsonObject = new JSONObject();
      jsonObject.put("Method", "POST");
      jsonObject.put("Request-URI", "http://www.tutorialspoint.com/");
      jsonObject.put("HTTP-Version", "HTTP/1.1");
        
      //Case 1: Converts JSONObject of Header to String
      String headerText = HTTP.toString(jsonObject);
      System.out.println(headerText); 
        
      headerText = "POST \"http://www.tutorialspoint.com/\" HTTP/1.1";
      //Case 2: Converts Header String to JSONObject
      System.out.println(HTTP.toJSONObject(headerText));
   }
}

Production

POST "http://www.tutorialspoint.com/" HTTP/1.1

{"Request-URI":"http://www.tutorialspoint.com/","Method":"POST","HTTP-Version":"HTTP/1.1"}