You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
3.0 KiB
C#

using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
namespace ConsoleApplication
{
internal class Program
{
static string GetTimestamp()
{
long milliseconds = DateTimeOffset.Now.ToUnixTimeMilliseconds();
return milliseconds.ToString();
}
static string GenerateMD5(string input)
{
using (MD5 md5 = MD5.Create())
{
byte[] inputBytes = Encoding.UTF8.GetBytes(input);
byte[] hashBytes = md5.ComputeHash(inputBytes);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hashBytes.Length; i++)
{
sb.Append(hashBytes[i].ToString("x2"));
}
return sb.ToString();
}
}
public static void Main(string[] args)
{
//创建 HttpClient 实例
using (HttpClient client = new HttpClient())
{
// 设置请求的基础地址
client.BaseAddress = new Uri("http://IP/jd/services/sendReceiveCollectionBag");
string timeStamp = GetTimestamp();
// 设置请求头
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("JD-MachineCode", "channel-machine-test-rk-001");
client.DefaultRequestHeaders.Add("JD-Timestamp", timeStamp);
client.DefaultRequestHeaders.Add("JD-Signature", GenerateMD5("channel-machine-test-rk-001" + timeStamp + "08F68F890E91838CE6C5D08347F17F58"));
object parameters = new object();
var jsonContent = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(parameters), Encoding.UTF8, "application/json");
// 选择请求方式
HttpResponseMessage response;
try
{
// 发送 POST 请求
response = client.PostAsync("/api/endpoint", jsonContent).GetAwaiter().GetResult();
// 处理响应
if (response.IsSuccessStatusCode)
{
string responseData = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();;
Console.WriteLine("Response: " + responseData);
}
else
{
Console.WriteLine("Error: " + response.ReasonPhrase);
}
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
}
Thread.Sleep(-1);
}
}
}