Wie kann ich ein JSON-Objekt in einem http-Beitrag in Java senden?
Ich möchte ein JSON-Objekt senden (Beachten Sie, dass es nicht in einen String konvertiert werden sollte, da der serverseitige Code auf dem Spring-Starter-Projekt basiert und Parameter wie (@RequestBody PCAP pcap) enthält.) Ich habe den folgenden Code, aber er konvertiert den Körper in eine Zeichenfolge, die mir 400 schlechte Anfrage gibt.
private void sendData(String ip){
try{
JSONObject json=new JSONObject();
json.put("time_range", "22-23");
json.put("flow_id", "786");
json.put("ip_a", "192.65.78.22");
json.put("port_a", "8080");
json.put("regex", "%ab");
URL url=new URL("http://"+ip+":8080/pcap");
HttpURLConnection httpcon=(HttpURLConnection)url.openConnection();
httpcon.setDoOutput(true);
httpcon.setRequestMethod("POST");
httpcon.setRequestProperty("Accept", "application/json");
httpcon.setRequestProperty("Content-Type", "application/json");
Cookie cookie=new Cookie("user", "abc");
cookie.setValue("store");
httpcon.setRequestProperty("Accept", "application/json");
httpcon.setRequestProperty("Cookie", cookie.getValue());
OutputStreamWriter output=new OutputStreamWriter(httpcon.getOutputStream());
System.out.println(json);
output.write(json.toString());
httpcon.connect();
String output1=httpcon.getResponseMessage();
System.out.println(output1);
}catch(Exception e){
}
}
Hinweis: Serverseitiger Code ist
@RequestMapping(value = URIConstansts.PCAP, produces = { "application/json" }, method = RequestMethod.POST)
public ResponseEntity getPcap(HttpServletRequest request,@RequestBody PcapParameters pcap_params )