发起回调
public Result PostXml(string url, string xml, string token = null)
{
byte[] bytes = Encoding.UTF8.GetBytes(xml);
var client = new RestClient(url);
RestRequest request = new RestRequest(Method.POST);
request.AddHeader("ContentType", "text/xml");
if (!string.IsNullOrWhiteSpace(token))
{
request.AddHeader("token", token);
}
request.Timeout = 10000;
request.AddJsonBody(bytes.Length);
var response = client.Execute(request);
var ds= JsonConvert.DeserializeObject<Result>(response.Content);
return new Result() {Code=ds.Code };
}
接受回调
public Result PostXml1()
{
Stream TmpBody = _accessor.HttpContext.Request.Body;
string requestData = "";
TmpBody.Position = 0;
using (var streamReader = new StreamReader(TmpBody, Encoding.UTF8))
{
//读取Body的时候,请尽量使用异步方式读取。ASP.NET Core默认是不支持同步读取的,会抛出异常
//解决方法 启用 KestrelServerOptions 中 AllowSynchronousIO
requestData = streamReader.ReadToEndAsync().Result;
TmpBody.Position = 0;
}
return new Result { Code =200,Msg= requestData };
}