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