HttpUtils发送delete方法
生活随笔
收集整理的這篇文章主要介紹了
HttpUtils发送delete方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
問題描述:需要訪問外部delete修飾的接口。hutool工具類的HttpUtils修飾的方法中只有get 和post 請求,需要自己重寫
如果使用post 和get 則會報405問題
package com.zhada.cloud.transit.infrastructure.utils;import com.alibaba.fastjson.JSON; import org.apache.http.HttpEntity; import org.apache.http.client.ResponseHandler; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.BasicResponseHandler; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils;import java.io.IOException; import java.util.Map;public class HttpUtils {/*** HttpClient發送json字符串post請求** @param* @param json* @return*/public static HttpResult sendPost(String url, Map<String, Object> headers, String json) {HttpResult result = null;CloseableHttpClient httpClient = HttpClients.createDefault();ResponseHandler<String> responseHandler = new BasicResponseHandler();try {//第一步:創建HttpClient對象httpClient = HttpClients.createDefault();//第二步:創建httpPost對象HttpPost httpPost = new HttpPost(url);//第三步:給httpPost設置JSON格式的參數StringEntity requestEntity = new StringEntity(json, "utf-8");requestEntity.setContentEncoding("UTF-8");if (headers !=null){for (Map.Entry<String, Object> entry : headers.entrySet()) {httpPost.setHeader(entry.getKey(), String.valueOf(entry.getValue()));}}httpPost.setEntity(requestEntity);//第四步:發送HttpPost請求,獲取返回值String msg = httpClient.execute(httpPost, responseHandler);result = JSON.parseObject(msg,HttpResult.class);} catch (Exception e) {e.printStackTrace();} finally {try {httpClient.close();} catch (IOException e) {e.printStackTrace();}}//第五步:處理返回值return result ;}public static String doDelete(String data, String url) throws IOException {CloseableHttpClient client = null;HttpDeleteWithBody httpDelete = null;String result = null;try {client = HttpClients.createDefault();httpDelete = new HttpDeleteWithBody(url);httpDelete.addHeader("Content-type","application/json; charset=utf-8");httpDelete.setHeader("Accept", "application/json; charset=utf-8");httpDelete.setEntity(new StringEntity(data));CloseableHttpResponse response = client.execute(httpDelete);HttpEntity entity = response.getEntity();result = EntityUtils.toString(entity);if (200 == response.getStatusLine().getStatusCode()) {}} catch (Exception e) {} finally {client.close();}return result;}public static class HttpResult{private String flag;private String code;private String msg;private String lid;private String datas;public String getFlag() {return flag;}public void setFlag(String flag) {this.flag = flag;}public String getCode() {return code;}public void setCode(String code) {this.code = code;}public String getMsg() {return msg;}public void setMsg(String msg) {this.msg = msg;}public String getLid() {return lid;}public void setLid(String lid) {this.lid = lid;}public String getDatas() {return datas;}public void setDatas(String datas) {this.datas = datas;}} } package com.zhada.cloud.transit.infrastructure.utils;import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;import java.net.URI;public class HttpDeleteWithBody extends HttpEntityEnclosingRequestBase {public static final String METHOD_NAME = "DELETE";/*** 獲取方法(必須重載)** @return*/@Overridepublic String getMethod() {return METHOD_NAME;}public HttpDeleteWithBody(final String uri) {super();setURI(URI.create(uri));}public HttpDeleteWithBody(final URI uri) {super();setURI(uri);}public HttpDeleteWithBody() {super();}}?
總結
以上是生活随笔為你收集整理的HttpUtils发送delete方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 贴片电阻制造流程
- 下一篇: jeecg 框架 swagger后台接口