From 220f21a574d70a381e8a928c6a0b7893b723bf18 Mon Sep 17 00:00:00 2001 From: fwq <582742538@qq.com> Date: 星期二, 09 十一月 2021 19:53:09 +0800 Subject: [PATCH] 返回错误信息 --- src/main/java/com/hx/util/RequestMethod.java | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 46 insertions(+), 1 deletions(-) diff --git a/src/main/java/com/hx/util/RequestMethod.java b/src/main/java/com/hx/util/RequestMethod.java index 9c0b36b..6aa7102 100644 --- a/src/main/java/com/hx/util/RequestMethod.java +++ b/src/main/java/com/hx/util/RequestMethod.java @@ -62,7 +62,7 @@ conn.setDoOutput(true);//鍏佽瀵瑰杈撳嚭鏁版嵁 conn.setDoInput(true); conn.setUseCaches(false); - conn.setRequestProperty("Charset", "utf-8"); + conn.setRequestProperty("Charset", encoding); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); //conn.setRequestProperty("Content-Length", String.valueOf(entity.length)); OutputStreamWriter outStream = new OutputStreamWriter(conn.getOutputStream(),"UTF-8"); @@ -76,6 +76,51 @@ } return null; } + + /** + * 鍙戦�丳ost璇锋眰 + * @param path 璇锋眰璺緞 + * @param content 鍙戦�佸唴瀹� + * @param header 璇锋眰澶村弬鏁拌缃� + * @param contentType 璇锋眰绫诲瀷锛岄粯璁pplication/x-www-form-urlencoded + * @param encoding 缂栫爜锛岄粯璁TF-8 + * @return 鏈嶅姟鍣ㄧ鍐呭 + */ + public static String sendPOSTRequest(String path, String content,Map<String,String> header,String contentType, String encoding){ + //byte[] entity = content.toString().getBytes();//鐢熸垚瀹炰綋鏁版嵁 + try{ + if(StringUtils.isNull(contentType)){ + contentType = "application/x-www-form-urlencoded"; + } + if(StringUtils.isNull(encoding)){ + encoding = "UTF-8"; + } + + HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection(); + conn.setConnectTimeout(5000); + conn.setRequestMethod("POST"); + conn.setDoOutput(true);//鍏佽瀵瑰杈撳嚭鏁版嵁 + conn.setDoInput(true); + conn.setUseCaches(false); + conn.setRequestProperty("Charset", encoding); + conn.setRequestProperty("Content-Type",contentType); + if(header != null){ + for(String key:header.keySet()){//keySet鑾峰彇map闆嗗悎key鐨勯泦鍚� 鐒跺悗鍦ㄩ亶鍘唊ey鍗冲彲 + conn.setRequestProperty(key,header.get(key)); + } + } + //conn.setRequestProperty("Content-Length", String.valueOf(entity.length)); + OutputStreamWriter outStream = new OutputStreamWriter(conn.getOutputStream(),"UTF-8"); + outStream.write(content); + outStream.flush(); + if(conn.getResponseCode() == 200){ + return StreamUtils.InputStreamTOString(conn.getInputStream()); + } + }catch(Exception e){ + e.printStackTrace(); + } + return null; + } /** * 鍙戦�丳ost璇锋眰 -- Gitblit v1.8.0