提交Web表单并获取C#中的响应数据

我正在尝试编写一个C#应用程序,它可以自动发送用户ID,密码和登录(提交“登录”按钮)到网站(例如http://abc.com )并获取Httprequest中返回的数据。 我怎么做? 我在坚持……感谢您的推荐! :d

我用这些function……
首先,你可以传递一个带有param名称和param值的数组,第二个你可以传递整个字符串。
请记住,如果在没有javascript代码的情况下执行POST,则可以正常工作(它会返回您可以随意解析的HTML页面)…所以请仔细查看网页源代码。

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 ""; } }