Difference between revisions of "Java Interactive Client"
From Textserver wiki
Line 8: | Line 8: | ||
// | // | ||
//////////////////////////////////////////////////////////////////////// | //////////////////////////////////////////////////////////////////////// | ||
− | |||
import org.apache.http.client.HttpClient; | import org.apache.http.client.HttpClient; |
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 }