将XML字符串作为响应发送

时间:2010-06-01 06:02:01

标签: c# asp.net request cross-domain response

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

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

但我不知道如何将响应和XML String一起发送回网站(不同的域名)?

编辑:如何获取发生POST的网址。

2 个答案:

答案 0 :(得分:2)

您可以获取来自 Request.ServerVariables [“HTTP_REFERER”]

的网址

对于XML,这里有两个我使用的函数

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)

答案 1 :(得分:1)

不能使用以下代码:

Request.UrlReferrer.ToString();