mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 21:57:56 +08:00
C#SDK提交
This commit is contained in:
@@ -10,6 +10,7 @@ using SDKCSharp.Common;
|
||||
using SDKCSharp.Request;
|
||||
using SDKCSharp.Response;
|
||||
using SDKCSharp.Utility;
|
||||
using System.IO;
|
||||
|
||||
namespace SDKCSharp.Client
|
||||
{
|
||||
@@ -31,7 +32,6 @@ namespace SDKCSharp.Client
|
||||
private string url;
|
||||
private string appId;
|
||||
private string privateKey;
|
||||
private bool isPriKeyFromFile;
|
||||
|
||||
private OpenConfig openConfig;
|
||||
private OpenRequest openRequest;
|
||||
@@ -52,9 +52,32 @@ namespace SDKCSharp.Client
|
||||
this.url = url;
|
||||
this.appId = appId;
|
||||
this.privateKey = privateKey;
|
||||
this.isPriKeyFromFile = priKeyFromFile;
|
||||
this.openConfig = openConfig;
|
||||
this.openRequest = new OpenRequest(openConfig);
|
||||
// 如果是从文件中加载私钥
|
||||
if (priKeyFromFile)
|
||||
{
|
||||
this.privateKey = LoadCertificateFile(privateKey);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 加载秘钥文件
|
||||
/// </summary>
|
||||
/// <returns>返回私钥内容.</returns>
|
||||
/// <param name="filename">文件路径.</param>
|
||||
private static string LoadCertificateFile(string filename)
|
||||
{
|
||||
if(!File.Exists(filename))
|
||||
{
|
||||
throw new SopException("文件不存在," + filename);
|
||||
}
|
||||
using (FileStream fs = File.OpenRead(filename))
|
||||
{
|
||||
byte[] data = new byte[fs.Length];
|
||||
fs.Read(data, 0, data.Length);
|
||||
return Encoding.UTF8.GetString(data);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -85,7 +108,7 @@ namespace SDKCSharp.Client
|
||||
}
|
||||
form[this.openConfig.AppKeyName] = this.appId;
|
||||
string content = SopSignature.getSignContent(form);
|
||||
string sign = SignUtil.CreateSign(form, privateKey, request.Charset, isPriKeyFromFile, request.SignType);
|
||||
string sign = SignUtil.CreateSign(form, privateKey, request.Charset, request.SignType);
|
||||
form[this.openConfig.SignName] = sign;
|
||||
|
||||
string resp = this.doExecute(url, requestForm, header);
|
||||
|
@@ -11,6 +11,7 @@ using System.Text.RegularExpressions;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
|
||||
using SDKCSharp.Common;
|
||||
using System.Collections.Specialized;
|
||||
|
||||
namespace SDKCSharp.Client
|
||||
{
|
||||
@@ -18,6 +19,7 @@ namespace SDKCSharp.Client
|
||||
{
|
||||
public const string CONTENT_TYPE_JSON = "application/json";
|
||||
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 CookieContainer cookieContainer = new CookieContainer();
|
||||
@@ -40,7 +42,7 @@ namespace SDKCSharp.Client
|
||||
request.CookieContainer = cookieContainer;
|
||||
request.ContinueTimeout = this.openConfig.ConnectTimeoutSeconds * 1000;
|
||||
request.ReadWriteTimeout = this.openConfig.ReadTimeoutSeconds * 1000;
|
||||
bindHeader(request, header);
|
||||
BindHeader(request, header);
|
||||
return request;
|
||||
}
|
||||
|
||||
@@ -76,7 +78,7 @@ namespace SDKCSharp.Client
|
||||
/// <returns></returns>
|
||||
public string PostJsonBody(string url, string json, Dictionary<string, string> header)
|
||||
{
|
||||
var request = CreateWebRequest(url, header);
|
||||
HttpWebRequest request = CreateWebRequest(url, header);
|
||||
request.ContentType = CONTENT_TYPE_JSON;
|
||||
request.Method = METHOD_POST;
|
||||
|
||||
@@ -94,7 +96,29 @@ namespace SDKCSharp.Client
|
||||
|
||||
}
|
||||
|
||||
private void bindHeader(HttpWebRequest request, Dictionary<string, string> header)
|
||||
/// <summary>
|
||||
/// 模拟表单提交
|
||||
/// </summary>
|
||||
/// <returns>返回结果.</returns>
|
||||
/// <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)
|
||||
{
|
||||
WebClient webClient = new WebClient();
|
||||
// 表单参数
|
||||
NameValueCollection postParams = new NameValueCollection();
|
||||
foreach (var item in form)
|
||||
{
|
||||
postParams.Add(item.Key, item.Value);
|
||||
}
|
||||
byte[] byRemoteInfo = webClient.UploadValues(url, METHOD_POST, postParams);
|
||||
return Encoding.UTF8.GetString(byRemoteInfo);
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void BindHeader(HttpWebRequest request, Dictionary<string, string> header)
|
||||
{
|
||||
if (header == null || header.Count == 0)
|
||||
{
|
||||
@@ -164,7 +188,7 @@ namespace SDKCSharp.Client
|
||||
webRequest.Method = METHOD_POST;
|
||||
webRequest.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);
|
||||
webRequest.ContentLength = postBytes.Length;
|
||||
bindHeader(webRequest, header);
|
||||
BindHeader(webRequest, header);
|
||||
if (Regex.IsMatch(url, "^https://"))
|
||||
{
|
||||
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
|
||||
|
@@ -37,10 +37,10 @@ namespace SDKCSharp.Client
|
||||
/// <returns></returns>
|
||||
public string Request(string url, RequestForm requestForm, Dictionary<string, string> header)
|
||||
{
|
||||
return this.doPost(url, requestForm, header);
|
||||
return this.DoPost(url, requestForm, header);
|
||||
}
|
||||
|
||||
public string doGet(string url, RequestForm requestForm, Dictionary<string, string> header)
|
||||
public string DoGet(string url, RequestForm requestForm, Dictionary<string, string> header)
|
||||
{
|
||||
StringBuilder queryString = new StringBuilder();
|
||||
Dictionary<string, string> form = requestForm.Form;
|
||||
@@ -57,7 +57,7 @@ namespace SDKCSharp.Client
|
||||
|
||||
}
|
||||
|
||||
public string doPost(string url, RequestForm requestForm, Dictionary<string, string> header)
|
||||
public string DoPost(string url, RequestForm requestForm, Dictionary<string, string> header)
|
||||
{
|
||||
Dictionary<string, string> form = requestForm.Form;
|
||||
List<UploadFile> files = requestForm.Files;
|
||||
@@ -67,16 +67,12 @@ namespace SDKCSharp.Client
|
||||
}
|
||||
else
|
||||
{
|
||||
return this.openHttp.PostJsonBody(url, JsonUtil.ToJSONString(form), header);
|
||||
return this.openHttp.PostFormBody(url, form, header);
|
||||
}
|
||||
}
|
||||
|
||||
public string PostJsonBody(string url, string json)
|
||||
{
|
||||
return this.openHttp.PostJsonBody(url, json, null);
|
||||
}
|
||||
|
||||
protected string causeException(Exception e)
|
||||
|
||||
protected string CauseException(Exception e)
|
||||
{
|
||||
ErrorResponse result = new ErrorResponse();
|
||||
result.SubCode = HTTP_ERROR_CODE;
|
||||
@@ -86,7 +82,6 @@ namespace SDKCSharp.Client
|
||||
return JsonUtil.ToJSONString(result);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
class ErrorResponse : BaseResponse
|
||||
|
Reference in New Issue
Block a user