b5999b4cf7437f1fcbb178282250353d79a27b29.svn-base 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package cn.com.goldenwater.dcproj.utils;
  2. import cn.com.goldenwater.dcproj.constValue.CommonLabel;
  3. import net.sf.json.JSONObject;
  4. import org.apache.http.HttpResponse;
  5. import org.apache.http.client.methods.HttpPost;
  6. import org.apache.http.entity.StringEntity;
  7. import org.apache.http.impl.client.CloseableHttpClient;
  8. import org.apache.http.impl.client.HttpClients;
  9. import org.apache.http.util.EntityUtils;
  10. import javax.servlet.http.HttpServletRequest;
  11. import java.io.BufferedReader;
  12. import java.io.IOException;
  13. import java.io.InputStreamReader;
  14. import java.net.URI;
  15. import java.net.URL;
  16. import java.net.URLConnection;
  17. import java.nio.charset.Charset;
  18. public class HttpUtil {
  19. public String getHttpResponse(String allConfigUrl) {
  20. BufferedReader in = null;
  21. StringBuffer result = new StringBuffer();
  22. try {
  23. // url请求中如果有中文,要在接收方用相应字符转码
  24. URI uri = new URI(allConfigUrl);
  25. URL url = uri.toURL();
  26. URLConnection connection = url.openConnection();
  27. connection.setRequestProperty("Content-type", "text/html");
  28. connection.setRequestProperty("Accept-Charset", "utf-8");
  29. connection.setRequestProperty("contentType", "utf-8");
  30. connection.setConnectTimeout(300000);
  31. connection.connect();
  32. // 读取URL的响应
  33. in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
  34. String line;
  35. while ((line = in.readLine()) != null) {
  36. result.append(line);
  37. }
  38. return result.toString();
  39. } catch (Exception e) {
  40. e.printStackTrace();
  41. } finally {
  42. try {
  43. if (in != null) {
  44. in.close();
  45. }
  46. } catch (Exception e2) {
  47. e2.printStackTrace();
  48. }
  49. }
  50. return result.toString();
  51. }
  52. public static String resultBody(String url, Object params, HttpServletRequest request){
  53. String content = "";
  54. CloseableHttpClient httpCilent = HttpClients.createDefault();
  55. HttpPost httpPost = new HttpPost(url);
  56. JSONObject jsonObject = JSONObject.fromObject(params);
  57. try {
  58. httpPost.addHeader("Content-type", "application/json; charset=utf-8");
  59. httpPost.setHeader("Accept", "application/json");
  60. httpPost.setHeader(CommonLabel.ORGId,request.getHeader(CommonLabel.ORGId));
  61. httpPost.setHeader(CommonLabel.PERSID,request.getHeader(CommonLabel.PERSID));
  62. httpPost.setHeader(CommonLabel.ACCESS_TOKEN,request.getHeader(CommonLabel.ACCESS_TOKEN));
  63. httpPost.setEntity(new StringEntity(jsonObject.toString(), Charset.forName("UTF-8")));
  64. HttpResponse httpResponse = httpCilent.execute(httpPost);
  65. if (httpResponse.getStatusLine().getStatusCode() == 200) {
  66. content = EntityUtils.toString(httpResponse.getEntity());
  67. }
  68. } catch (IOException e) {
  69. e.printStackTrace();
  70. } finally {
  71. try {
  72. httpCilent.close();//释放资源
  73. } catch (IOException e) {
  74. e.printStackTrace();
  75. }
  76. }
  77. return content;
  78. }
  79. }