Java Interactive Client
From Textserver wiki
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Example client to submit an interactive request to TextServer
4 // SERVICENAME service.
5 // Input may be a plain text or a text file. No ZIP files.
6 // Output will be in the requested format (XML, json, conll)
7 //
8 ////////////////////////////////////////////////////////////////////////
9
10
11 import org.apache.http.client.HttpClient;
12 import org.apache.http.HttpEntity;
13 import org.apache.http.HttpResponse;
14 import org.apache.http.HttpStatus;
15
16 import org.apache.http.client.methods.HttpPost;
17 import org.apache.http.entity.mime.MultipartEntityBuilder;
18 import org.apache.http.impl.client.HttpClientBuilder;
19 import org.apache.http.util.EntityUtils;
20
21 public class SERVICENAMEClientInteractive {
22
23 // service base URL
24 static final String url = "http://frodo.lsi.upc.edu:8080/TextWS/textservlet/ws/processQuery/";
25 static final String service = "SERVICENAME";
26 static final int BUFFERSIZE = 2048;
27
28 public static void main(String[] args) throws Exception {
29
30 // Get request parameters
31 System.out.print("Text to analyze: ");
32 String text = System.console().readLine();
33 System.out.print("Language: ");
34 String lang = System.console().readLine();
35 System.out.print("Output format (xml,json,conll,naf): ");
36 String out = System.console().readLine();
37 System.out.print("TextServer Username: ");
38 String user = System.console().readLine();
39 System.out.print("TextServer Password: ");
40 String pwd = System.console().readLine();
41
42 // Create request, fill query parameters
43 HttpPost request = new HttpPost(url+service);
44 request.setEntity(MultipartEntityBuilder.create()
45 .addTextBody("username", user)
46 .addTextBody("password", pwd)
47 .addTextBody("text_input", text)
48 .addTextBody("language", lang)
49 .addTextBody("output", out)
50 .addTextBody("interactive", "1")
51 .build()
52 );
53
54 // create client, send request, get response
55 HttpClient client = HttpClientBuilder.create().build();
56 HttpResponse response = client.execute(request);
57 String content = EntityUtils.toString(response.getEntity());
58
59 // handle connection errors
60 if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
61 System.out.println(response.getStatusLine() + " - " + content);
62 System.exit(1);
63 }
64
65 // No error, appropriately process response
66 // (e.g. parsing XML or JSON, and doing clever stuff with the content)
67 System.out.println(content);
68 }
69 }