Java Requires Chilkat v11.0.0+
Chilkat2 Java
Example: Http.HttpReq method
Demonstrates how to call the HttpReq method.Chilkat Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpRequest;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;
public class ChilkatExample {
public static void main(String[] args) {
boolean success = false;
Http http = new Http();
success = false;
// This example will send an HTTP POST with the body of the HTTP request containing JSON.
// Create the following JSON: { "name": "Harry", "state": "FL" }
JsonObject json = new JsonObject();
json.updateString("name", "Harry");
json.updateString("state", "FL");
// Specify the details of the request
HttpRequest req = new HttpRequest();
req.setHttpVerb("POST");
req.setContentType("application/json");
req.loadBodyFromString(json.emit(), "utf-8");
// Send the charset attribute in the HTTP request header.
req.setSendCharset(true);
req.setCharset("utf-8");
// Send the POST
// You can send the POST to the URL below.
// The response will contain the raw body of the request that was sent.
// (i.e. everything except the HTTP request header).
HttpResponse resp = new HttpResponse();
try {
http.httpReq("https://www.chilkatsoft.com/echo_request_body.asp", req, resp);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// Examine the HTTP request header we sent:
System.out.println(http.getLastHeader());
// The response body contains the raw content of the HTTP request body we sent.
System.out.println(resp.getBodyStr());
// Sample output:
// POST /echo_request_body.asp HTTP/1.1
// Host: www.chilkatsoft.com
// Content-Type: application/json; charset=utf-8
// Content-Length: 29
//
//
// {"name":"Harry","state":"FL"}
}
}