C#HTTP获取数据主要有两种方式 ,这里主要记录Post方式
1.Post获取
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Net.Http; using Newtonsoft; using Newtonsoft.Json.Linq; using Newtonsoft.Json; using System.Windows.Forms; namespace LabelPrinter { /// <summary> /// WebApi帮助类 /// </summary> class WebHelper { public WebHelper() { HttpClientHelper = new HttpClient(); } #region LocalVariables HttpClient HttpClientHelper; static string BaseUrl = "http://10.172.2.171:16041/WebApi/MesCommand?msgFormat=JSON&message="; #endregion OuterJsonData TestOuterJsonClass = new OuterJsonData(); InnerJsonClass TestInnerJsonClass = new InnerJsonClass(); /// <summary> /// 获取Http数据 /// </summary> /// <param name="sn"></param> /// <returns></returns> public string GetSnDataByGet(string SN) { try { //TestInnerJsonClass.CartonId = SN; //string AgrumentStr = JsonConvert.SerializeObject(TestInnerJsonClass); //TestOuterJsonClass.assemblyName = "SMTTrackOut"; //TestOuterJsonClass.className = "SMTTrackOut.GetCartonInfo"; //TestOuterJsonClass.sender = "AUTO"; //TestOuterJsonClass.arguments = AgrumentStr; //string JsonData = JsonConvert.SerializeObject(TestOuterJsonClass); string JsonStringHeader = "{ 'assemblyName':'SMTTrackOut','className':'SMTTrackOut.GetCartonInfo','sender':'AUTO','arguments':{ 'CartonId':'"; string JsonStringTail = "'} }"; var Response1 = HttpClientHelper.GetAsync(BaseUrl + JsonStringHeader+ SN+ JsonStringTail).Result; return Response1.Content.ReadAsStringAsync().Result; } catch(HttpRequestException) { return "RequstErr"; } } /// <summary> /// 分割Json字符串 /// </summary> /// <param name="JsonData"></param> /// <returns></returns> public ParaMeters GetPrintData(string sn,string JsonData) { JObject obj=null; try { obj = JObject.Parse(JsonData); string Result = obj["result"].ToString(); ParaMeters para = new ParaMeters(); para.ProDuct = JObject.Parse(JsonData).SelectToken("ProductId").ToString(); para.SN = sn; para.PN = obj["ProductId"].ToString(); para.VenderLot = DateTime.Now.ToString("yyyy-MM-dd") + DateTime.Now.ToLongTimeString().ToString(); para.ShipQty = obj["LotQty"].ToString(); para.DueDate = DateTime.Now.AddYears(1).ToString(); para.BinCode = ""; para.Location = ""; return para; } catch(Exception er) { MessageBox.Show(er.ToString()); return null; } } } class ParaMeters { /// <summary> /// 字符 /// </summary> public string ProDuct { set; get; } /// <summary> /// SN一维码底部带字符 /// </summary> public string SN { set; get; } /// <summary> /// PN码字符+一维码 /// </summary> public string PN { set; get; } /// <summary> /// 日期时间 /// </summary> public string VenderLot { set; get; } /// <summary> /// 数量字符+一维码 /// </summary> public string ShipQty { set; get; } /// <summary> /// 截止日期(不带时间) /// </summary> public string DueDate { set; get; } /// <summary> /// 一维码 /// </summary> public string BinCode { set; get; } /// <summary> /// 无。只有Location: /// </summary> public string Location { set; get; } } public class OuterJsonData { private string _AssemblyName; public string assemblyName { get { return _AssemblyName; } set { _AssemblyName = value; } } private string _ClassName; public string className { get { return _ClassName; } set { _ClassName = value; } } private string _Sender; public string sender { get { return _Sender; } set { _Sender = value; } } private string _Arguments; public string arguments { get { return _Arguments; } set { _Arguments = value; } } } public class InnerJsonClass { private string _CartonId; public string CartonId { get { return _CartonId; } set { _CartonId = value; } } } }
标签:set,string,para,get,C#,获取数据,using,POST,public From: https://www.cnblogs.com/HomeSapiens/p/17436681.html