|
|
|
@ -6,9 +6,11 @@ using SlnMesnac.RfidUpload.NLog;
|
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using NewLife.Http;
|
|
|
|
|
using TouchSocket.Core;
|
|
|
|
|
using TouchSocket.Rpc;
|
|
|
|
|
using TouchSocket.Sockets;
|
|
|
|
@ -34,43 +36,7 @@ namespace SlnMesnac.RfidUpload.TouchSocket
|
|
|
|
|
private JsonChange jsonChange = JsonChange.Instance;
|
|
|
|
|
private AppConfig appConfig = AppConfig.Instance;
|
|
|
|
|
|
|
|
|
|
private WebApiClientApp() { }
|
|
|
|
|
|
|
|
|
|
private WebApiClientSlim CreateWebApiClient(string url)
|
|
|
|
|
{
|
|
|
|
|
var client = new WebApiClientSlim(new System.Net.Http.HttpClient());
|
|
|
|
|
client.Setup(new TouchSocketConfig()
|
|
|
|
|
.SetRemoteIPHost(ExtractBaseURL(appConfig.localUrl))
|
|
|
|
|
.ConfigurePlugins(a =>
|
|
|
|
|
{
|
|
|
|
|
}));
|
|
|
|
|
return client;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 例:
|
|
|
|
|
/// appConfig.localUrl =http://127.0.0.1:9090/api/uploadCsb
|
|
|
|
|
/// ExtractBaseURL(appConfig.localUrl)= http://127.0.0.1:9090
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="url"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
static string ExtractBaseURL(string url)
|
|
|
|
|
{
|
|
|
|
|
// 使用正则表达式匹配 http:// 或 https:// 开头的 URL 部分,直到第一个斜杠
|
|
|
|
|
string pattern = @"^(https?://[^/]+)";
|
|
|
|
|
Match match = Regex.Match(url, pattern);
|
|
|
|
|
|
|
|
|
|
if (match.Success)
|
|
|
|
|
{
|
|
|
|
|
return match.Value;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public string UploadAsync(int code,string paramStr)
|
|
|
|
|
{
|
|
|
|
|
string result = string.Empty;
|
|
|
|
@ -154,22 +120,26 @@ namespace SlnMesnac.RfidUpload.TouchSocket
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string Post(string url, object postData)
|
|
|
|
|
{
|
|
|
|
|
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
|
|
|
|
|
req.Method = "POST";
|
|
|
|
|
req.ContentType = "application/json";
|
|
|
|
|
req.Timeout = 5000;
|
|
|
|
|
|
|
|
|
|
if (req == null) return string.Empty;
|
|
|
|
|
private HttpClient client = new HttpClient();
|
|
|
|
|
|
|
|
|
|
byte[] data = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(postData));
|
|
|
|
|
using (Stream reqStream = req.GetRequestStream())
|
|
|
|
|
reqStream.Write(data, 0, data.Length);
|
|
|
|
|
using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
|
|
|
|
|
using (Stream stream = resp.GetResponseStream())
|
|
|
|
|
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
|
|
|
|
|
return reader.ReadToEnd();
|
|
|
|
|
public string Post(string url, object postData)
|
|
|
|
|
{
|
|
|
|
|
var res = client.PostJson(url,postData);
|
|
|
|
|
return res;
|
|
|
|
|
//HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
|
|
|
|
|
//req.Method = "POST";
|
|
|
|
|
//req.ContentType = "application/json";
|
|
|
|
|
//req.Timeout = 5000;
|
|
|
|
|
|
|
|
|
|
//if (req == null) return string.Empty;
|
|
|
|
|
|
|
|
|
|
//byte[] data = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(postData));
|
|
|
|
|
//using (Stream reqStream = req.GetRequestStream())
|
|
|
|
|
// reqStream.Write(data, 0, data.Length);
|
|
|
|
|
//using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
|
|
|
|
|
//using (Stream stream = resp.GetResponseStream())
|
|
|
|
|
//using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
|
|
|
|
|
// return reader.ReadToEnd();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|