Send XML String as Response

2020-04-02 03:28发布

问题:

I am getting my Request from a third party application(different domain) to my ASP application. I am handling the request and doing the business part in my application and as a acknowledgement I need to send XML string as Response to the same Page which POSTED the request to my Application. I was successful in retrieving the input from Request using the following code

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

But i am not sure how to send back the response along with XML String to the site(different domain)?

EDIT: How to get the URL from where the POST happened.

回答1:

Cant you just use the following code:

Request.UrlReferrer.ToString();


回答2:

You can get the url that come from Request.ServerVariables["HTTP_REFERER"]

For the XML, here are 2 functions that I use

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;
}

And here is an example how I use it

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

MyClassThatKeepTheData cTheObject = new MyClassThatKeepTheData();

ObjectToXML(typeof(MyClassThatKeepTheData), cTheObject)