110,545
社区成员
发帖
与我相关
我的任务
分享
{
"data":[ {
"AUFNR": "1000370",
"BWART": "101",
"WERKS": "2000",
"LGORT": "6001",
"BLDAT": "20190522",
"BUDAT": "20190522",
"ZMBLNR": "",
"MENGE": "1",
"CHARG": "10125205",
"LOBM_HSDAT": "20190522",
"ZT03": "0001",
"ZT05": "09",
"LOBM_UDCODE": "",
"LOBM_VFDAT": "",
"ZT01": "02",
"ZT07": "",
"ZT11": "",
"ZT13": "",
"ZCP0010": ""
}
,
{
"AUFNR": "1000370",
"BWART": "101",
"WERKS": "2000",
"LGORT": "6001",
"BLDAT": "20190522",
"BUDAT": "20190522",
"ZMBLNR": "",
"MENGE": "1",
"CHARG": "10125205",
"LOBM_HSDAT": "20190522",
"ZT03": "0001",
"ZT05": "09",
"LOBM_UDCODE": "",
"LOBM_VFDAT": "",
"ZT01": "02",
"ZT07": "",
"ZT11": "",
"ZT13": "",
"ZCP0010": ""
}
]
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace verb
{
public class ProductOrderIn
{
public List<OrderInDate> Indata { set; get; }
}
public class OrderInDate
{
/// <summary>
/// 生产订单号
/// </summary>
private string AUFNR { set; get; }
/// <summary>
/// 移动类型
/// </summary>
private string BWART { set; get; }
/// <summary>
/// 工厂代码
/// </summary>
private string WERKS { set; get; }
/// <summary>
/// 库存地点
/// </summary>
private string LGORT { set; get; }
/// <summary>
/// 凭证日期
/// </summary>
private string BLDAT { set; get; }
/// <summary>
/// 过账日期
/// </summary>
private string BUDAT { set; get; }
/// <summary>
/// 物料凭证
/// </summary>
private string ZMBLNR { set; get; }
/// <summary>
/// 入库数量
/// </summary>
private string MENGE { set; get; }
/// <summary>
/// 批次号
/// </summary>
private string CHARG { set; get; }
/// <summary>
/// 内部特性1
/// </summary>
private string LOBM_HSDAT { set; get; }
/// <summary>
/// 内部特性2
/// </summary>
private string ZT03 { set; get; }
/// <summary>
/// 内部特性3
/// </summary>
private string ZT05 { set; get; }
/// <summary>
/// 内部特性4
/// </summary>
private string LOBM_UDCODE { set; get; }
/// <summary>
/// 内部特性5
/// </summary>
private string LOBM_VFDAT { set; get; }
/// <summary>
/// 内部特性6
/// </summary>
private string ZT01 { set; get; }
/// <summary>
/// 内部特性7
/// </summary>
private string ZT07 { set; get; }
/// <summary>
/// 内部特性8
/// </summary>
private string ZT11 { set; get; }
/// <summary>
/// 内部特性9
/// </summary>
private string ZT13 { set; get; }
/// <summary>
/// 内部特性10
/// </summary>
private string ZCP0010 { set; get; }
}
}
{
"data":[ {
"AUFNR": "1000370",
"BWART": "101",
"WERKS": "2000",
"LGORT": "6001",
"BLDAT": "20190522",
"BUDAT": "20190522",
"ZMBLNR": "",
"MENGE": "1",
"CHARG": "10125205",
"LOBM_HSDAT": "20190522",
"ZT03": "0001",
"ZT05": "09",
"LOBM_UDCODE": "",
"LOBM_VFDAT": "",
"ZT01": "02",
"ZT07": "",
"ZT11": "",
"ZT13": "",
"ZCP0010": ""
}
,
{
"AUFNR": "1000370",
"BWART": "101",
"WERKS": "2000",
"LGORT": "6001",
"BLDAT": "20190522",
"BUDAT": "20190522",
"ZMBLNR": "",
"MENGE": "1",
"CHARG": "10125205",
"LOBM_HSDAT": "20190522",
"ZT03": "0001",
"ZT05": "09",
"LOBM_UDCODE": "",
"LOBM_VFDAT": "",
"ZT01": "02",
"ZT07": "",
"ZT11": "",
"ZT13": "",
"ZCP0010": ""
}
]
}
那就简单点,这个是要求的Json格式,我现在要POST数据的格式就是这样的,我前台怎么整成这个数据发送出去?[/quote]
byte[] data = Encoding.UTF8.GetBytes(buffer.ToString());
using (Stream stream = request.GetRequestStream())
{
if(data != null && data.Length > 0)
stream.Write(data, 0, data.Length);
//if(dataCustom != null && dataCustom.Length > 0)
// stream.Write(dataCustom, 0, dataCustom.Length);
}
这个写的很明显了啊,你想发送什么就发送什么啊Buffer你随意写啊{
"data":[ {
"AUFNR": "1000370",
"BWART": "101",
"WERKS": "2000",
"LGORT": "6001",
"BLDAT": "20190522",
"BUDAT": "20190522",
"ZMBLNR": "",
"MENGE": "1",
"CHARG": "10125205",
"LOBM_HSDAT": "20190522",
"ZT03": "0001",
"ZT05": "09",
"LOBM_UDCODE": "",
"LOBM_VFDAT": "",
"ZT01": "02",
"ZT07": "",
"ZT11": "",
"ZT13": "",
"ZCP0010": ""
}
,
{
"AUFNR": "1000370",
"BWART": "101",
"WERKS": "2000",
"LGORT": "6001",
"BLDAT": "20190522",
"BUDAT": "20190522",
"ZMBLNR": "",
"MENGE": "1",
"CHARG": "10125205",
"LOBM_HSDAT": "20190522",
"ZT03": "0001",
"ZT05": "09",
"LOBM_UDCODE": "",
"LOBM_VFDAT": "",
"ZT01": "02",
"ZT07": "",
"ZT11": "",
"ZT13": "",
"ZCP0010": ""
}
]
}
/// <summary>
/// 创建POST方式的HTTP请求
/// </summary>
/// <param name="url">Post地址</param>
/// <param name="getDataFunc">获得数据回调(循环迭代回调)</param>
/// <param name="parameters">传参</param>
/// <param name="timeout">超时时间(毫秒整数)</param>
/// <param name="header">常用头</param>
/// <param name="headerCustom">自定义头</param>
/// <param name="dataCustom">自定义数据</param>
/// <param name="cookies"></param>
/// <param name="errorHandler">报错回调</param>
/// <returns></returns>
public void CreatePostHttpResponse(string url,
IDictionary<string, string> parameters,
NetErrorHandle errorHandler,
int timeout,
IDictionary<HttpRequestHeader, string> header,
IDictionary<string, string> headerCustom,
//byte[] dataCustom,
CookieCollection cookies,
Action finishCallBack = null
)
{
HttpWebRequest request = null;
try
{
//如果是发送HTTPS请求
if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
{
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
request = WebRequest.Create(url) as HttpWebRequest;
request.ProtocolVersion = HttpVersion.Version11;
}
else
{
request = WebRequest.Create(url) as HttpWebRequest;
}
request.Proxy = null;
request.Method = "POST";
request.ServicePoint.Expect100Continue = false;
//request.ContentType = "image/jpeg";
//设置代理UserAgent和超时
//request.UserAgent = userAgent;
WebHeaderCollection tmpHeader = new WebHeaderCollection();
request.Timeout = timeout;
if (header != null)
{
foreach (var pair in header)
tmpHeader.Add(pair.Key, pair.Value);
}
if (headerCustom != null)
{
foreach (var pair in headerCustom)
tmpHeader.Add(pair.Key, pair.Value);
}
string[] keys = tmpHeader.AllKeys;
for (int i = keys.Length - 1; i >= 0; i--)
{
switch (keys[i])
{
case "Accept":
request.Accept = tmpHeader[i];
tmpHeader.Remove("Accept");
break;
case "Connection":
request.Connection = tmpHeader[i];
tmpHeader.Remove("Connection");
break;
case "Content-Length":
request.ContentLength = long.Parse(tmpHeader[i]);
tmpHeader.Remove("Content-Length");
break;
case "Content-Type":
request.ContentType = tmpHeader[i];
tmpHeader.Remove("Content-Type");
break;
case "Expect":
request.Expect = tmpHeader[i];
tmpHeader.Remove("Expect");
break;
case "Date":
request.Date = System.DateTime.Parse(tmpHeader[i]);
tmpHeader.Remove("Date");
break;
case "Host":
request.Host = tmpHeader[i];
tmpHeader.Remove("Host");
break;
case "If-Modified-Since":
request.IfModifiedSince = System.DateTime.Parse(tmpHeader[i]);
tmpHeader.Remove("If-Modified-Since");
break;
case "Range":
string[] rangeArr = tmpHeader[i].Split('_');
request.AddRange(int.Parse(rangeArr[0]), int.Parse(rangeArr[1]));
tmpHeader.Remove("Range");
break;
case "Referer":
request.Referer = tmpHeader[i];
tmpHeader.Remove("Referer");
break;
case "Transfer-Encoding":
request.TransferEncoding = tmpHeader[i];
tmpHeader.Remove("Transfer-Encoding");
break;
case "User-Agent":
request.UserAgent = tmpHeader[i];
tmpHeader.Remove("User-Agent");
break;
}
}
//request.Headers = tmpHeader;
request.Headers.Add(tmpHeader);
if (cookies != null)
{
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(cookies);
}
//发送POST数据
if (!(parameters == null || parameters.Count == 0))
{
StringBuilder buffer = new StringBuilder();
int i = 0;
foreach (string key in parameters.Keys)
{
if (i > 0)
{
buffer.AppendFormat("&{0}={1}", key, parameters[key]);
}
else
{
buffer.AppendFormat("{0}={1}", key, parameters[key]);
i++;
}
}
byte[] data = Encoding.UTF8.GetBytes(buffer.ToString());
using (Stream stream = request.GetRequestStream())
{
if(data != null && data.Length > 0)
stream.Write(data, 0, data.Length);
//if(dataCustom != null && dataCustom.Length > 0)
// stream.Write(dataCustom, 0, dataCustom.Length);
}
}
HttpWebResponse reponse = null;
reponse = request.GetResponse() as HttpWebResponse;
if (reponse != null)
{
//返回响应头
Dictionary<string, string[]> repHeaderDic = new Dictionary<string, string[]>();
WebHeaderCollection collection = reponse.Headers;
for (int i = 0; i < collection.Count; i++)
{
string key = collection.GetKey(i);
string[] _value = collection.GetValues(key);
repHeaderDic.Add(key, _value);
}
if (repHeaderFunc != null)
{
repHeaderFunc(repHeaderDic);
}
using (System.IO.Stream stream = reponse.GetResponseStream())
{
this.Length = long.Parse(repHeaderDic["Content-Length"][0]);
curIndex = 0;
byte[] buffer = new byte[20480];
bool isAllBack = this.Length <= memeryMaxStore ? true : false;
byte[] memeryData = null;
float curTime = ClockHelper.Instance.GetTimeSinceProjectStart();
float timeOutDuation = (float)timeout / 1000;
if (isAllBack)
{
memeryData = new byte[this.Length];
}
while (isStart && curIndex < this.Length)
{
if (stream.CanRead)
{
int count = stream.Read(buffer, 0, buffer.Length);
if (isAllBack)
{
Array.Copy(buffer, 0, memeryData, curIndex, count);
}
else
{
if (getDataFunc != null)
{
getDataFunc(buffer, count);
}
}
curIndex += count;
}
if (ClockHelper.Instance.GetTimeSinceProjectStart() - curTime >= timeOutDuation)
{
errorHandler("存储数据超时...");
break;
}
Thread.Sleep(0);
}
if (isStart && isAllBack)
{
if (getDataFunc != null)
{
getDataFunc(memeryData, memeryData.Length);
}
如果是表单发送的话,得了解一下rfc协议自己封装一个HTTp的字符串写进流里面发,这个我也写过了
public class SerializeJsonSwitch
{
/// <summary>
/// JSON序列化
/// </summary>
public static string JsonSerializer<T>(T t)
{
var ser = new DataContractJsonSerializer(typeof(T));
var ms = new MemoryStream();
ser.WriteObject(ms, t);
string jsonString = Encoding.UTF8.GetString(ms.ToArray());
ms.Close();
return jsonString;
}
/// <summary>
/// JSON反序列化
/// </summary>
public static T JsonDeserialize<T>(string jsonString)
{
var ser = new DataContractJsonSerializer(typeof(T));
var ms = new MemoryStream(Encoding.Unicode.GetBytes(jsonString));
var obj = (T)ser.ReadObject(ms);
return obj;
}
}