Sample code for 30+ languages & platforms
Java

Example: Http.DownloadSb method

See more HTTP Examples

Demonstrates the DownloadSb method.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.StringBuilder;

public class ChilkatExample {

    public static void main(String[] args) {
        boolean success = false;

        Http http = new Http();
        http.setKeepResponseBody(true);

        StringBuilder sb = new StringBuilder();
        try {
            http.downloadSb("https://chilkatsoft.com/testData/helloWorld.txt", "utf-8", sb);
            success = true;
        } catch (ChilkatException e) {
            success = false;
        }

        int statusCode = http.getLastStatus();
        if (!success) {
            if (statusCode == 0) {
                // Unable to either send the request or get the response.
                System.out.println(http.getLastErrorText());
            } else {
                // We got a response, but the status code was not in the 200s
                System.out.println("Response status code: " + statusCode);
                // Examine the response body.
                System.out.println("Response body:");
                System.out.println(http.getLastResponseBody());
            }

            System.out.println("Download failed.");
        } else {
            System.out.println("Download success, response status = " + statusCode);
            System.out.println(sb.getAsString());
        }
    }
}