org.json-HTTP

HTTP 클래스는 웹 브라우저의 헤더 텍스트를 JSONObject로 또는 그 반대로 변환하는 정적 메서드를 제공합니다.

다음 방법은 예제에서 다룹니다.

  • toJSONObject(String) − 헤더 텍스트를 JSONObject 객체로 변환합니다.

  • toString(JSONObject) − JSONObject를 헤더 텍스트로 변환합니다.

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));
   }
}

산출

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

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