import com.google.gson.Gson;import com.google.gson.GsonBuilder;import com.google.gson.JsonObject;import org.apache.http.*;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.entity.StringEntity;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.util.EntityUtils;import java.io.IOException;public class TestClient {protected String url;protected CloseableHttpClient httpClient;protected long lastTime;protected static Gson gson = new Gson();public TestClient(String url){this.url = url;httpClient = HttpClients.createDefault();}public String get(){HttpGet get = new HttpGet(url);try {long time1 = System.currentTimeMillis();CloseableHttpResponse response = httpClient.execute(get);lastTime = System.currentTimeMillis() - time1;HttpEntity resEntity = response.getEntity();return EntityUtils.toString(resEntity,"UTF-8");}catch (Exception e){e.printStackTrace();return null;}}public String post(JsonObject data){HttpPost post = new HttpPost(url);post.setHeader("Content-Type", "application/json;charset=UTF-8");Gson gson = new GsonBuilder().create();StringEntity se = new StringEntity(gson.toJson(data), "UTF-8");se.setContentType("application/json");post.setEntity(se);try {CloseableHttpResponse response = httpClient.execute(post);HttpEntity resEntity = response.getEntity();return EntityUtils.toString(resEntity,"UTF-8");}catch (IOException e){e.printStackTrace();return null;}}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。