You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

194 lines
5.8 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Net;
  5. using System.IO;
  6. namespace PhalApiClientSDK
  7. {
  8. /**
  9. * PhalApi客户端SDK包(JAVA版)
  10. *
  11. * - 以接口查询语言(ASQL)的方式来实现接口请求
  12. * - 出于简明客户端,将全部的类都归于同一个文件,避免过多的加载
  13. *
  14. * <br>使用示例:<br>
  15. ```
  16. * PhalApiClientResponse response = PhalApiClient.create()
  17. * .withHost("http://demo.phalapi.net/")
  18. * .withService("Default.Index")
  19. * .withparamsList("name", "dogstar")
  20. * .withTimeout(3000)
  21. * .request();
  22. *
  23. * Log.v("response ret", response.ret + "");
  24. * Log.v("response data", response.data);
  25. * Log.v("response msg", response.msg);
  26. ```
  27. *
  28. * @package PhalApi\Response
  29. * @license http://www.phalapi.net/license GPL 协议
  30. * @link http://www.phalapi.net/
  31. * @author dogstar <chanzonghuang@gmail.com> 2015-10-16
  32. */
  33. public class PhalApiClient {
  34. protected String host;
  35. protected PhalApiClientFilter filter;
  36. protected PhalApiClientParser parser;
  37. protected String service;
  38. protected int timeoutMs;
  39. protected Dictionary<String, String> paramsList;
  40. /**
  41. * 创建一个接口实例,注意:不是单例模式
  42. * @return PhalApiClient
  43. */
  44. public static PhalApiClient create() {
  45. return new PhalApiClient();
  46. }
  47. protected PhalApiClient() {
  48. this.host = "";
  49. this.parser = new PhalApiClientParserJson();
  50. this.reset();
  51. }
  52. /**
  53. * 设置接口域名
  54. * @param String host
  55. * @return PhalApiClient
  56. */
  57. public PhalApiClient withHost(String host) {
  58. this.host = host;
  59. return this;
  60. }
  61. /**
  62. * 设置过滤器,与服务器的DI().filter对应
  63. * @param PhalApiClientFilter filter 过滤器
  64. * @return PhalApiClient
  65. */
  66. public PhalApiClient withFilter(PhalApiClientFilter filter) {
  67. this.filter = filter;
  68. return this;
  69. }
  70. /**
  71. * 设置结果解析器,仅当不是JSON返回格式时才需要设置
  72. * @param PhalApiClientParser parser 结果解析器
  73. * @return PhalApiClient
  74. */
  75. public PhalApiClient withParser(PhalApiClientParser parser) {
  76. this.parser = parser;
  77. return this;
  78. }
  79. /**
  80. * 重置,将接口服务名称、接口参数、请求超时进行重置,便于重复请求
  81. * @return PhalApiClient
  82. */
  83. public PhalApiClient reset() {
  84. this.service = "";
  85. this.timeoutMs = 3000;
  86. this.paramsList = new Dictionary<String, String>();
  87. return this;
  88. }
  89. /**
  90. * 设置将在调用的接口服务名称,如:Default.Index
  91. * @param String service 接口服务名称
  92. * @return PhalApiClient
  93. */
  94. public PhalApiClient withService(String service) {
  95. this.service = service;
  96. return this;
  97. }
  98. /**
  99. * 设置接口参数,此方法是唯一一个可以多次调用并累加参数的操作
  100. * @param String name 参数名字
  101. * @param String value 值
  102. * @return PhalApiClient
  103. */
  104. public PhalApiClient withParams(String name, String value) {
  105. this.paramsList.Add(name, value);
  106. return this;
  107. }
  108. /**
  109. * 设置超时时间,单位毫秒
  110. * @param int timeoutMS 超时时间,单位毫秒
  111. * @return PhalApiClient
  112. */
  113. public PhalApiClient withTimeout(int timeoutMs) {
  114. this.timeoutMs = timeoutMs;
  115. return this;
  116. }
  117. /**
  118. * 发起接口请求
  119. * @return PhalApiClientResponse
  120. */
  121. public PhalApiClientResponse request() {
  122. String url = this.host;
  123. if (this.service != null && this.service.Length > 0) {
  124. url += "?service=" + this.service;
  125. }
  126. if (this.filter != null) {
  127. this.filter.filter(this.service, this.paramsList);
  128. }
  129. try {
  130. String rs = this.doRequest(url, this.paramsList, this.timeoutMs);
  131. return this.parser.parse(rs);
  132. } catch (Exception ex) {
  133. //return new PhalApiClientResponse(408, new JSONObject(), ex.Message);
  134. return new PhalApiClientResponse(408); //TODO
  135. }
  136. }
  137. protected String doRequest(String requestUrl, Dictionary<String, String> paramsList, int timeoutMs) {
  138. String result = null;
  139. Encoding encoding = Encoding.Default;
  140. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUrl);
  141. request.Method = "post";
  142. request.Accept = "text/html, application/xhtml+xml, */*";
  143. request.ContentType = "application/x-www-form-urlencoded";
  144. String strPostdata = "";
  145. //KeyValuePair<T,K>
  146. foreach (KeyValuePair<String, String> kv in paramsList)
  147. {
  148. strPostdata += "&" + kv.Key + "=" + kv.Value;
  149. }
  150. byte[] buffer = encoding.GetBytes(strPostdata);
  151. request.ContentLength = buffer.Length;
  152. request.GetRequestStream().Write(buffer, 0, buffer.Length);
  153. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  154. using (StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding("utf-8")))
  155. {
  156. return reader.ReadToEnd();
  157. }
  158. }
  159. }
  160. }