| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- using System;
- using System.Collections.Generic;
- using System.Net.Http;
- using System.Threading.Tasks;
- namespace NTMiner {
- public static partial class JsonRequestBinaryResponseRpcRoot {
- /// <summary>
- /// 注意:Response时ReadAsByteArrayAsync后进行二进制反序列化。
- /// </summary>
- /// <typeparam name="TResponse"></typeparam>
- /// <param name="host">用于组装Url</param>
- /// <param name="port">用于组装Url</param>
- /// <param name="controller">用于组装Url</param>
- /// <param name="action">用于组装Url</param>
- /// <param name="query">Url上的查询参数,承载登录名、时间戳、签名</param>
- /// <param name="callback"></param>
- public static void GetAsync<TResponse>(
- string host,
- int port,
- string controller,
- string action,
- Dictionary<string, string> query,
- Action<TResponse, Exception> callback,
- int? timeountMilliseconds = null) {
- Task.Factory.StartNew(() => {
- try {
- using (HttpClient client = RpcRoot.CreateHttpClient()) {
- client.SetTimeout(timeountMilliseconds);
- Task<HttpResponseMessage> getHttpResponse = client.GetAsync(RpcRoot.GetUrl(host, port, controller, action, query));
- if (getHttpResponse.Result.IsSuccessStatusCode) {
- getHttpResponse.Result.Content.ReadAsByteArrayAsync().ContinueWith(t => {
- callback?.Invoke(VirtualRoot.BinarySerializer.Deserialize<TResponse>(t.Result), null);
- });
- }
- else {
- callback?.Invoke(default, new NTMinerException($"{action} http response {getHttpResponse.Result.StatusCode.ToString()} {getHttpResponse.Result.ReasonPhrase}"));
- }
- }
- }
- catch (Exception e) {
- callback?.Invoke(default, e);
- }
- });
- }
- /// <summary>
- /// 注意:Request时PostAsJson,Response时ReadAsByteArrayAsync后进行二进制反序列化。
- /// </summary>
- /// <typeparam name="TResponse"></typeparam>
- /// <param name="host">用于组装Url</param>
- /// <param name="port">用于组装Url</param>
- /// <param name="controller">用于组装Url</param>
- /// <param name="action">用于组装Url</param>
- /// <param name="callback"></param>
- /// <param name="timeountMilliseconds"></param>
- public static void PostAsync<TResponse>(
- string host,
- int port,
- string controller,
- string action,
- Action<TResponse, Exception> callback,
- int timeountMilliseconds = 0) {
- PostAsync(host, port, controller, action, query: null, data: null, callback, timeountMilliseconds);
- }
- /// <summary>
- /// 注意:Request时PostAsJson,Response时ReadAsByteArrayAsync后进行二进制反序列化。
- /// </summary>
- /// <typeparam name="TResponse"></typeparam>
- /// <param name="host">用于组装Url</param>
- /// <param name="port">用于组装Url</param>
- /// <param name="controller">用于组装Url</param>
- /// <param name="action">用于组装Url</param>
- /// <param name="data">post的数据,PostAsJson</param>
- /// <param name="callback"></param>
- /// <param name="timeountMilliseconds"></param>
- public static void PostAsync<TResponse>(
- string host,
- int port,
- string controller,
- string action,
- object data,
- Action<TResponse, Exception> callback,
- int timeountMilliseconds = 0) {
- PostAsync(host, port, controller, action, query: null, data, callback, timeountMilliseconds);
- }
- /// <summary>
- /// 注意:Request时PostAsJson,Response时ReadAsByteArrayAsync后进行二进制反序列化。
- /// </summary>
- /// <typeparam name="TResponse"></typeparam>
- /// <param name="host">用于组装Url</param>
- /// <param name="port">用于组装Url</param>
- /// <param name="controller">用于组装Url</param>
- /// <param name="action">用于组装Url</param>
- /// <param name="signData">用于组装url查询字符串</param>
- /// <param name="data">post的数据,PostAsJson</param>
- /// <param name="callback"></param>
- /// <param name="timeountMilliseconds"></param>
- public static void SignPostAsync<TResponse>(
- string host,
- int port,
- string controller,
- string action,
- object data,
- Action<TResponse, Exception> callback,
- int timeountMilliseconds = 0) {
- PostAsync(host, port, controller, action, query: RpcRoot.RpcUser.GetSignData(data), data, callback, timeountMilliseconds);
- }
- /// <summary>
- /// 注意:Request时PostAsJson,Response时ReadAsByteArrayAsync后进行二进制反序列化。
- /// </summary>
- /// <typeparam name="TResponse">post的data的类型</typeparam>
- /// <param name="host">用于组装Url</param>
- /// <param name="port">用于组装Url</param>
- /// <param name="controller">用于组装Url</param>
- /// <param name="action">用于组装Url</param>
- /// <param name="query">Url上的查询参数,承载登录名、时间戳、签名</param>
- /// <param name="data">post的数据,PostAsJson</param>
- /// <param name="callback"></param>
- /// <param name="timeountMilliseconds"></param>
- public static void PostAsync<TResponse>(
- string host,
- int port,
- string controller,
- string action,
- Dictionary<string, string> query,
- object data,
- Action<TResponse, Exception> callback,
- int timeountMilliseconds = 0) {
- Task.Factory.StartNew(() => {
- try {
- using (HttpClient client = RpcRoot.CreateHttpClient()) {
- client.SetTimeout(timeountMilliseconds);
- Task<HttpResponseMessage> getHttpResponse = client.PostAsJsonAsync(RpcRoot.GetUrl(host, port, controller, action, query), data);
- if (getHttpResponse.Result.IsSuccessStatusCode) {
- getHttpResponse.Result.Content.ReadAsByteArrayAsync().ContinueWith(t => {
- callback?.Invoke(VirtualRoot.BinarySerializer.Deserialize<TResponse>(t.Result), null);
- });
- }
- else {
- callback?.Invoke(default, new NTMinerException($"{action} http response {getHttpResponse.Result.StatusCode.ToString()} {getHttpResponse.Result.ReasonPhrase}"));
- }
- }
- }
- catch (Exception e) {
- callback?.Invoke(default, e);
- }
- });
- }
- }
- }
|