Created
April 19, 2024 03:23
-
-
Save v5tech/764e8ee7154a5f353f73157ace40a8ce to your computer and use it in GitHub Desktop.
http get request with body
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.aimeizi; | |
import cn.hutool.json.JSONObject; | |
import org.apache.commons.io.IOUtils; | |
import org.apache.http.HttpEntity; | |
import org.apache.http.client.methods.CloseableHttpResponse; | |
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; | |
import org.apache.http.client.methods.HttpGet; | |
import org.apache.http.entity.StringEntity; | |
import org.apache.http.impl.client.CloseableHttpClient; | |
import org.apache.http.impl.client.HttpClients; | |
import java.net.URI; | |
import java.nio.charset.StandardCharsets; | |
public class HttpGetRequestWithBodyAppliaction { | |
public static void main(String[] args) { | |
String URL = "https://httpbin.org/anything"; | |
JSONObject body = new JSONObject(); | |
body.put("clientId", "cli_a692154013e6900c"); | |
body.put("clientSecret", "pFFaSoB54jJivS8tCVy7CcAmoEJADIeS"); | |
try { | |
StringEntity entityValue = new StringEntity(body.toString()); | |
CloseableHttpClient client = HttpClients.createDefault(); | |
HttpGetWithEntity httpGetWithEntity = new HttpGetWithEntity(URL); | |
httpGetWithEntity.setEntity(entityValue); | |
httpGetWithEntity.setHeader("Accept", "application/json"); | |
httpGetWithEntity.setHeader("Content-type", "application/json"); | |
CloseableHttpResponse responseClient = client.execute(httpGetWithEntity); | |
HttpEntity entityResponse = responseClient.getEntity(); | |
String response = IOUtils.toString(entityResponse.getContent(), StandardCharsets.UTF_8); | |
System.out.println(response); | |
client.close(); | |
} catch (Exception ex) { | |
System.out.println(ex); | |
} | |
} | |
} | |
class HttpGetWithEntity extends HttpEntityEnclosingRequestBase { | |
public HttpGetWithEntity() { | |
super(); | |
} | |
public HttpGetWithEntity(URI uri) { | |
super(); | |
setURI(uri); | |
} | |
public HttpGetWithEntity(String uri) { | |
super(); | |
setURI(URI.create(uri)); | |
} | |
@Override | |
public String getMethod() { | |
return HttpGet.METHOD_NAME; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment