Difference between revisions of "Java Interactive Client"
From Textserver wiki
(Created page with "<syntaxhighlight lang="python" line="1" > </syntaxhighlight>") |
|||
Line 1: | Line 1: | ||
− | <syntaxhighlight lang=" | + | <syntaxhighlight lang="java" line="1" > |
+ | //////////////////////////////////////////////////////////////////////// | ||
+ | // | ||
+ | // Example client to submit an interactive request to TextServer | ||
+ | // SERVICENAME service. | ||
+ | // Input may be a plain text or a text file. No ZIP files. | ||
+ | // Output will be in the requested format (XML, json, conll) | ||
+ | // | ||
+ | //////////////////////////////////////////////////////////////////////// | ||
+ | |||
+ | |||
+ | import org.apache.http.client.HttpClient; | ||
+ | import org.apache.http.HttpEntity; | ||
+ | import org.apache.http.HttpResponse; | ||
+ | import org.apache.http.HttpStatus; | ||
+ | |||
+ | import org.apache.http.client.methods.HttpPost; | ||
+ | import org.apache.http.entity.mime.MultipartEntityBuilder; | ||
+ | import org.apache.http.impl.client.HttpClientBuilder; | ||
+ | import org.apache.http.util.EntityUtils; | ||
+ | |||
+ | public class SERVICENAMEClientInteractive { | ||
+ | |||
+ | // service base URL | ||
+ | static final String url = "http://frodo.lsi.upc.edu:8080/TextWS/textservlet/ws/processQuery/"; | ||
+ | static final String service = "SERVICENAME"; | ||
+ | static final int BUFFERSIZE = 2048; | ||
+ | |||
+ | public static void main(String[] args) throws Exception { | ||
+ | |||
+ | // Get request parameters | ||
+ | System.out.print("Text to analyze: "); | ||
+ | String text = System.console().readLine(); | ||
+ | System.out.print("Language: "); | ||
+ | String lang = System.console().readLine(); | ||
+ | System.out.print("Output format (xml,json,conll,naf): "); | ||
+ | String out = System.console().readLine(); | ||
+ | System.out.print("TextServer Username: "); | ||
+ | String user = System.console().readLine(); | ||
+ | System.out.print("TextServer Password: "); | ||
+ | String pwd = System.console().readLine(); | ||
+ | |||
+ | // Create request, fill query parameters | ||
+ | HttpPost request = new HttpPost(url+service); | ||
+ | request.setEntity(MultipartEntityBuilder.create() | ||
+ | .addTextBody("username", user) | ||
+ | .addTextBody("password", pwd) | ||
+ | .addTextBody("text_input", text) | ||
+ | .addTextBody("language", lang) | ||
+ | .addTextBody("output", out) | ||
+ | .addTextBody("interactive", "1") | ||
+ | .build() | ||
+ | ); | ||
+ | |||
+ | // create client, send request, get response | ||
+ | HttpClient client = HttpClientBuilder.create().build(); | ||
+ | HttpResponse response = client.execute(request); | ||
+ | String content = EntityUtils.toString(response.getEntity()); | ||
+ | |||
+ | // handle connection errors | ||
+ | if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { | ||
+ | System.out.println(response.getStatusLine() + " - " + content); | ||
+ | System.exit(1); | ||
+ | } | ||
+ | |||
+ | // No error, appropriately process response | ||
+ | // (e.g. parsing XML or JSON, and doing clever stuff with the content) | ||
+ | System.out.println(content); | ||
+ | } | ||
+ | } | ||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 13:00, 20 January 2016
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 }