c923d709a3825c25396a511276cf98e185624b89
[yaffs-website] / vendor / jcalderonzumba / gastonjs / examples / java / GastonJSClient.java
1 import java.io.InputStream;
2 import java.lang.System;
3
4 import org.apache.http.HttpResponse;
5 import org.apache.http.client.methods.HttpPost;
6 import org.apache.http.entity.StringEntity;
7 import org.apache.http.impl.client.HttpClientBuilder;
8 import org.apache.commons.io.IOUtils;
9 import org.apache.http.impl.client.CloseableHttpClient;
10
11 public class GastonJSClient{
12
13     public static void main(String[] args) throws Exception{
14             // write your code here
15             String visitPage = "{\n" +
16                     "  \"name\": \"visit\",\n" +
17                     "  \"args\":[\n" +
18                     "    \"http://www.google.es\"\n" +
19                     "  ]\n" +
20                     "}";
21             String renderPage = "{\"name\":\"render\",\"args\":[\"/Users/juan/Downloads/page_image.png\",true,null]}";
22             CloseableHttpClient httpClient = HttpClientBuilder.create().build();
23             try {
24                 //Do the visit
25                 HttpPost request = new HttpPost("http://127.0.0.1:8510/v1/api");
26                 StringEntity params = new StringEntity(visitPage);
27                 request.addHeader("content-type", "application/json");
28                 request.setEntity(params);
29                 HttpResponse response = httpClient.execute(request);
30                 InputStream body = response.getEntity().getContent();
31                 String myString = IOUtils.toString(body, "UTF-8");
32                 System.out.println(myString);
33                 //Do the page print
34                 params = new StringEntity(renderPage);
35                 request.setEntity(params);
36                 response = httpClient.execute(request);
37                 body = response.getEntity().getContent();
38                 myString = IOUtils.toString(body, "UTF-8");
39                 System.out.println(myString);
40                 // handle response here...
41             } catch (Exception ex) {
42                 // handle exception here
43                 System.out.println(ex.toString());
44             } finally {
45                 httpClient.close();
46             }
47         }
48 }