C# 提交页面,检查提交的字段名称,检查响应
我需要从web应用程序获取数据。我无法访问数据库或应用程序的源(.net) web应用程序的工作原理是这样的——在字段中输入值,单击submit按钮,与这些字段关联的数据将在模式弹出窗口中返回 我需要以编程的方式执行同样的操作,而不需要实际打开浏览器 我需要知道需要发布的字段的名称和URL。然后存储响应 任何.Net语言都可以C# 提交页面,检查提交的字段名称,检查响应,c#,vb.net,C#,Vb.net,我需要从web应用程序获取数据。我无法访问数据库或应用程序的源(.net) web应用程序的工作原理是这样的——在字段中输入值,单击submit按钮,与这些字段关联的数据将在模式弹出窗口中返回 我需要以编程的方式执行同样的操作,而不需要实际打开浏览器 我需要知道需要发布的字段的名称和URL。然后存储响应 任何.Net语言都可以 你知道怎么做吗?谢谢。我使用这些功能发布页面并获得结果: public static string HttpPost(string url, object[] postD
你知道怎么做吗?谢谢。我使用这些功能发布页面并获得结果:
public static string HttpPost(string url, object[] postData, string saveTo = "")
{
StringBuilder post = new StringBuilder();
for (int i = 0; i < postData.Length; i += 2)
post.Append(string.Format("{0}{1}={2}", i == 0 ? "" : "&", postData[i], postData[i + 1]));
return HttpPost(url, post.ToString(), saveTo);
}
public static string HttpPost(string url, string postData, string saveTo = "")
{
postData = postData.Replace("\r\n", "");
try
{
WebRequest req = WebRequest.Create(url);
byte[] send = Encoding.Default.GetBytes(postData);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
//req.ContentType = "text/xml;charset=\"utf-8\"";
req.ContentLength = send.Length;
Stream sout = req.GetRequestStream();
sout.Write(send, 0, send.Length);
sout.Flush();
sout.Close();
WebResponse res = req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream());
string returnvalue = sr.ReadToEnd();
if (!string.IsNullOrEmpty(saveTo))
File.WriteAllText(saveTo, returnvalue);
//Debug.WriteLine("{0}\n{1}", postData, returnvalue);
return returnvalue;
}
catch (Exception ex)
{
Debug.WriteLine("POST Error on {0}\n {1}", url, ex.Message);
return "";
}
}
公共静态字符串HttpPost(字符串url,对象[]postData,字符串saveTo=”“)
{
StringBuilder post=新建StringBuilder();
对于(int i=0;i
感谢@Macro的详细回复!在大多数情况下,它工作得很好。然而,我仍然有一个问题。在web页面上,它是一个AJAX回发,结果显示在一个模式弹出窗口中。现在,通过您的代码,我得到了整个页面的响应,但是模式弹出窗口是错误的。我为您的函数提供了一个字符串数组,其中包含textboxname、value、dropdownlistname、value等。。。等等,我做错了什么-(@Supars:你没有做错什么,我提供的功能应该和你的一模一样,可能页面有点奇怪,我不知道,对不起:(