[{"data":1,"prerenderedAt":219},["Reactive",2],{"/samples/java":3},[4,61,101,140,180],{"_path":5,"_dir":6,"_draft":7,"_partial":7,"_locale":8,"title":9,"description":10,"language":11,"library":12,"link":13,"body":14,"_type":56,"_id":57,"_source":58,"_file":59,"_extension":60},"/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":15,"children":16,"toc":53},"root",[17,32,44],{"type":18,"tag":19,"props":20,"children":21},"element","p",{},[22,30],{"type":18,"tag":23,"props":24,"children":27},"a",{"href":13,"rel":25},[26],"nofollow",[28],{"type":29,"value":12},"text",{"type":29,"value":31}," 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":18,"tag":33,"props":34,"children":38},"pre",{"className":35,"code":37,"language":6,"meta":8},[36],"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",[39],{"type":18,"tag":40,"props":41,"children":42},"code",{"__ignoreMap":8},[43],{"type":29,"value":37},{"type":18,"tag":33,"props":45,"children":48},{"className":46,"code":47,"language":6,"meta":8},[36],"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",[49],{"type":18,"tag":40,"props":50,"children":51},{"__ignoreMap":8},[52],{"type":29,"value":47},{"title":8,"searchDepth":54,"depth":54,"links":55},2,[],"markdown","content:samples:java:httpclient.md","content","samples/java/httpclient.md","md",{"_path":62,"_dir":6,"_draft":7,"_partial":7,"_locale":8,"title":63,"description":64,"language":11,"library":65,"link":66,"body":67,"_type":56,"_id":99,"_source":58,"_file":100,"_extension":60},"/samples/java/javanet","Javanet","java.net is a package in the Java standard library that provides functionalities for networking operations. It includes classes and interfaces for working with URLs, establishing network connections, sending and receiving data over sockets, and handling network protocols.","java.net","https://cr.openjdk.org/~egahlin/jep-349/javadocs/api/java.base/java/net/package-summary.html",{"type":15,"children":68,"toc":97},[69,79,88],{"type":18,"tag":19,"props":70,"children":71},{},[72,77],{"type":18,"tag":23,"props":73,"children":75},{"href":66,"rel":74},[26],[76],{"type":29,"value":65},{"type":29,"value":78}," is a package in the Java standard library that provides functionalities for networking operations. It includes classes and interfaces for working with URLs, establishing network connections, sending and receiving data over sockets, and handling network protocols.",{"type":18,"tag":33,"props":80,"children":83},{"className":81,"code":82,"language":6,"meta":8},[36],"import com.google.gson.Gson;\nimport com.google.gson.JsonObject;\nimport java.io.*;\nimport java.net.HttpURLConnection;\nimport java.net.URL;\n\npublic class Convert {\n    public static byte[] convert(String apiKey, JsonObject params, String endpoint) throws Exception {\n        if (!\"pdf\".equals(endpoint) && !\"png\".equals(endpoint) && !\"jpg\".equals(endpoint) && !\"webp\".equals(endpoint)) {\n            throw new IllegalArgumentException(\"Invalid endpoint.\");\n        }\n\n        URL url = new URL(\"https://api.pdfshift.io/v3/convert/\" + endpoint);\n        \n        HttpURLConnection conn = (HttpURLConnection) url.openConnection();\n        conn.setDoOutput(true);\n        conn.setRequestMethod(\"POST\");\n        conn.setRequestProperty(\"X-API-Key\", apiKey);\n        conn.setRequestProperty(\"Content-Type\", \"application/json\");\n        conn.getOutputStream().write(params.toString().getBytes());\n        \n        int responseCode = conn.getResponseCode();\n        if(responseCode != 200) {\n            throw new RuntimeException(\"HTTP POST failed with error code : \" + responseCode);\n        }\n\n        InputStream is = conn.getInputStream();\n        ByteArrayOutputStream buffer = new ByteArrayOutputStream();\n        int nRead;\n        byte[] data = new byte[1024];\n        while ((nRead = is.read(data, 0, data.length)) != -1) {\n            buffer.write(data, 0, nRead);\n        }\n        buffer.flush();\n        \n        if(params.has(\"filename\") || params.has(\"webhook\")) {\n            return new Gson().fromJson(new String(buffer.toByteArray()), JsonObject.class).toString().getBytes();\n        }\n\n        return buffer.toByteArray();\n    }\n}\n",[84],{"type":18,"tag":40,"props":85,"children":86},{"__ignoreMap":8},[87],{"type":29,"value":82},{"type":18,"tag":33,"props":89,"children":92},{"className":90,"code":91,"language":6,"meta":8},[36],"public static void main(String[] args) throws Exception {\n    String endpoint = \"pdf\";\n    String apiKey = \"sk_XXXXXXXXXXXXXX\";\n    JsonObject params = new JsonObject();\n    params.addProperty(\"source\", \"https://en.wikipedia.org/wiki/REST\");\n    byte[] result = convert(apiKey, params, endpoint); \n    \n    OutputStream outStream = new FileOutputStream(\"result.pdf\");\n    outStream.write(result);\n    outStream.close();\n}\n",[93],{"type":18,"tag":40,"props":94,"children":95},{"__ignoreMap":8},[96],{"type":29,"value":91},{"title":8,"searchDepth":54,"depth":54,"links":98},[],"content:samples:java:javanet.md","samples/java/javanet.md",{"_path":102,"_dir":6,"_draft":7,"_partial":7,"_locale":8,"title":103,"description":104,"language":11,"library":103,"link":105,"body":106,"_type":56,"_id":138,"_source":58,"_file":139,"_extension":60},"/samples/java/netty","Netty","Netty is a high-performance networking framework for building server-side and client-side applications in Java. It provides a flexible and event-driven architecture for handling network communication efficiently. Netty is commonly used for building scalable and low-latency network applications, such as web servers, proxy servers, and messaging systems.","https://netty.io/",{"type":15,"children":107,"toc":136},[108,118,127],{"type":18,"tag":19,"props":109,"children":110},{},[111,116],{"type":18,"tag":23,"props":112,"children":114},{"href":105,"rel":113},[26],[115],{"type":29,"value":103},{"type":29,"value":117}," is a high-performance networking framework for building server-side and client-side applications in Java. It provides a flexible and event-driven architecture for handling network communication efficiently. Netty is commonly used for building scalable and low-latency network applications, such as web servers, proxy servers, and messaging systems.",{"type":18,"tag":33,"props":119,"children":122},{"className":120,"code":121,"language":6,"meta":8},[36],"import io.netty.handler.codec.http.*;\nimport io.netty.buffer.Unpooled;\nimport io.netty.util.CharsetUtil;\n\nimport java.util.HashMap;\nimport java.util.Map;\n\nimport com.google.gson.Gson;\nimport com.google.gson.reflect.TypeToken;\n\npublic class Convert {\n    private static final String API_KEY = \"sk_XXXXXXXXXXXXXX\";\n    private static final String HOST = \"https://api.pdfshift.io/v3/convert/\";\n    \n    private static byte[] convert(String apiKey, Map\u003CString, String> params, String endpoint) throws Exception {\n        if(!Arrays.asList(\"pdf\", \"png\", \"jpg\", \"webp\").contains(endpoint)) {\n            throw new IllegalArgumentException(\"Invalid endpoint\");\n        }\n\n        String url = HOST + endpoint;\n        String paramsJson = new Gson().toJson(params);\n\n        FullHttpRequest request = new DefaultFullHttpRequest(\n                HttpVersion.HTTP_1_1, HttpMethod.POST, url,\n                Unpooled.copiedBuffer(paramsJson, CharsetUtil.UTF_8));\n\n        request.headers().set(HttpHeaderNames.CONTENT_TYPE, \"application/json\");\n        request.headers().set(\"X-API-Key\", apiKey);\n        \n\n        HttpClient client = new HttpClient();\n        FullHttpResponse response = client.sendRequest(request);\n\n        if (!HttpResponseStatus.OK.equals(response.status())) {\n            throw new RuntimeException(\"Failed : HTTP error code : \" + response.status());\n        }\n\n        if (params.containsKey(\"filename\") || params.containsKey(\"webhook\")) {\n            Map\u003CString, Object> responseMap = new Gson().fromJson(\n                    response.content().toString(CharsetUtil.UTF_8),\n                    new TypeToken\u003CMap\u003CString, Object>>() {\n                    }.getType());\n            return responseMap;\n        }\n\n        return response.content().array();\n    }\n}\n",[123],{"type":18,"tag":40,"props":124,"children":125},{"__ignoreMap":8},[126],{"type":29,"value":121},{"type":18,"tag":33,"props":128,"children":131},{"className":129,"code":130,"language":6,"meta":8},[36],"public static void main(String[] args) {\n    Map\u003CString, String> params = new HashMap\u003C>();\n    params.put(\"source\", \"https://en.wikipedia.org/wiki/REST\");\n    \n    byte[] result = convert(API_KEY, params, \"pdf\");\n    Files.write(Paths.get(\"result.pdf\"), result);\n}\n",[132],{"type":18,"tag":40,"props":133,"children":134},{"__ignoreMap":8},[135],{"type":29,"value":130},{"title":8,"searchDepth":54,"depth":54,"links":137},[],"content:samples:java:netty.md","samples/java/netty.md",{"_path":141,"_dir":6,"_draft":7,"_partial":7,"_locale":8,"title":142,"description":143,"language":11,"library":144,"link":145,"body":146,"_type":56,"_id":178,"_source":58,"_file":179,"_extension":60},"/samples/java/okhttp","Okhttp","OkHttp is a popular HTTP client library for Java and Android. It provides a simple and intuitive API for making HTTP requests and processing responses. OkHttp supports features like connection pooling, caching, interceptors, and asynchronous requests.","OkHttp","https://square.github.io/okhttp/",{"type":15,"children":147,"toc":176},[148,158,167],{"type":18,"tag":19,"props":149,"children":150},{},[151,156],{"type":18,"tag":23,"props":152,"children":154},{"href":145,"rel":153},[26],[155],{"type":29,"value":144},{"type":29,"value":157}," is a popular HTTP client library for Java and Android. It provides a simple and intuitive API for making HTTP requests and processing responses. OkHttp supports features like connection pooling, caching, interceptors, and asynchronous requests.",{"type":18,"tag":33,"props":159,"children":162},{"className":160,"code":161,"language":6,"meta":8},[36],"import okhttp3.*;\nimport java.io.IOException;\nimport java.nio.file.Files;\nimport java.nio.file.Paths;\nimport okhttp3.RequestBody;\nimport okhttp3.Response;\n\npublic class PDFShift {\n\n    private static final OkHttpClient client = new OkHttpClient();\n\n    public static 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 IllegalArgumentException(\"Invalid endpoint\");\n        }\n\n        String url = \"https://api.pdfshift.io/v3/convert/\" + endpoint;\n        \n        MediaType mediaType = MediaType.parse(\"application/json\");\n        RequestBody body = RequestBody.create(mediaType, params);\n        Request request = new Request.Builder()\n            .url(url)\n            .method(\"POST\", body)\n            .addHeader(\"X-API-Key\", apiKey)\n            .addHeader(\"Content-Type\", \"application/json\")\n            .build();\n\n        byte[] content;\n        try (Response response = client.newCall(request).execute()) {\n            if (!response.isSuccessful()) throw new IOException(\"Unexpected code \" + response);\n            content = response.body().bytes();\n        }\n\n        if (params.contains(\"\\\"filename\\\"\") || params.contains(\"\\\"webhook\\\"\")) {\n            return new Gson().fromJson(new String(content.toByteArray()), JsonObject.class).toString().getBytes();\n        }\n\n        return content;\n    }\n}\n",[163],{"type":18,"tag":40,"props":164,"children":165},{"__ignoreMap":8},[166],{"type":29,"value":161},{"type":18,"tag":33,"props":168,"children":171},{"className":169,"code":170,"language":6,"meta":8},[36],"public static void main(String[] args) throws Exception {\n    String apiKey = \"sk_XXXXXXXXXXXXXX\";\n    String params = \"{\\\"source\\\" : \\\"https://en.wikipedia.org/wiki/REST\\\"}\";\n    String endpoint = \"pdf\";\n\n    byte[] result = convert(apiKey, params, endpoint);\n    Files.write(Paths.get(\"result.pdf\"), result);\n}\n",[172],{"type":18,"tag":40,"props":173,"children":174},{"__ignoreMap":8},[175],{"type":29,"value":170},{"title":8,"searchDepth":54,"depth":54,"links":177},[],"content:samples:java:okhttp.md","samples/java/okhttp.md",{"_path":181,"_dir":6,"_draft":7,"_partial":7,"_locale":8,"title":182,"description":183,"language":11,"library":182,"link":184,"body":185,"_type":56,"_id":217,"_source":58,"_file":218,"_extension":60},"/samples/java/retrofit","Retrofit","Retrofit is a type-safe HTTP client library for Java and Android. It simplifies the process of consuming RESTful web services by generating Java interface definitions based on RESTful API endpoints. Retrofit handles the boilerplate code for making HTTP requests and parsing responses, making it easy to work with web APIs in Java applications.","https://square.github.io/retrofit/",{"type":15,"children":186,"toc":215},[187,197,206],{"type":18,"tag":19,"props":188,"children":189},{},[190,195],{"type":18,"tag":23,"props":191,"children":193},{"href":184,"rel":192},[26],[194],{"type":29,"value":182},{"type":29,"value":196}," is a type-safe HTTP client library for Java and Android. It simplifies the process of consuming RESTful web services by generating Java interface definitions based on RESTful API endpoints. Retrofit handles the boilerplate code for making HTTP requests and parsing responses, making it easy to work with web APIs in Java applications.",{"type":18,"tag":33,"props":198,"children":201},{"className":199,"code":200,"language":6,"meta":8},[36],"import okhttp3.MediaType;\nimport okhttp3.OkHttpClient;\nimport okhttp3.Request;\nimport okhttp3.RequestBody;\nimport okhttp3.Response;\n\nimport org.json.JSONObject;\n\nimport java.io.FileOutputStream;\nimport java.io.IOException;\nimport java.nio.file.Files;\nimport java.nio.file.Paths;\n\npublic class PDFConverter {\n\n    private static final MediaType JSON\n            = MediaType.get(\"application/json; charset=utf-8\");\n\n    private static OkHttpClient client = new OkHttpClient();\n\n    static byte[] convert(String apiKey, JSONObject params, String endpoint) throws IOException {\n        if (!endpoint.equals(\"pdf\") && !endpoint.equals(\"png\") && !endpoint.equals(\"jpg\") && !endpoint.equals(\"webp\")) {\n            throw new IllegalArgumentException(\"Invalid endpoint\");\n        }\n\n        String url = \"https://api.pdfshift.io/v3/convert/\" + endpoint;\n\n        RequestBody body = RequestBody.create(params.toString(), JSON);\n        Request request = new Request.Builder()\n                .url(url)\n                .post(body)\n                .addHeader(\"X-API-Key\", apiKey)\n                .build();\n        Response response = client.newCall(request).execute();\n\n        if (!response.isSuccessful()) {\n            throw new IOException(\"Unexpected code \" + response);\n        }\n\n        if (params.has(\"filename\") || params.has(\"webhook\")) {\n            // Return in JSON in this case\n            return new Gson().fromJson(new String(response.body().bytes()), JsonObject.class).toString().getBytes();\n        }\n\n        // Returns the bytes\n        return response.body().bytes();\n    }\n}\n",[202],{"type":18,"tag":40,"props":203,"children":204},{"__ignoreMap":8},[205],{"type":29,"value":200},{"type":18,"tag":33,"props":207,"children":210},{"className":208,"code":209,"language":6,"meta":8},[36],"public static void main(String[] args) throws IOException {\n    JSONObject params = new JSONObject();\n    params.put(\"source\", \"https://en.wikipedia.org/wiki/REST\");\n    byte[] binary = convert(\"sk_XXXXXXXXXXXXXX\", params, \"pdf\");\n    Files.write(Paths.get(\"result.pdf\"), binary);\n}\n",[211],{"type":18,"tag":40,"props":212,"children":213},{"__ignoreMap":8},[214],{"type":29,"value":209},{"title":8,"searchDepth":54,"depth":54,"links":216},[],"content:samples:java:retrofit.md","samples/java/retrofit.md",1785426822991]