views:

47

answers:

2

Hello All,

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  A: 

Cant you just use the following code:

Request.UrlReferrer.ToString();
Wim Haanstra
I found Request.UrlReferrer is NULL. I post from HTML file saved to my local disc.
Sri Kumar
+2  A: 

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)
Aristos
how to return the response with the XML string?
Sri Kumar
@Sri there are many ways to return the xml, one is to create an .ashx file, and just type it there. Your client just need to ask for this file. Other way is depend from your protocol. If your cliend ask for one page of you (.aspx), you just type the xml inside this page.
Aristos