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