在asp.net中获取和发布方法

时间:2013-05-13 12:45:52

标签: c# 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();
 }

1 个答案:

答案 0 :(得分:0)

重定向到刚刚触发帖子的页面似乎需要做很多不必要的工作,并阻止您检查返回的源代码。

我建议使用WebRequestWebResponse类。在您的代码中,您使用要POST的URL和数据创建WebRequest。然后将其作为POST提交,并获取响应对象。然后,您可以在将响应内容(源代码)写回客户端之前检查它。