发送 XML 字符串作为响应

2024-02-26

我正在从第三方应用程序(不同域)向我的 ASP 应用程序获取请求。我正在处理请求并在我的应用程序中执行业务部分,作为确认,我需要将 XML 字符串作为响应发送到将请求发布到我的应用程序的同一页面。我使用以下代码成功检索了 Request 中的输入

  NameValueCollection postPageCollection = Request.Form;
  foreach (string name in postPageCollection.AllKeys)
    {
        ... = postPageCollection[name]);
    }

但我不确定如何将响应与 XML 字符串一起发送回站点(不同的域)?

EDIT:如何从 POST 发生的位置获取 URL。


你可以得到来自的urlRequest.ServerVariables["HTTP_REFERER"]

对于 XML,这是我使用的 2 个函数

public static string ObjectToXML(Type type, object obby)
{
    XmlSerializer ser = new XmlSerializer(type);
    using (System.IO.MemoryStream stm = new System.IO.MemoryStream())
    {
        //serialize to a memory stream
        ser.Serialize(stm, obby);
        //reset to beginning so we can read it.  
        stm.Position = 0;
        //Convert a string. 
        using (System.IO.StreamReader stmReader = new System.IO.StreamReader(stm))
        {
            string xmlData = stmReader.ReadToEnd();
            return xmlData;
        }
    }
}

public static object XmlToObject(Type type, string xml)
{
    object oOut = null;

    //hydrate based on private string var
    if (xml != null && xml.Length > 0)
    {
        System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(type);

        using (System.IO.StringReader sReader = new System.IO.StringReader(xml))
        {
            oOut = serializer.Deserialize(sReader);

            sReader.Close();
        }
    }

    return oOut;
}

这是我如何使用它的示例

[Serializable]
public class MyClassThatKeepTheData
{
    public int EnaTest;
}

MyClassThatKeepTheData cTheObject = new MyClassThatKeepTheData();

ObjectToXML(typeof(MyClassThatKeepTheData), cTheObject)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

发送 XML 字符串作为响应 的相关文章

随机推荐