2013-05-13 46 views
0

我有一個應用程序,我使用「後」方法發送一些數據到第三方應用程序。然後,第三方應用程序運行一些Java與我發佈的數據,然後它爲我提供了一個我有任何可能編程方式來審查這個呈現的頁面的源代碼來找到它的一些數據?在asp.net中獲取和發佈方法

NameValueCollection data = new NameValueCollection(); 
data.Add("v1", "val1"); 
data.Add("v2", "val2"); 
HttpClass.RedirectAndPOST(this.Page, "http://DestUrl/Default.aspx", data); 



public static void RedirectAndPOST(Page page, string destinationUrl, 
           NameValueCollection data) 
{ 
//Prepare the Posting form 
string strForm = PreparePOSTForm(destinationUrl, data); 
    //Add a literal control the specified page holding 
//the Post Form, this is to submit the Posting form with the request. 
    page.Controls.Add(new LiteralControl(strForm)); 
    } 
    public static void RedirectAndPOST(Page page, string destinationUrl, 
           NameValueCollection data) 
    { 
    //Prepare the Posting form 
    string strForm = PreparePOSTForm(destinationUrl, data); 
    //Add a literal control the specified page holding 
    //the Post Form, this is to submit the Posting form with the request. 
    page.Controls.Add(new LiteralControl(strForm)); 
    } 

private static String PreparePOSTForm(string url, NameValueCollection data) 
    { 
//Set a name for the form 
string formID = "PostForm"; 
//Build the form using the specified data to be posted. 
StringBuilder strForm = new StringBuilder(); 
strForm.Append("<form id=\"" + formID + "\" name=\"" + 
       formID + "\" action=\"" + url + 
       "\" method=\"POST\">"); 

foreach (string key in data) 
{ 
    strForm.Append("<input type=\"hidden\" name=\"" + key + 
        "\" value=\"" + data[key] + "\">"); 
} 

strForm.Append("</form>"); 
//Build the JavaScript which will do the Posting operation. 
StringBuilder strScript = new StringBuilder(); 
strScript.Append("<script language="'javascript'">"); 
strScript.Append("var v" + formID + " = document." + 
       formID + ";"); 
strScript.Append("v" + formID + ".submit();"); 
strScript.Append("</script>"); 
//Return the form and the script concatenated. 
//(The order is important, Form then JavaScript) 
return strForm.ToString() + strScript.ToString(); 
} 

回答

0

重定向到只是觸發後好像很多不必要的工作,並阻止您檢查返回源代碼的頁面。

我會推薦使用WebRequestWebResponse類。在您的代碼中,您使用要發佈的URL和數據創建WebRequest。然後您將其作爲POST提交,並獲取響應對象。然後,您可以在將其寫回客戶端之前檢查響應內容(源代碼)。

相關問題