SDK可指定requestMethod

This commit is contained in:
tanghc
2019-05-28 11:08:50 +08:00
parent 4bd3869ed8
commit cb6bdcc22c
10 changed files with 116 additions and 78 deletions

View File

@@ -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;
}
}
}

View File

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

View File

@@ -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;
}
}
}

View File

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

View File

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

View File

@@ -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;
}

View File

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