158 lines
5.9 KiB
C#
158 lines
5.9 KiB
C#
using Newtonsoft.Json.Linq;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.ComponentModel;
|
||
using System.Data;
|
||
using System.Drawing;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Net;
|
||
using System.Net.Http;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using System.Windows.Forms;
|
||
|
||
namespace HttpTestApp
|
||
{
|
||
public partial class Form1 : Form
|
||
{
|
||
string SIP = "172.16.21.210";//"172.30.8.2";//"172.16.21.210";
|
||
public Form1()
|
||
{
|
||
InitializeComponent();
|
||
}
|
||
|
||
private void button1_Click(object sender, EventArgs e)
|
||
{
|
||
JObject parm = new JObject()
|
||
{
|
||
{"batch",textBox2.Text },
|
||
{"reel",textBox3.Text }
|
||
};
|
||
|
||
//从主机台取缺陷文件名列表和JSON数组
|
||
var obj = getDefectFromBatchReel(parm);
|
||
if (obj.Value<int>("code") != 200)
|
||
throw new Exception(obj.Value<string>("data"));
|
||
//
|
||
var defectInfo = obj.Value<JArray>("data"); //文件名列表(主机台已对文件名排序,这里不需再排序(主机台按各自index进行的排序,比对在缺陷文件名后面)
|
||
if (defectInfo.Count < 1)
|
||
throw new Exception("主机台缺陷文件已不存在!");
|
||
textBox1.Text = "";
|
||
for (int i = 0; i < defectInfo.Count; i++)
|
||
{
|
||
textBox1.AppendText(defectInfo[i].ToString() + "\r\n");
|
||
}
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// date,sn
|
||
/// </summary>
|
||
/// <param name="req"></param>
|
||
/// <returns></returns>
|
||
/// <exception cref="Exception"></exception>
|
||
private JObject getDefectFromBatchReel(JObject req)
|
||
{
|
||
|
||
JObject resp = postSync($"http://{SIP}:10086" + "/api/query_table", req.ToString());
|
||
if (!resp.Value<bool>("success"))//框架库内
|
||
throw new Exception(resp.Value<string>("data"));//框架库内
|
||
|
||
//成功接收返回
|
||
JObject obj = JObject.Parse(resp.Value<string>("data"));
|
||
return obj;
|
||
}
|
||
//HttpClient方式
|
||
private JObject postSync(string url, string json, bool recvResp = true, bool isJson = true)
|
||
{
|
||
JObject resp = new JObject();
|
||
try
|
||
{
|
||
HttpClient http = new HttpClient();
|
||
StringContent dataContent;
|
||
//第一种方式:data是json格式
|
||
if (isJson)
|
||
dataContent = new StringContent(json, System.Text.Encoding.UTF8, "application/json"); // {"PageNum":"1","PageSIze":"20","info":"311011500300661"}
|
||
else
|
||
{
|
||
// 第二种方式:form表单提交 内容post 提交都在StringContent请求body中添加
|
||
string lsUrlEncodeStr = json2Params(JObject.Parse(json));
|
||
dataContent = new StringContent(lsUrlEncodeStr, System.Text.Encoding.UTF8, "application/x-www-form-urlencoded"); //PageNum=1&PageSize=20&info=311011500300661
|
||
}
|
||
|
||
http.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", "token");
|
||
var taskstr = http.PostAsync(url, dataContent).Result.Content.ReadAsStringAsync();
|
||
//API.OutputDebugString("wlq postSync:url=" + url + ";resp=" + taskstr.Result);
|
||
//读取返回数据
|
||
//return taskstr.Result;
|
||
if (recvResp)
|
||
{
|
||
resp.Add("data", taskstr.Result);
|
||
resp.Add("success", true);
|
||
}
|
||
else
|
||
{
|
||
resp.Add("data", "");
|
||
resp.Add("success", true);
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
resp.Add("success", false);
|
||
resp.Add("data", ex.Message);
|
||
}
|
||
|
||
return resp;
|
||
}
|
||
|
||
private string json2Params(JObject json)
|
||
{
|
||
string result = "";
|
||
IEnumerable<JProperty> properties = json.Properties();
|
||
foreach (JProperty item in properties)
|
||
{
|
||
result += item.Name.ToString() + "=" + item.Value.ToString() + "&";
|
||
// item.Name 为 键
|
||
// item.Value 为 值
|
||
}
|
||
|
||
result = result.Substring(0, result.Length - 1);
|
||
//string result1 = WebUtility.UrlEncode(result);//转义字符大写
|
||
////string result2 = HttpUtility.UrlEncode(result);//转义字符小写
|
||
return result;
|
||
}
|
||
|
||
private void button2_Click(object sender, EventArgs e)
|
||
{
|
||
JObject parm = new JObject()
|
||
{
|
||
{"batch",textBox2.Text },
|
||
{"reel",textBox3.Text }
|
||
};
|
||
var obj = getDefectFromBatchReelToExcel(parm);
|
||
var datas = Convert.FromBase64String(obj.Value<string>("data"));
|
||
//var datas = resp.Value<byte[]>("data");
|
||
|
||
string path = $"{DateTime.Now.ToString("yyyyMMddHHmmss")}.xlsx";
|
||
File.WriteAllBytes(path, datas);
|
||
return;
|
||
}
|
||
|
||
|
||
private JObject getDefectFromBatchReelToExcel(JObject req)
|
||
{
|
||
JObject resp = postSync($"http://{SIP}:10086" + "/api/get_defect_from_batch_reel", req.ToString());
|
||
if (!resp.Value<bool>("success"))//框架库内
|
||
throw new Exception(resp.Value<string>("data"));//框架库内
|
||
|
||
//成功接收返回
|
||
JObject obj = JObject.Parse(resp.Value<string>("data"));
|
||
return obj;
|
||
//var bmp = (new MemoryStream(Convert.FromBase64String(resp.Value<string>("data"))));
|
||
//var strDAta = resp.Value<string>("data");
|
||
|
||
}
|
||
}
|
||
}
|