簡體   English   中英

asp.net中的獲取和發布方法

[英]getting and posting method in asp.net

我有一個應用程序,該應用程序使用“ post”方法將一些數據發送到第三方應用程序。然后,第三方應用程序使用我發布的數據運行了一些java,然后為我提供了一個html頁面。是否可以通過編程方式查看該呈現頁面的源代碼可以在其中找到一些數據?

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

重定向到僅觸發帖子的頁面似乎是很多不必要的工作,並阻止您檢查返回的源代碼。

我建議使用WebRequestWebResponse類。 在代碼中,使用要發布的URL和數據創建一個WebRequest。 然后,將其作為POST提交,並獲取響應對象。 然后,您可以檢查響應內容(源代碼),然后再將其寫回客戶端。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM