2010-06-01 84 views
0

我從第三方應用程序(不同域)獲取我的請求到我的ASP應用程序。我正在處理請求並在我的應用程序中執行業務部分,並且作爲確認,我需要將XML字符串作爲響應發送到將請求發送到我的應用程序的同一頁。我成功地使用下面的代碼發送XML字符串作爲響應

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

從檢索請求中的輸入,但我不知道如何發回具有XML字符串到站點(不同領域)沿迴應?

編輯:如何從發生POST的位置獲取URL。

回答

1

廣東話你只需要使用下面的代碼:

Request.UrlReferrer.ToString(); 
+0

我發現Request.UrlReferrer爲NULL。我從HTML文件發佈保存到我的本地光盤。 – 2010-06-01 09:11:59

2

你可以得到來自Request.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) 
+0

如何使用XML字符串返回響應? – 2010-06-03 11:02:51

+0

@Sri有許多方法可以返回xml,一種是創建一個.ashx文件,然後在那裏輸入它。你的客戶只需要詢問這個文件。其他方式取決於你的協議。如果你的cliend請求你的一頁(.aspx),你只需在這個頁面中輸入xml。 – Aristos 2010-06-03 11:55:16