From cb6bdcc22c446fe5017707ff00c57fa8fc915a73 Mon Sep 17 00:00:00 2001 From: tanghc Date: Tue, 28 May 2019 11:08:50 +0800 Subject: [PATCH] =?UTF-8?q?SDK=E5=8F=AF=E6=8C=87=E5=AE=9ArequestMethod?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/docs/_sidebar.md | 50 ++++++++-------- doc/docs/files/10095_SDK开发.md | 8 +++ .../storyweb/config/OpenServiceConfig.java | 2 +- .../sdk-csharp/SDKCSharp/Client/OpenHttp.cs | 19 ++++-- .../SDKCSharp/Client/OpenRequest.cs | 59 ++++++++++--------- .../SDKCSharp/Common/RequestForm.cs | 32 +++++----- .../SDKCSharp/Common/RequestMethod.cs | 8 +++ sop-sdk/sdk-csharp/SDKCSharp/Program.cs | 4 +- .../SDKCSharp/Request/BaseRequest.cs | 11 ++++ .../SDKCSharp/Request/GetStoryRequest.cs | 1 + 10 files changed, 116 insertions(+), 78 deletions(-) create mode 100644 sop-sdk/sdk-csharp/SDKCSharp/Common/RequestMethod.cs diff --git a/doc/docs/_sidebar.md b/doc/docs/_sidebar.md index 79d8dbd6..4e1a1465 100644 --- a/doc/docs/_sidebar.md +++ b/doc/docs/_sidebar.md @@ -1,27 +1,27 @@ -* [首页](/?t=1558576159211) +* [首页](/?t=1559012807224) * 开发文档 - * [快速体验](files/10010_快速体验.md?t=1558576159212) - * [项目接入到SOP](files/10011_项目接入到SOP.md?t=1558576159229) - * [新增接口](files/10020_新增接口.md?t=1558576159229) - * [业务参数校验](files/10030_业务参数校验.md?t=1558576159229) - * [错误处理](files/10040_错误处理.md?t=1558576159229) - * [编写文档](files/10041_编写文档.md?t=1558576159229) - * [接口交互详解](files/10050_接口交互详解.md?t=1558576159230) - * [easyopen支持](files/10070_easyopen支持.md?t=1558576159230) - * [使用签名校验工具](files/10080_使用签名校验工具.md?t=1558576159230) - * [ISV管理](files/10085_ISV管理.md?t=1558576159230) - * [路由授权](files/10090_路由授权.md?t=1558576159230) - * [接口限流](files/10092_接口限流.md?t=1558576159230) - * [SDK开发](files/10095_SDK开发.md?t=1558576159230) - * [使用SpringCloudGateway](files/10096_使用SpringCloudGateway.md?t=1558576159230) - * [应用授权](files/10097_应用授权.md?t=1558576159230) - * [更改数据节点名称](files/10099_更改数据节点名称.md?t=1558576159230) - * [对接前端](files/10100_对接前端.md?t=1558576159231) - * [自定义过滤器](files/10102_自定义过滤器.md?t=1558576159231) - * [文件上传](files/10104_文件上传.md?t=1558576159231) + * [快速体验](files/10010_快速体验.md?t=1559012807224) + * [项目接入到SOP](files/10011_项目接入到SOP.md?t=1559012807240) + * [新增接口](files/10020_新增接口.md?t=1559012807240) + * [业务参数校验](files/10030_业务参数校验.md?t=1559012807240) + * [错误处理](files/10040_错误处理.md?t=1559012807240) + * [编写文档](files/10041_编写文档.md?t=1559012807240) + * [接口交互详解](files/10050_接口交互详解.md?t=1559012807240) + * [easyopen支持](files/10070_easyopen支持.md?t=1559012807240) + * [使用签名校验工具](files/10080_使用签名校验工具.md?t=1559012807240) + * [ISV管理](files/10085_ISV管理.md?t=1559012807241) + * [路由授权](files/10090_路由授权.md?t=1559012807241) + * [接口限流](files/10092_接口限流.md?t=1559012807241) + * [SDK开发](files/10095_SDK开发.md?t=1559012807241) + * [使用SpringCloudGateway](files/10096_使用SpringCloudGateway.md?t=1559012807241) + * [应用授权](files/10097_应用授权.md?t=1559012807241) + * [更改数据节点名称](files/10099_更改数据节点名称.md?t=1559012807241) + * [对接前端](files/10100_对接前端.md?t=1559012807241) + * [自定义过滤器](files/10102_自定义过滤器.md?t=1559012807241) + * [文件上传](files/10104_文件上传.md?t=1559012807241) * 原理分析 - * [原理分析之@ApiMapping](files/90010_原理分析之@ApiMapping.md?t=1558576159231) - * [原理分析之路由存储](files/90011_原理分析之路由存储.md?t=1558576159231) - * [原理分析之如何路由](files/90012_原理分析之如何路由.md?t=1558576159231) - * [原理分析之文档归纳](files/90013_原理分析之文档归纳.md?t=1558576159231) - * [常见问题](files/90100_常见问题.md?t=1558576159231) + * [原理分析之@ApiMapping](files/90010_原理分析之@ApiMapping.md?t=1559012807242) + * [原理分析之路由存储](files/90011_原理分析之路由存储.md?t=1559012807242) + * [原理分析之如何路由](files/90012_原理分析之如何路由.md?t=1559012807242) + * [原理分析之文档归纳](files/90013_原理分析之文档归纳.md?t=1559012807242) + * [常见问题](files/90100_常见问题.md?t=1559012807242) diff --git a/doc/docs/files/10095_SDK开发.md b/doc/docs/files/10095_SDK开发.md index d537c5c7..740d6b6e 100644 --- a/doc/docs/files/10095_SDK开发.md +++ b/doc/docs/files/10095_SDK开发.md @@ -75,7 +75,15 @@ public class GetStoryRequest extends BaseRequest { return "alipay.story.find"; } } +``` + +可重写`getRequestMethod()`方法指定HTTP请求method,默认是POST。 +```java +@Override +protected RequestMethod getRequestMethod() { + return RequestMethod.GET; +} ``` ### 使用方式 diff --git a/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/config/OpenServiceConfig.java b/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/config/OpenServiceConfig.java index 0c8f1023..c39ba27c 100644 --- a/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/config/OpenServiceConfig.java +++ b/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/config/OpenServiceConfig.java @@ -39,7 +39,7 @@ public class OpenServiceConfig extends AlipayServiceConfiguration { @Override protected boolean swaggerAccessProtected() { - return false; + return true; } } } diff --git a/sop-sdk/sdk-csharp/SDKCSharp/Client/OpenHttp.cs b/sop-sdk/sdk-csharp/SDKCSharp/Client/OpenHttp.cs index f1d4c9cf..bbc544d1 100644 --- a/sop-sdk/sdk-csharp/SDKCSharp/Client/OpenHttp.cs +++ b/sop-sdk/sdk-csharp/SDKCSharp/Client/OpenHttp.cs @@ -21,6 +21,7 @@ namespace SDKCSharp.Client public const string CONTENT_TYPE_STREAM = "application/octet-stream"; public const string CONTENT_TYPE_FORM = "application/x-www-form-urlencoded"; public const string METHOD_POST = "POST"; + public const string METHOD_GET = "GET"; public CookieContainer cookieContainer = new CookieContainer(); @@ -54,6 +55,7 @@ namespace SDKCSharp.Client public string Get(string url, Dictionary header) { var request = CreateWebRequest(url, header); + request.Method = METHOD_GET; var response = (HttpWebResponse)request.GetResponse(); var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd(); return responseString; @@ -103,7 +105,8 @@ namespace SDKCSharp.Client /// URL. /// Form. /// Header. - public string PostFormBody(string url, Dictionary form, Dictionary header) + /// method,默认POST + public string RequestFormBody(string url, Dictionary form, Dictionary header, string method = "POST") { WebClient webClient = new WebClient(); // 表单参数 @@ -112,7 +115,15 @@ namespace SDKCSharp.Client { postParams.Add(item.Key, item.Value); } - byte[] byRemoteInfo = webClient.UploadValues(url, METHOD_POST, postParams); + if (header != null) + { + ICollection keys = header.Keys; + foreach (string key in keys) + { + webClient.Headers.Add(key, header[key]); + } + } + byte[] byRemoteInfo = webClient.UploadValues(url, method, postParams); return Encoding.UTF8.GetString(byRemoteInfo); } @@ -264,9 +275,5 @@ namespace SDKCSharp.Client } } - static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) - { - return true; - } } } diff --git a/sop-sdk/sdk-csharp/SDKCSharp/Client/OpenRequest.cs b/sop-sdk/sdk-csharp/SDKCSharp/Client/OpenRequest.cs index 73a4a441..6c46c823 100644 --- a/sop-sdk/sdk-csharp/SDKCSharp/Client/OpenRequest.cs +++ b/sop-sdk/sdk-csharp/SDKCSharp/Client/OpenRequest.cs @@ -13,10 +13,7 @@ namespace SDKCSharp.Client { public class OpenRequest { - private const string AND = "&"; - private const string EQ = "="; - private const string UTF8 = "UTF-8"; - + private const string HTTP_ERROR_CODE = "-400"; private OpenConfig openConfig; @@ -36,28 +33,6 @@ namespace SDKCSharp.Client /// 请求头 /// public string Request(string url, RequestForm requestForm, Dictionary header) - { - return this.DoPost(url, requestForm, header); - } - - public string DoGet(string url, RequestForm requestForm, Dictionary header) - { - StringBuilder queryString = new StringBuilder(); - Dictionary form = requestForm.Form; - Dictionary.KeyCollection keys = form.Keys; - foreach (string keyName in keys) - { - queryString.Append(AND).Append(keyName).Append(EQ) - .Append(HttpUtility.UrlEncode(form[keyName].ToString(), Encoding.UTF8)); - } - - string requestUrl = url + "?" + queryString.ToString().Substring(1); - - return this.openHttp.Get(requestUrl); - - } - - public string DoPost(string url, RequestForm requestForm, Dictionary header) { Dictionary form = requestForm.Form; List files = requestForm.Files; @@ -67,11 +42,39 @@ namespace SDKCSharp.Client } else { - return this.openHttp.PostFormBody(url, form, header); + RequestMethod requestMethod = requestForm.RequestMethod; + if (requestMethod == RequestMethod.GET) + { + string query = this.BuildGetQueryString(form, requestForm.Charset); + if (!string.IsNullOrEmpty(query)) + { + url = url + "?" + query; + } + return openHttp.Get(url, header); + } + return this.openHttp.RequestFormBody(url, form, header); } } - + public string BuildGetQueryString(Dictionary form, Encoding charset) + { + StringBuilder queryString = new StringBuilder(); + Dictionary.KeyCollection keys = form.Keys; + int i = 0; + foreach (string keyName in keys) + { + if (i++ > 0) + { + queryString.Append("&"); + } + queryString.Append(keyName).Append("=") + .Append(HttpUtility.UrlEncode(form[keyName], charset)); + } + return queryString.ToString(); + } + + + protected string CauseException(Exception e) { ErrorResponse result = new ErrorResponse(); diff --git a/sop-sdk/sdk-csharp/SDKCSharp/Common/RequestForm.cs b/sop-sdk/sdk-csharp/SDKCSharp/Common/RequestForm.cs index 7dadf9b2..43a3ec46 100644 --- a/sop-sdk/sdk-csharp/SDKCSharp/Common/RequestForm.cs +++ b/sop-sdk/sdk-csharp/SDKCSharp/Common/RequestForm.cs @@ -8,33 +8,33 @@ namespace SDKCSharp.Common { public class RequestForm { - - private Dictionary form; /// /// 请求表单内容 /// - public Dictionary Form - { - get { return form; } - set { form = value; } - } - - private List files; + public Dictionary Form { get; set; } /// /// 上传文件 /// - public List Files - { - get { return files; } - set { files = value; } - } + public List Files { get; set; } + + /// + /// 指定或者设置字符集 + /// + /// The charset. + public Encoding Charset { get; set; } + + /// + /// 指定或设置HTTP请求method + /// + /// The request method. + public RequestMethod RequestMethod { get; set; } = RequestMethod.POST; public RequestForm(Dictionary form) { - this.form = form; + this.Form = form; } - + } } diff --git a/sop-sdk/sdk-csharp/SDKCSharp/Common/RequestMethod.cs b/sop-sdk/sdk-csharp/SDKCSharp/Common/RequestMethod.cs new file mode 100644 index 00000000..e9d6f4ad --- /dev/null +++ b/sop-sdk/sdk-csharp/SDKCSharp/Common/RequestMethod.cs @@ -0,0 +1,8 @@ +using System; +namespace SDKCSharp.Common +{ + public enum RequestMethod + { + GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE + } +} diff --git a/sop-sdk/sdk-csharp/SDKCSharp/Program.cs b/sop-sdk/sdk-csharp/SDKCSharp/Program.cs index 6d9af60c..d9ab9680 100644 --- a/sop-sdk/sdk-csharp/SDKCSharp/Program.cs +++ b/sop-sdk/sdk-csharp/SDKCSharp/Program.cs @@ -29,8 +29,8 @@ namespace SDKTest public static void Main(string[] args) { TestGet(); - //TestCommon(); - //TestUpload(); + TestCommon(); + TestUpload(); } // 标准用法 diff --git a/sop-sdk/sdk-csharp/SDKCSharp/Request/BaseRequest.cs b/sop-sdk/sdk-csharp/SDKCSharp/Request/BaseRequest.cs index bde0d217..c246b171 100644 --- a/sop-sdk/sdk-csharp/SDKCSharp/Request/BaseRequest.cs +++ b/sop-sdk/sdk-csharp/SDKCSharp/Request/BaseRequest.cs @@ -48,6 +48,15 @@ namespace SDKCSharp.Request return SdkConfig.DEFAULT_VERSION; } + /// + /// 指定HTTP请求method,默认POST + /// + /// The request method. + public virtual RequestMethod GetRequestMethod() + { + return RequestMethod.POST; + } + public BaseRequest() { this.method = this.GetMethod(); @@ -93,6 +102,8 @@ namespace SDKCSharp.Request dict[openConfig.DataName] = biz_content; RequestForm requestForm = new RequestForm(dict); + requestForm.Charset = this.charset; + requestForm.RequestMethod = GetRequestMethod(); requestForm.Files = this.files; return requestForm; } diff --git a/sop-sdk/sdk-csharp/SDKCSharp/Request/GetStoryRequest.cs b/sop-sdk/sdk-csharp/SDKCSharp/Request/GetStoryRequest.cs index 7233ba1f..fc5896a2 100644 --- a/sop-sdk/sdk-csharp/SDKCSharp/Request/GetStoryRequest.cs +++ b/sop-sdk/sdk-csharp/SDKCSharp/Request/GetStoryRequest.cs @@ -1,4 +1,5 @@ using System; +using SDKCSharp.Common; using SDKCSharp.Response; namespace SDKCSharp.Request