我想使用预定义的Cookie导航到网站,
向input type="text"
的几个文本添加一些文本,然后使用提交按钮提交表单。
我知道可以做到,但我找不到。
我已经尝试将POST数据发送到页面,但是我必须单击按钮才能执行操作。 这是我的代码:
static String readHtmlPage(string url)
{
//setup some variables
String username = "demo";
String password = "password";
String firstname = "John";
String lastname = "Smith";
//setup some variables end
String result = "";
String strPost = "username=" + username + "&password=" + password + "&firstname=" + firstname + "&lastname=" + lastname;
StreamWriter myWriter = null;
HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);
objRequest.Headers["Cookie"] = "sid=0";
objRequest.Headers["Cookie"] = "username=0";
objRequest.Method = "POST";
objRequest.ContentLength = strPost.Length;
objRequest.ContentType = "application/x-www-form-urlencoded";
try
{
myWriter = new StreamWriter(objRequest.GetRequestStream());
myWriter.Write(strPost);
}
catch (Exception e)
{
return e.Message;
}
finally
{
myWriter.Close();
}
HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
using (StreamReader sr =
new StreamReader(objResponse.GetResponseStream()))
{
result = sr.ReadToEnd();
// Close and clean up the StreamReader
sr.Close();
}
return result;
}
static void Main(string[] args)
{
Console.Write(readHtmlPage("http://www.ggogle.com/"));
}
答案 0 :(得分:0)
我的建议,我过去所做的是:
- 使用Fiddler并使用浏览器点击网站,并像平常一样填写表格
- Fiddler将记录请求/响应,您可以复制发布数据字符串并替换您需要的任何值,并使用HttpWebRequest / HttpWebResponse以编程方式执行POST并获得响应。
post data through httpWebRequest
示例:这是我提交最后一条评论时捕获的帖子数据。
comment=When+you+collect+the+recorded+POST+string+you+can+swap+out+the+key+value+pairs+in+there+before+you+make+the+request.+When+the+OnClick+event+fires+it+will+POST+data+to+the+server%2C+this+is+what+you+need+to+recreate+nothing+with+the+javascript.&fkey=62a7d57a52ee7fa723413a2e1dbe7e71
string postData = string.format("comment={0}&fkey={1}", myCommentString, myFKey);
然后,您可以将此字符串传递给POST所针对的URL,并使用HttpWebRequest重新创建它。
您还需要确保在帖子字符串中对您的值进行URL编码。