.net core或web api 接口发起回调:
[Route("api/[controller]")]
[ApiController]
public class HomeController : ControllerBase
{
[HttpGet]
public string Get()
{
StringBuilder xmlResult = new StringBuilder("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
List<BookInfo> bookList = GetBookList(); //获取图书列表
if (bookList != null && bookList.Count > 0)
{
xmlResult.Append("<bookstore>");
foreach (BookInfo book in bookList)
{
xmlResult.AppendFormat("<book id=\"{0}\" category=\"{1}\">", book.BookId, book.Category);
xmlResult.AppendFormat("<title>{0}</title>", book.Title);
xmlResult.AppendFormat("<author>{0}</author>", book.Author);
xmlResult.AppendFormat("<publishDate>{0}</publishDate>", DateTime.Now.ToString());
xmlResult.AppendFormat("<price>{0}</price>", book.Price);
xmlResult.Append("</book>");
}
xmlResult.Append("</bookstore>");
var f = JsonConvert.SerializeObject(bookList);
PostXml("http://47.93.190.175:8571/api/home/index", xmlResult.ToString()+"########################"+f);
}
return xmlResult.ToString();
}
public void PostXml(string url, string xml)
{
HttpWebRequest request = null;
HttpWebResponse response = null;
try
{
byte[] bytes = Encoding.UTF8.GetBytes(xml);
request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = "Post";
request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36";
request.AllowAutoRedirect = true;
request.KeepAlive = true;//建立持久性连接
//整数据
ASCIIEncoding encoding = new ASCIIEncoding();
request.ContentLength = bytes.Length;
request.ContentType = "text/xml";
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(bytes, 0, bytes.Length);
}
//响应
response = (HttpWebResponse)request.GetResponse();
string text = string.Empty;
using (Stream responseStm = response.GetResponseStream())
{
StreamReader redStm = new StreamReader(responseStm, Encoding.UTF8);
text = redStm.ReadToEnd();
}
}
catch (Exception ex)
{
var msg = ex.Message;
}
//byte[] bytes = Encoding.UTF8.GetBytes(xml);
//HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
//request.Method = "POST";
//request.ContentLength = bytes.Length;
//request.ContentType = "text/xml";
//using (Stream requestStream = request.GetRequestStream())
//{
// requestStream.Write(bytes, 0, bytes.Length);
//}
//HttpWebResponse response = (HttpWebResponse)request.GetResponse();
//if (response.StatusCode != HttpStatusCode.OK)
//{
// string message = String.Format("POST failed. Received HTTP {0}",
// response.StatusCode);
// throw new ApplicationException(message);
//}
}
/// <summary>
/// 获取图书列表
/// </summary>
/// <returns></returns>
public List<BookInfo> GetBookList()
{
List<BookInfo> bookList = new List<BookInfo>();
BookInfo book1 = new BookInfo()
{
BookId = 1,
Category = "CHILDREN",
Title = "Harry Potter",
Author = "J K. Rowling",
PublishDate = new DateTime(2005, 08, 15),
Price = 29.99
};
bookList.Add(book1);
BookInfo book2 = new BookInfo()
{
BookId = 2,
Category = "WEB",
Title = "Learning XML",
Author = "Erik T. Ray",
PublishDate = new DateTime(2003, 10, 18),
Price = 39.95
};
bookList.Add(book2);
return bookList;
}
/// <summary>
/// 图书信息实体类
/// </summary>
public class BookInfo
{
public int BookId { set; get; } //图书ID
public string Title { set; get; } //图书名称
public string Category { set; get; } //图书分类
public string Author { set; get; } //图书作者
public DateTime PublishDate { set; get; } //出版时间
public Double Price { set; get; } //销售价格
}
.net core接收回调参数:
using (var reader = new StreamReader(HttpContext.Request.Body))
{
//var body = await reader.ReadToEndAsync();
//var xmlDoc = XDocument.Parse(body);
//Console.WriteLine(xmlDoc);
string content1 = reader.ReadToEndAsync().Result;
Console.WriteLine(content1);
}
.net 接收回调参数:
Int32 intLen = Convert.ToInt32(Request.InputStream.Length);
byte[] b = new byte[intLen];
Request.InputStream.Read(b, 0, intLen);
string resultFromWx = System.Text.Encoding.UTF8.GetString(b);