123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- 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<string>();
- 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<string>()
- {
- "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<UpLoadTraceModel>(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("测试完成");
- }
- }
- }
|