SDK可指定requestMethod

1.x
tanghc 6 years ago
parent 4bd3869ed8
commit cb6bdcc22c
  1. 50
      doc/docs/_sidebar.md
  2. 8
      doc/docs/files/10095_SDK开发.md
  3. 2
      sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/config/OpenServiceConfig.java
  4. 19
      sop-sdk/sdk-csharp/SDKCSharp/Client/OpenHttp.cs
  5. 59
      sop-sdk/sdk-csharp/SDKCSharp/Client/OpenRequest.cs
  6. 32
      sop-sdk/sdk-csharp/SDKCSharp/Common/RequestForm.cs
  7. 8
      sop-sdk/sdk-csharp/SDKCSharp/Common/RequestMethod.cs
  8. 4
      sop-sdk/sdk-csharp/SDKCSharp/Program.cs
  9. 11
      sop-sdk/sdk-csharp/SDKCSharp/Request/BaseRequest.cs
  10. 1
      sop-sdk/sdk-csharp/SDKCSharp/Request/GetStoryRequest.cs

@ -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)

@ -75,7 +75,15 @@ public class GetStoryRequest extends BaseRequest<GetStoryResponse> {
return "alipay.story.find";
}
}
```
可重写`getRequestMethod()`方法指定HTTP请求method,默认是POST。
```java
@Override
protected RequestMethod getRequestMethod() {
return RequestMethod.GET;
}
```
### 使用方式

@ -39,7 +39,7 @@ public class OpenServiceConfig extends AlipayServiceConfiguration {
@Override
protected boolean swaggerAccessProtected() {
return false;
return true;
}
}
}

@ -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<string, string> 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
/// <param name="url">URL.</param>
/// <param name="form">Form.</param>
/// <param name="header">Header.</param>
public string PostFormBody(string url, Dictionary<string, string> form, Dictionary<string, string> header)
/// <param name="method">method,默认POST</param>
public string RequestFormBody(string url, Dictionary<string, string> form, Dictionary<string, string> 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<string> 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;
}
}
}

@ -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
/// <param name="header">请求头</param>
/// <returns></returns>
public string Request(string url, RequestForm requestForm, Dictionary<string, string> header)
{
return this.DoPost(url, requestForm, header);
}
public string DoGet(string url, RequestForm requestForm, Dictionary<string, string> header)
{
StringBuilder queryString = new StringBuilder();
Dictionary<string, string> form = requestForm.Form;
Dictionary<string, string>.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<string, string> header)
{
Dictionary<string, string> form = requestForm.Form;
List<UploadFile> 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<string, string> form, Encoding charset)
{
StringBuilder queryString = new StringBuilder();
Dictionary<string, string>.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();

@ -8,33 +8,33 @@ namespace SDKCSharp.Common
{
public class RequestForm
{
private Dictionary<string, string> form;
/// <summary>
/// 请求表单内容
/// </summary>
public Dictionary<string, string> Form
{
get { return form; }
set { form = value; }
}
private List<UploadFile> files;
public Dictionary<string, string> Form { get; set; }
/// <summary>
/// 上传文件
/// </summary>
public List<UploadFile> Files
{
get { return files; }
set { files = value; }
}
public List<UploadFile> Files { get; set; }
/// <summary>
/// 指定或者设置字符集
/// </summary>
/// <value>The charset.</value>
public Encoding Charset { get; set; }
/// <summary>
/// 指定或设置HTTP请求method
/// </summary>
/// <value>The request method.</value>
public RequestMethod RequestMethod { get; set; } = RequestMethod.POST;
public RequestForm(Dictionary<string, string> form)
{
this.form = form;
this.Form = form;
}
}
}

@ -0,0 +1,8 @@
using System;
namespace SDKCSharp.Common
{
public enum RequestMethod
{
GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE
}
}

@ -29,8 +29,8 @@ namespace SDKTest
public static void Main(string[] args)
{
TestGet();
//TestCommon();
//TestUpload();
TestCommon();
TestUpload();
}
// 标准用法

@ -48,6 +48,15 @@ namespace SDKCSharp.Request
return SdkConfig.DEFAULT_VERSION;
}
/// <summary>
/// 指定HTTP请求method,默认POST
/// </summary>
/// <returns>The request method.</returns>
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;
}

@ -1,4 +1,5 @@
using System;
using SDKCSharp.Common;
using SDKCSharp.Response;
namespace SDKCSharp.Request

Loading…
Cancel
Save