返回数据如下:
{
"mainTable": {
"create_time": "2023-01-03",
"RECORD_DATE": "2023-01-03",
"id": "6",
"ASSEMBLY_ITEM": "",
"ASSEMBLY_SN": "0955D"
},
"detail1": [{
"COMPONENT_NUMBER": "1",
"COMPONENT_ITEM": "",
"COMPONENT_TYPE": "III",
"id": "37",
"CORE_PARTS_SN": "***312**"
}, {
"COMPONENT_NUMBER": "2",
"COMPONENT_ITEM": "",
"COMPONENT_TYPE": "主机",
"id": "38",
"CORE_PARTS_SN": "*****1250902****"
}
]
}
代码:
//using Kingdee.BOS.Core.Metadata.Util;
//using Kingdee.BOS.JSON;
//using Kingdee.BOS.Util;
using Kingdee.BOS.JSON;
using Kingdee.BOS.WebApi.Client;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
namespace cux.button.test
{
public class TestHttp
{
public void test()
{
String data= "datajson={\"operationinfo\":{\"operator\":\"1\"},\"mainTable\":{\"ASSEMBLY_SN\":\"DIO:00.02.N1.0955DOM:IDB10-C910-CTYP:BJ4259SNFKB-XJ(RPZ)PD:2023-1-3 18:50:37NO.:1/1\"},\"pageInfo\":{\"pageNo\":\"1\",\"pageSize\":\"10\"},\"header\":{\"systemid\":\"wq\",\"currentDateTime\":\"20240726135047\",\"Md5\":\"746AAF020BE2BBF8D44BFEBD31A4069B\"}}";
String serviceUrl = String.Format("{0}/{1}", "http://IP地址", "api/cube/restful/interface/getDataByPK/getTraceInfoByAssemblySn");
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(serviceUrl);
byte[] buf = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(data);
myRequest.Method = "POST";
myRequest.ContentLength = buf.Length;
//myRequest.ContentType="application/json";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.MaximumAutomaticRedirections = 1;
myRequest.AllowAutoRedirect = true;
//发送请求
Stream stream= myRequest.GetRequestStream();
stream.Write(buf, 0, buf.Length);
stream.Close();
//获取接口返回值
//通过WEB访问对象获取响应内容
HttpWebResponse myResponse= (HttpWebResponse)myRequest.GetResponse();
//通过响应内容流创建StreamReader对象,因为StreamReader更高级更快
StreamReader reader=new StreamReader(myResponse.GetResponseStream(),Encoding.UTF8 );
String returnXml= reader.ReadToEnd(); //利用StreamReader就可以从响应内容从头读到尾
JSONObject jo= JSONObject.Parse( returnXml );
String returnJsonContent = jo["result"].ToString();
jo = JSONObject.Parse(returnJsonContent);
JSONObject mainTableJson= JSONObject.Parse(jo["mainTable"].ToString());
String id= mainTableJson["id"].ToString();
String itemNum = mainTableJson["ASSEMBLY_ITEM"].ToString();
JSONArray detailJArrays = (JSONArray)jo["detail1"];
foreach (JSONObject detail in detailJArrays) {
string componentNumber = detail["COMPONENT_NUMBER"].ToString();
string comSn = detail["CORE_PARTS_SN"].ToString();
Console.WriteLine("COMPONENT_NUMBER: COMPONENT_NUMBER = {0}, Type = {1}", componentNumber, comSn);
}
reader.Close();
myResponse.Close();
}
}
}