模拟提交的两种用法
1、HttpWebRequest
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://fanyi.baidu.com/transcontent"); Encoding encoding = Encoding.UTF8; string param = "ie=utf-8&source=txt&query=hello&t=1327829764203&token=8a7dcbacb3ed72cad9f3fb079809a127&from=auto&to=auto"; //encoding.GetBytes(postData); byte[] bs = Encoding.ASCII.GetBytes(param); string responseData = String.Empty; req.Method = "POST"; req.ContentType = "application/x-www-form-urlencoded"; req.ContentLength = bs.Length; using (Stream reqStream = req.GetRequestStream()) { reqStream.Write(bs, 0, bs.Length); reqStream.Close(); } using (HttpWebResponse response = (HttpWebResponse)req.GetResponse()) { using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding)) { responseData = reader.ReadToEnd(); } Response.Write(responseData); }
2、Webbrowser
private void Form1_Load(object sender, EventArgs e) { this.webBrowser1.Url = new Uri("http://www.baidu.com");//指定url地址为百度首页 } private void button1_Click(object sender, EventArgs e) { HtmlElement searchWords = webBrowser1.Document.All["kw"];//获取百度搜索的文本框 HtmlElement searchButton = webBrowser1.Document.All["su"];//获取百度搜索的按钮 searchWords.SetAttribute("value", "guwei4037");//给百度搜索的文本框赋值 searchButton.InvokeMember("click");//调用百度搜索按钮的点击事件 }