From 73bbc431d29e9620ab55060cdd32f7317c668c3e Mon Sep 17 00:00:00 2001 From: seagull <24163551@qq.com> Date: Thu, 21 Dec 2017 15:57:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DHTTP=E8=B0=83=E7=94=A8?= =?UTF-8?q?=E8=BF=87=E7=A8=8B=E4=B8=AD=E7=9A=84=E4=B8=80=E4=B8=AABUG?= =?UTF-8?q?=E4=BB=A5=E5=8F=8A=E5=A2=9E=E5=8A=A0HTTP=E4=B8=8A=E4=BC=A0?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../luckyclient/publicclass/InvokeMethod.java | 28 ++-- .../remoterinterface/HttpClientHelper.java | 126 ++++++++++++++++-- 2 files changed, 132 insertions(+), 22 deletions(-) diff --git a/src/main/java/luckyclient/publicclass/InvokeMethod.java b/src/main/java/luckyclient/publicclass/InvokeMethod.java index 3f53d65..e02e58a 100644 --- a/src/main/java/luckyclient/publicclass/InvokeMethod.java +++ b/src/main/java/luckyclient/publicclass/InvokeMethod.java @@ -1,5 +1,6 @@ package luckyclient.publicclass; +import java.io.File; import java.lang.reflect.Method; import java.util.HashMap; import java.util.List; @@ -136,22 +137,27 @@ public class InvokeMethod { String tempparam=ptp.getParam().replaceAll(""", "\""); JSONObject json= JSONObject.fromObject(tempparam); params.put(ptp.getParamname().replaceAll(""", "\""), json); + luckyclient.publicclass.LogUtil.APP + .info("模板参数【" + ptp.getParamname() + "】 JSONObject类型参数值:【" + json.toString() + "】"); }else if(ptp.getParamtype()==2){ String tempparam=ptp.getParam().replaceAll(""", "\""); JSONArray jarr=JSONArray.fromObject(tempparam); params.put(ptp.getParamname().replaceAll(""", "\""), jarr); + luckyclient.publicclass.LogUtil.APP + .info("模板参数【" + ptp.getParamname() + "】 JSONArray类型参数值:【" + jarr.toString() + "】"); + }else if(ptp.getParamtype()==3){ + String tempparam=ptp.getParam().replaceAll(""", "\""); + File file=new File(tempparam); + params.put(ptp.getParamname().replaceAll(""", "\""), file); + luckyclient.publicclass.LogUtil.APP + .info("模板参数【" + ptp.getParamname() + "】 File类型参数值:【" + file.getAbsolutePath() + "】"); }else{ params.put(ptp.getParamname().replaceAll(""", "\""), ptp.getParam().replaceAll(""", "\"")); + luckyclient.publicclass.LogUtil.APP + .info("模板参数【" + ptp.getParamname() + "】 String类型参数值:【" + ptp.getParam().replaceAll(""", "\"") + "】"); } } - for (ProjectTemplateParams ptp : paramslist) { - if(ptp.getParam().indexOf("***[")>-1&&"***[".equals(ptp.getParam().substring(0, 4))){ - ptp.setParam(ptp.getParam().substring(3)); - } - params.put(ptp.getParamname().replaceAll(""", "\""), ptp.getParam().replaceAll(""", "\"")); - } - if (functionname.toLowerCase().equals("httpurlpost")) { result = HttpClientHelper.sendHttpURLPost(packagename, params, ppt.getContentencoding().toLowerCase(),ppt.getConnecttimeout(),headmsg); @@ -171,6 +177,9 @@ public class InvokeMethod { } else if (functionname.toLowerCase().equals("httpclientpost")) { result = HttpClientHelper.httpClientPost(packagename, params, ppt.getContentencoding().toLowerCase(),headmsg); + } else if (functionname.toLowerCase().equals("httpclientuploadfile")) { + result = HttpClientHelper.httpClientUploadFile(packagename, params, + ppt.getContentencoding().toLowerCase(),headmsg); } else if (functionname.toLowerCase().equals("httpclientpostjson")) { result = HttpClientHelper.httpClientPostJson(packagename, params, ppt.getContentencoding().toLowerCase(),headmsg); @@ -269,6 +278,10 @@ public class InvokeMethod { String tempparam=ptp.getParam().replaceAll(""", "\""); JSONArray jarr=JSONArray.fromObject(tempparam); params.put(ptp.getParamname().replaceAll(""", "\""), jarr); + }else if(ptp.getParamtype()==3){ + String tempparam=ptp.getParam().replaceAll(""", "\""); + File file=new File(tempparam); + params.put(ptp.getParamname().replaceAll(""", "\""), file); }else{ params.put(ptp.getParamname().replaceAll(""", "\""), ptp.getParam().replaceAll(""", "\"")); } @@ -337,7 +350,6 @@ public class InvokeMethod { public static void main(String[] args) { // TODO Auto-generated method stub - } } diff --git a/src/main/java/luckyclient/publicclass/remoterinterface/HttpClientHelper.java b/src/main/java/luckyclient/publicclass/remoterinterface/HttpClientHelper.java index d7b77aa..1d94a44 100644 --- a/src/main/java/luckyclient/publicclass/remoterinterface/HttpClientHelper.java +++ b/src/main/java/luckyclient/publicclass/remoterinterface/HttpClientHelper.java @@ -1,6 +1,7 @@ package luckyclient.publicclass.remoterinterface; import java.io.BufferedReader; +import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; @@ -12,14 +13,15 @@ import java.net.Socket; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; +import java.nio.charset.Charset; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import javax.xml.bind.DatatypeConverter; +import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; @@ -28,6 +30,7 @@ import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPut; import org.apache.http.entity.StringEntity; +import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; @@ -103,6 +106,8 @@ public class HttpClientHelper { while ((temp = br.readLine()) != null) { resultBuffer.append(temp); } + }else{ + resultBuffer.append("Content-Length=0 响应码:"+con.getResponseCode()); } } catch (Exception e) { luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e); @@ -205,6 +210,8 @@ public class HttpClientHelper { while ((temp = br.readLine()) != null) { resultBuffer.append(temp); } + }else{ + resultBuffer.append("Content-Length=0"); } } catch (Exception e) { luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e); @@ -367,6 +374,9 @@ public class HttpClientHelper { while ((temp = br.readLine()) != null) { resultBuffer.append(temp); } + if(resultBuffer.length()==0){ + resultBuffer.append("读取服务器响应数据异常!响应码:"+con.getResponseCode()); + } } catch (Exception e) { luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e); throw new RuntimeException(e); @@ -442,6 +452,9 @@ public class HttpClientHelper { while ((temp = br.readLine()) != null) { resultBuffer.append(temp); } + if(resultBuffer.length()==0){ + resultBuffer.append("读取服务器响应数据异常!"); + } } catch (Exception e) { luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e); throw new RuntimeException(e); @@ -493,14 +506,21 @@ public class HttpClientHelper { } CloseableHttpResponse response = httpclient.execute(httpPost); + //获取请求对象中的响应行对象 + org.apache.http.StatusLine statusLine = response.getStatusLine(); + //从状态行中获取状态码 + String responsecode = String.valueOf(statusLine.getStatusCode()); + // 读取服务器响应数据 + resultBuffer = new StringBuffer(); - // 读取服务器响应数据 - resultBuffer = new StringBuffer(); - br = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), charset)); - String temp; - while ((temp = br.readLine()) != null) { - resultBuffer.append(temp); - } + br = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), charset)); + String temp; + while ((temp = br.readLine()) != null) { + resultBuffer.append(temp); + } + if(resultBuffer.length()==0){ + resultBuffer.append("读取服务器响应数据异常,响应码:"+responsecode); + } } catch (Exception e) { luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e); throw new RuntimeException(e); @@ -553,6 +573,10 @@ public class HttpClientHelper { } CloseableHttpResponse response = httpclient.execute(httpPost); + //获取请求对象中的响应行对象 + org.apache.http.StatusLine statusLine = response.getStatusLine(); + //从状态行中获取状态码 + String responsecode = String.valueOf(statusLine.getStatusCode()); // 读取服务器响应数据 resultBuffer = new StringBuffer(); @@ -561,6 +585,9 @@ public class HttpClientHelper { while ((temp = br.readLine()) != null) { resultBuffer.append(temp); } + if(resultBuffer.length()==0){ + resultBuffer.append("读取服务器响应数据异常,响应码:"+responsecode); + } } catch (Exception e) { luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e); throw new RuntimeException(e); @@ -579,13 +606,87 @@ public class HttpClientHelper { return resultBuffer.toString(); } + /** + * @Description:使用HttpClient上传文件 + */ + public static String httpClientUploadFile(String urlParam, Map params, String charset,Map headmsg) { + StringBuffer resultBuffer = null; + CloseableHttpClient httpclient = HttpClients.createDefault(); + HttpPost httpPost = new HttpPost(urlParam); + //替换头域信息 + for (Map.Entry m :headmsg.entrySet()) { + String key=m.getKey(); + String value=m.getValue(); + luckyclient.publicclass.LogUtil.APP.info("开始设置|替换httpClientUploadFile头域信息...key:【"+key+"】 value:【"+value+"】"); + if(null!=value&&value.indexOf("Base64(")==0){ + String valuesub=value.substring(value.indexOf("Base64(")+7,value.lastIndexOf(")")); + value="Basic " + DatatypeConverter.printBase64Binary((valuesub).getBytes()); + luckyclient.publicclass.LogUtil.APP.info("将头域【"+key+"】的值【"+value+"】FORMAT成BASE64格式..."); + httpPost.setHeader(key, value); + }else{ + httpPost.setHeader(key, value); + } + } + // 构建请求参数 + BufferedReader br = null; + try { + if(params.size()>0){ + //拼接参数 + MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create(); + //设置请求的编码格式 + entityBuilder.setCharset(Charset.forName(charset)); + + for (Map.Entry m :params.entrySet()) { + if (m.getValue() instanceof File) { + entityBuilder.addBinaryBody(m.getKey(), (File)m.getValue()); + luckyclient.publicclass.LogUtil.APP.info("设置httpClientUploadFile 上传文件参数信息...key:【"+m.getKey()+"】 value:【"+m.getValue()+"】"); + }else{ + entityBuilder.addTextBody(m.getKey(), m.getValue().toString()); + luckyclient.publicclass.LogUtil.APP.info("设置httpClientUploadFile参数信息...key:【"+m.getKey()+"】 value:【"+m.getValue()+"】"); + } + } + HttpEntity reqEntity =entityBuilder.build(); + httpPost.setEntity(reqEntity); + } + + CloseableHttpResponse response = httpclient.execute(httpPost); + //获取请求对象中的响应行对象 + org.apache.http.StatusLine statusLine = response.getStatusLine(); + //从状态行中获取状态码 + String responsecode = String.valueOf(statusLine.getStatusCode()); + // 读取服务器响应数据 + resultBuffer = new StringBuffer(); + + br = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), charset)); + String temp; + while ((temp = br.readLine()) != null) { + resultBuffer.append(temp); + } + if(resultBuffer.length()==0){ + resultBuffer.append("读取服务器响应数据异常,响应码:"+responsecode); + } + } catch (Exception e) { + luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e); + throw new RuntimeException(e); + } finally { + if (br != null) { + try { + br.close(); + } catch (IOException e) { + luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e); + br = null; + throw new RuntimeException(e); + } + } + } + return resultBuffer.toString(); + } + /** * @Description:使用HttpClient发送get请求 */ public static String httpClientGet(String urlParam, Map params, String charset,Map headmsg) { StringBuffer resultBuffer = null; - //@SuppressWarnings({ "deprecation", "resource" }) - //HttpClient client = new HttpClient(); CloseableHttpClient httpclient = HttpClients.createDefault(); BufferedReader br = null; // 构建请求参数 @@ -1142,12 +1243,9 @@ public class HttpClientHelper { } return responsecode + resultBuffer.toString(); } + public static void main(String[] args) { // TODO Auto-generated method stub - Map params = new HashMap(0); - Map headmsg = new HashMap(0); - String a=httpClientPut("http://tmsclub.gugud.com/aggregation/courses/48/start", params, "UTF-8",headmsg); - System.out.println(a); } }