using Newtonsoft.Json; using System; using System.Collections.Generic; using System.IO; using System.Net; using System.Net.Http; using System.Net.NetworkInformation; using System.Net.Sockets; using System.Text; using System.Threading.Tasks; namespace ConsoleApp2 { internal class Program { static Random r=new Random(); static async Task Main(string[] args) { NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); var ipAAddresses= new List(); foreach (NetworkInterface adapter in nics) { IPInterfaceProperties ip = adapter.GetIPProperties(); string MAC = adapter.GetPhysicalAddress().ToString(); //获取单播地址集 UnicastIPAddressInformationCollection ipCollection = ip.UnicastAddresses; foreach (UnicastIPAddressInformation ipadd in ipCollection) { if (ipadd.Address.AddressFamily == AddressFamily.InterNetwork)//判断是否为ipv4 { Console.WriteLine($"Address:{ipadd.Address}"); ipAAddresses.Add(ipadd.Address.ToString()); } } } var client = new HttpClient(); var barcodeList = new List() { "RSH0000190842773", "LYC0000000483099", "LYC0000002511409", "RSH0000190842773", "LYC0000000483099", "LYC0000002511409" }; using var streamWriter = new StreamWriter("1.csv", append: true); foreach (var item in barcodeList) { try { //await streamWriter.WriteLineAsync($"开始get请求:{item}"); var stringBuilder = new StringBuilder(); var barcode = item; stringBuilder.Append(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")); stringBuilder.Append(","); var url = $"http://localhost:8765/v2/process_control?serial={barcode}&serial_type=part_id"; var responseMessage = await client.GetAsync(url); stringBuilder.Append(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")); stringBuilder.Append(","); stringBuilder.Append(barcode); stringBuilder.Append(","); stringBuilder.Append(ipAAddresses[0]); stringBuilder.Append(","); stringBuilder.Append(url); stringBuilder.Append(","); stringBuilder.Append("GET"); stringBuilder.Append(","); if (responseMessage.StatusCode == HttpStatusCode.OK) { var s = await responseMessage.Content.ReadAsStringAsync(); stringBuilder.Append(s); Console.WriteLine(s); } else { Console.WriteLine($"上传失败:状态码{responseMessage.StatusCode},{responseMessage.ReasonPhrase}"); } var string1 = stringBuilder.ToString(); await streamWriter.WriteLineAsync(string1); await streamWriter.FlushAsync(); stringBuilder.Clear(); } catch (HttpRequestException) { Console.WriteLine($"上传失败:超时"); } var stringBuilder2=new StringBuilder(); var stream = new StreamReader("jsconfig1.json", encoding: Encoding.UTF8); var content = await stream.ReadToEndAsync(); var upLadTraceModel=JsonConvert.DeserializeObject(content); upLadTraceModel.serials.part_id = item; var randomDouble= r.NextDouble(); upLadTraceModel.data.insight.results[0].value = (30d+ randomDouble).ToString(); if (DateTime.Now.Hour>=8&& DateTime.Now.Hour<=20) { upLadTraceModel.data.insight.uut_attributes.shift = "D"; } else { upLadTraceModel.data.insight.uut_attributes.shift = "N"; } var s3=JsonConvert.SerializeObject(upLadTraceModel); var stringContent = new StringContent(s3); stringContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); try { stringBuilder2.Append(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")); stringBuilder2.Append(","); stringBuilder2.Append(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")); stringBuilder2.Append(","); stringBuilder2.Append(item); stringBuilder2.Append(","); stringBuilder2.Append(ipAAddresses[0]); stringBuilder2.Append(","); stringBuilder2.Append("http://localhost:8765/v2/logs"); stringBuilder2.Append(","); stringBuilder2.Append("GET"); stringBuilder2.Append(","); var responseMessage = await client.PostAsync("http://localhost:8765/v2/logs", stringContent); if (responseMessage.StatusCode == HttpStatusCode.OK) { var s= await responseMessage.Content.ReadAsStringAsync(); stringBuilder2.Append(s); Console.WriteLine(s); } else { var s = await responseMessage.Content.ReadAsStringAsync(); stringBuilder2.Append(s); Console.WriteLine($"上传失败:状态码{responseMessage.StatusCode}," + $"请求uri:{responseMessage.RequestMessage.RequestUri}"); } var sendData = stringBuilder2.ToString(); await streamWriter.WriteLineAsync(sendData); await streamWriter.FlushAsync(); } catch (HttpRequestException e) { Console.WriteLine(e); } } Console.WriteLine("测试完成"); } } }