Http 远程调用

冷不防 2022-08-24 06:15 269阅读 0赞

Http 远程调用

1.HttpResquestProxy.java类。

  1. package com.cassandra.http.common;
  2. import java.io.UnsupportedEncodingException;
  3. import java.net.URLEncoder;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. import net.sf.json.JSONObject;
  7. import org.apache.commons.httpclient.HttpClient;
  8. import org.apache.commons.httpclient.methods.PostMethod;
  9. import org.apache.commons.lang.StringUtils;
  10. import org.apache.commons.logging.Log;
  11. import org.apache.commons.logging.LogFactory;
  12. import com.cassandra.http.bean.Response;
  13. public class HttpRequestProxy
  14. {
  15. private Log logger = LogFactory.getLog(HttpRequestProxy.class);
  16. private String connectionUrl;
  17. private Map<String,String> paramMap = new HashMap<String,String>();
  18. private Map<String, String> headMap = new HashMap<String,String>();
  19. private HttpClient httpClient;
  20. public HttpRequestProxy()
  21. {
  22. httpClient = new HttpClient();
  23. httpClient.getParams().setContentCharset("utf-8");
  24. }
  25. public HttpRequestProxy(String connectionUrl) {
  26. httpClient = new HttpClient();
  27. setConnectionUrl(connectionUrl);
  28. }
  29. public void clearHttpRequest(){
  30. paramMap.clear();
  31. headMap.clear();
  32. }
  33. public void appendParam(String key, String value) {
  34. try {
  35. if (StringUtils.isEmpty(value)) {
  36. return;
  37. }
  38. paramMap.put(key, URLEncoder.encode(value, "utf-8"));
  39. } catch (UnsupportedEncodingException e) {
  40. // TODO Auto-generated catch block
  41. e.printStackTrace();
  42. }
  43. }
  44. public void appendParam(Map<String, String> params) {
  45. if ((params == null) || (params.isEmpty())) {
  46. return;
  47. }
  48. for (Map.Entry entry : params.entrySet()) {
  49. paramMap.put((String)entry.getKey(),(String)entry.getValue());
  50. }
  51. }
  52. public void setHead(Map<String, String> params) {
  53. this.headMap.putAll(params);
  54. }
  55. private String getConnectionUrl() {
  56. return this.connectionUrl;
  57. }
  58. public void setConnectionUrl(String connectionUrl) {
  59. this.connectionUrl = connectionUrl;
  60. }
  61. public String sendRequest()
  62. {
  63. if (StringUtils.isBlank(getConnectionUrl())) {
  64. Response res = new Response();
  65. res.setStatus(-1);
  66. res.setMessage("用户中心地址异常!");
  67. return JSONObject.fromObject(res).toString();
  68. }
  69. logger.debug("sendRequest start!");
  70. try {
  71. PostMethod postMethod = new PostMethod(getConnectionUrl());
  72. if (!(this.headMap.isEmpty())) {
  73. for (Map.Entry head : this.headMap.entrySet()) {
  74. postMethod.addRequestHeader((String)head.getKey(), (String)head.getValue());
  75. }
  76. }
  77. if (!(this.paramMap.isEmpty())) {
  78. for (Map.Entry head : this.paramMap.entrySet()) {
  79. postMethod.addParameter((String)head.getKey(), (String)head.getValue());
  80. }
  81. }
  82. httpClient.executeMethod(postMethod);
  83. int code = postMethod.getStatusCode();
  84. if (code != 200) {
  85. logger.debug("$$$$$$$$$$$$$$ code" + code);
  86. throw new Exception();
  87. }
  88. logger.debug("$$$$$$$$$$$$$$ code" + code);
  89. String result = postMethod.getResponseBodyAsString();
  90. postMethod = null;
  91. /* InputStream in = postMethod.getResponseBodyAsStream();
  92. BufferedReader breader = new BufferedReader(
  93. new InputStreamReader(in, "gbk"));
  94. StringBuffer strResult = new StringBuffer();
  95. String strLine = breader.readLine();
  96. while(strLine != null){
  97. strResult.append(strLine);
  98. strResult.append("\n");
  99. strLine = breader.readLine();
  100. }
  101. */
  102. logger.debug("sendRequest end!");
  103. logger.info("请求结束,返回result= "+result);
  104. return result;
  105. } catch (Exception e) {
  106. logger.debug("$$$$$$$$$$$$$$$$$$$ e" + e.getMessage());
  107. Response res = new Response();
  108. res.setStatus(-1);
  109. res.setMessage("网络异常!");
  110. return JSONObject.fromObject(res).toString();
  111. } finally {
  112. }
  113. }
  114. }

2.方法调用示例

  1. public JSONObject remarkEdit(String memberId, String comment, String token){
  2. logger.info("HttpRequestUtils.remarkEdit() start");
  3. long startTime = System.currentTimeMillis();
  4. JSONObject jsonResult = new JSONObject();
  5. HttpRequestProxy httpProxy = new HttpRequestProxy(ENT_ACCOUNT_CENTER_URL);
  6. JSONObject json = new JSONObject();
  7. json.put("memberId", memberId);
  8. json.put("comment", comment);
  9. httpProxy.appendParam(SERVICE_KEY,UPDATE_ENTERPRISE_MEMBER);
  10. httpProxy.appendParam(PARAMS_KEY,JSONConverter.convertToString(json));
  11. httpProxy.appendParam(TOKEN, token);
  12. String result = httpProxy.sendRequest();
  13. jsonResult = JSONObject.fromObject(result);
  14. long costTime = System.currentTimeMillis() - startTime;
  15. logger.info("HttpRequestUtils.remarkEdit() cost time:"+ costTime);
  16. logger.info("HttpRequestUtils.remarkEdit() end");
  17. return jsonResult;
  18. }

发表评论

表情:
评论列表 (有 0 条评论,269人围观)

还没有评论,来说两句吧...

相关阅读

    相关 远程过程调用

    简介 RPC是指远程过程调用,也就是说两台服务器A,B,一个应用部署在A服务器上,想要调用B服务器上应用提供的函数/方法,由于不在一个内存空间,不能直接调用,需要通过网络