PhalApiClient.cs 5.8 KB

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