使用 C# WebClient 伪造表单提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/726710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fake a form submission with C# WebClient
提问by Brian
I need to call a web and retrieve the resulting data from the model in my asp.net mvc application. When accessed on the web, the form looks like this:
我需要调用 web 并从我的 asp.net mvc 应用程序中的模型中检索结果数据。在 Web 上访问时,表单如下所示:
<form id="textEntryForm" name="textEntryForm" method="post" action="/project/evaluate_to_pdf">
<textarea id="p" rows="20" name="p" cols="132"/><br/>
<input type="button" value="parse" name="do_parse" onclick="new Ajax.Updater('parsedProject','/project/parse',{asynchronous:true,evalScripts:true,on404:function(e){alert('not found!')},parameters:Form.serialize(this.form)});return false"/>
<input type="button" value="evaluate_to_html" name="do_evaluate_to_html" onclick="new Ajax.Updater('parsedProject','/project/evaluate_to_html',{asynchronous:true,evalScripts:true,on404:function(e){alert('not found!')},parameters:Form.serialize(this.form)});return false"/>
<input type="button" value="evaluate" name="do_evaluate" onclick="new Ajax.Updater('parsedProject','/project/evaluate',{asynchronous:true,evalScripts:true,on404:function(e){alert('not found!')},parameters:Form.serialize(this.form)});return false"/>
<input type="button" value="evaluate to pdf source" name="do_evaluate_to_pdf_source" onclick="new Ajax.Updater('parsedProject','/project/evaluate_to_pdf_source',{asynchronous:true,evalScripts:true,on404:function(e){alert('not found!')},parameters:Form.serialize(this.form)});return false"/>
<input type="submit" id="do_evaluate_to_pdf" value="evaluate_to_pdf" name="do_evaluate_to_pdf"/>
</form>
I need to pass the data that would be entered into textarea id="p". How do add that in, using a WebClient to connect?
我需要传递将输入到 textarea id="p" 的数据。如何添加,使用 WebClient 连接?
Thanks!
谢谢!
EditThis isn't for testing purposes, I need to retrieve the data for use in my application.
编辑这不是为了测试目的,我需要检索数据以在我的应用程序中使用。
采纳答案by Matt Grande
回答by eduncan911
You create a Stream and pass it into your HttpWebRequest.
您创建一个 Stream 并将其传递到您的 HttpWebRequest 中。
// Create a request using a URL that can receive a post.
WebRequest request =
WebRequest.Create("http://www.contoso.com/PostAccepter.aspx ");
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
string postData = "p=Some text here from the textarea";
byte[] byteArray = Encoding.UTF8.GetBytes (postData);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream ();
// Write the data to the request stream.
dataStream.Write (byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close ();
// Get the response.
WebResponse response = request.GetResponse ();
// Display the status.
Console.WriteLine (((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream ();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader (dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd ();
// Display the content.
Console.WriteLine (responseFromServer);
// Clean up the streams.
reader.Close ();
dataStream.Close ();
response.Close ();
回答by eduncan911
Another option is the Lightweight Test Automation Framework by Microsoft<- Here Steve Sanderson applies it to MVC.
另一种选择是Microsoft的轻量级测试自动化框架<- 这里 Steve Sanderson 将其应用于 MVC。
(source: codeville.net)
(来源:codeville.net)
(source: codeville.net)
(来源:codeville.net)
回答by wentbackward
These things have a habit of becoming more and more complex, for example should you need to handle cookies, authentication or multipart form uploads for uploading files etc. I suggest using curl (http://sourceforge.net/projects/libcurl-net/)
这些东西有变得越来越复杂的习惯,例如你是否需要处理 cookie、身份验证或多部分表单上传以上传文件等。我建议使用 curl ( http://sourceforge.net/projects/libcurl-net/)
回答by BFree
Something like this:
像这样的东西:
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
string data = "&p=" + dataThatNeedsToBeInTextArea;
byte[] byteArray = Encoding.UTF8.GetBytes (data);
req.ContentLength = byteArray.Length;
Stream stream= req.GetRequestStream ();
stream.Write (byteArray, 0, byteArray.Length);
stream.Close ();
StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
string response = streamIn.ReadToEnd();
streamIn .Close();
回答by Adam Neal
Agreeing with @wentbackward, WatiNis another alternative.
同意@wentbackward,WatiN是另一种选择。