[{"data":1,"prerenderedAt":78},["Reactive",2],{"/samples/java/httpclient":3,"related:VXBO1BNm7z":60},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"language":10,"library":11,"link":12,"body":13,"_type":55,"_id":56,"_source":57,"_file":58,"_extension":59},"/samples/java/httpclient","java",false,"","Httpclient","HttpClient is a class provided by the Java standard library that facilitates making HTTP requests and receiving HTTP responses. It offers a simple and flexible API for performing tasks such as sending GET, POST, PUT, and DELETE requests, handling headers and content, managing cookies, and configuring timeouts and other request parameters.","Java","HttpClient","https://openjdk.org/groups/net/httpclient/intro.html",{"type":14,"children":15,"toc":52},"root",[16,31,43],{"type":17,"tag":18,"props":19,"children":20},"element","p",{},[21,29],{"type":17,"tag":22,"props":23,"children":26},"a",{"href":12,"rel":24},[25],"nofollow",[27],{"type":28,"value":11},"text",{"type":28,"value":30}," is a class provided by the Java standard library that facilitates making HTTP requests and receiving HTTP responses. It offers a simple and flexible API for performing tasks such as sending GET, POST, PUT, and DELETE requests, handling headers and content, managing cookies, and configuring timeouts and other request parameters.",{"type":17,"tag":32,"props":33,"children":37},"pre",{"className":34,"code":36,"language":5,"meta":7},[35],"language-java","import org.apache.http.HttpResponse;\nimport org.apache.http.client.HttpClient;\nimport org.apache.http.client.methods.HttpPost;\nimport org.apache.http.entity.StringEntity;\nimport org.apache.http.impl.client.HttpClients;\nimport org.apache.http.util.EntityUtils;\n\nimport java.nio.file.Files;\nimport java.nio.file.Paths;\n\npublic class PDFConvert {\n\n    public byte[] convert(String apiKey, String params, String endpoint) throws Exception {\n        if (!endpoint.equals(\"pdf\") && !endpoint.equals(\"png\") && !endpoint.equals(\"jpg\") && !endpoint.equals(\"webp\")) {\n            throw new Exception(\"Invalid endpoint\");\n        }\n\n        HttpClient httpClient = HttpClients.createDefault();\n\n        HttpPost request = new HttpPost(\"https://api.pdfshift.io/v3/convert/\" + endpoint);\n        request.setHeader(\"Content-Type\", \"application/json\");\n        request.setHeader(\"X-API-Key\", apiKey);\n\n        StringEntity entity = new StringEntity(params);\n        request.setEntity(entity);\n\n        HttpResponse response = httpClient.execute(request);\n\n        if (response.getStatusLine().getStatusCode() != 200) {\n            throw new Exception(\"Http request failed with status code: \" + response.getStatusLine().getStatusCode());\n        }\n\n        byte[] binary = EntityUtils.toByteArray(response.getEntity());\n\n        if (params.contains(\"\\\"filename\\\"\") || params.contains(\"\\\"webhook\\\"\")) {\n            return EntityUtils.toString(response.getEntity()).getBytes();\n        }\n\n        return binary;\n    }\n}\n",[38],{"type":17,"tag":39,"props":40,"children":41},"code",{"__ignoreMap":7},[42],{"type":28,"value":36},{"type":17,"tag":32,"props":44,"children":47},{"className":45,"code":46,"language":5,"meta":7},[35],"public static void main(String[] args) {\n    try {\n        String apiKey = \"sk_XXXXXXXXXXXXXX\";\n        String params = \"{\\\"source\\\": \\\"https://en.wikipedia.org/wiki/REST\\\"}\";\n\n        PDFConvert pdfConvert = new PDFConvert();\n        byte[] binary = pdfConvert.convert(apiKey, params, \"pdf\");\n\n        Files.write(Paths.get(\"result.pdf\"), binary);\n    } catch (Exception e) {\n        e.printStackTrace();\n    }\n}\n",[48],{"type":17,"tag":39,"props":49,"children":50},{"__ignoreMap":7},[51],{"type":28,"value":46},{"title":7,"searchDepth":53,"depth":53,"links":54},2,[],"markdown","content:samples:java:httpclient.md","content","samples/java/httpclient.md","md",[61,62,66,70,74],{"_path":4,"language":10,"library":11,"_id":56},{"_path":63,"language":10,"library":64,"_id":65},"/samples/java/javanet","java.net","content:samples:java:javanet.md",{"_path":67,"language":10,"library":68,"_id":69},"/samples/java/netty","Netty","content:samples:java:netty.md",{"_path":71,"language":10,"library":72,"_id":73},"/samples/java/okhttp","OkHttp","content:samples:java:okhttp.md",{"_path":75,"language":10,"library":76,"_id":77},"/samples/java/retrofit","Retrofit","content:samples:java:retrofit.md",1785426823653]