I want to send http post with interceptor ,include header( cookies ...etc) and body .I test it OK with post man ( require enable add on interceptor in chrome). How can i send http post with java code.Please help me.
Asked
Active
Viewed 591 times
-2

user3089993
- 3
- 7
-
Possible duplicate of [Sending HTTP POST Request In Java](https://stackoverflow.com/questions/3324717/sending-http-post-request-in-java) – Noctem Jan 29 '19 at 06:47
-
As of Java 11, the apache HTTP Client is included in the JDK. With earlier version, use HttpUrlConnection. – Maurice Perry Jan 29 '19 at 07:07
1 Answers
0
If you want to send a POST request to a URL you just need an HTTP library. A common one is OkHttp. Example straight from their site:
public static final MediaType JSON
= MediaType.get("application/json; charset=utf-8");
OkHttpClient client = new OkHttpClient();
String post(String url, String json) throws IOException {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
try (Response response = client.newCall(request).execute()) {
return response.body().string();
}
}

Noctem
- 40
- 6
-
Hi Noctem,How can i add header and body.In your answer i only see body json ? – user3089993 Jan 29 '19 at 07:15