0

I have a POST method in the server side, which get a JSON (as raw text) and some headers. The response is data (a file content), which send by the server to the client.

I search for a way to write the client in Java. The client sends a POST message with raw text, and some headers, and know to get the response.

All the example I saw use the HttpsURLConnection, but I didn't see any way to send the raw text (JSON), and get the data content.

Or Smith
  • 3,556
  • 13
  • 42
  • 69
  • check this http://stackoverflow.com/questions/9623158/curl-and-httpurlconnection-post-json-data – Tapos Jun 11 '14 at 08:29

1 Answers1

0

Perhaps something like this could help ? (Using org.apache.httpcomponents:httpcore:4.3.2)

HttpEntity reqEntity = MultipartEntityBuilder.create()
    .addPart("query", new InputStreamBody(rawData, "rawData.xml"))                  
    .build();

HttpPost httppost = new HttpPost(serverUrl);
httppost.addHeader("SomeHeader", "SomeValue");
httppost.setEntity(reqEntity);

HttpClient httpclient = HttpClientBuilder.create().build();
HttpResponse response = httpclient.execute(httppost);
int statusCode = response.getStatusLine().getStatusCode();
InputStream os = response.getEntity().getContent();

...

Shimbawa
  • 262
  • 3
  • 12