[RESOLVIDO] Enviar JSON via HttpClient como valor

Tenho um HttpClient que envia um Json para um HttpServer. Porém, o server recebe o Json como nome, e não como valor. Como posso ajeitar isso? PS: Sou iniciante em Java, peço compreensão (:

Código do HTTPCLIENT:

public class ClientHTTP {

	public static void main(String[] args) throws ClientProtocolException, IOException, InterruptedException {
		SendData trab = new SendData();
		String JsonDeDados;
		HttpClient client = HttpClientBuilder.create().build();
		CloseableHttpClient clientclose = HttpClients.createDefault();
		HttpPost post = new HttpPost("http://localhost:7070/data");
		do {
			JsonDeDados = trab.sendMessageDATA();
			

			List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
			urlParameters.add(new BasicNameValuePair(JsonDeDados, "Infos"));
			StringEntity entity = new StringEntity(JsonDeDados);

			post.setEntity(entity);
			post.setHeader("Accept", "application/json");
			post.setHeader("Content-Type", "application/json");
			post.setHeader("Content-Type", "application/x-www-form-urlencoded");

			CloseableHttpResponse response = (CloseableHttpResponse) client.execute(post);
			System.out.println("Código da Resposta: " + response.getStatusLine().getStatusCode());
			clientclose.close();
			post.releaseConnection();
			Thread.sleep(1000);
		} while (true);
	}
}

Exemplo do Json de resposta:

{“pacote:”:“net”,“gateway”:“xxx.xx.xx.x”,“hostname”:“home”,“dominio”:"(none)",“dns”:“xxx.x.x.xx”,“ips”:[{“ip”:“xxx.xx.xx.x”}]} = null

Você só pode ter um Content-Type em teu header e ele deve ser compatível com o tipo de dado enviado, no caso, application/json

1 curtida

Muito útil como sempre! Valeu Darlan!