使用C# WebClient伪造表单提交

3

我需要在我的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>

我需要传递在textarea id="p"中输入的数据。如何使用WebClient连接添加它?
谢谢!
编辑:这不是为了测试目的,我需要检索数据以在我的应用程序中使用。

当然,您需要从应用程序中检索数据。 - user1228
6个回答


3

我认为他并不是在尝试进行测试。 - Joel Coehoorn
我不是 - 我编辑了帖子以反映这一点 - 对于疏忽感到抱歉。 - Brian

2
您需要创建一个流并将其传递到 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 ();

http://msdn.microsoft.com/en-us/library/debx8sh9.aspx


谢谢Eric。这如何将数据与它将要查找的“p” ID相关联? - Brian
上面例子中的“string postData =”是您要发送的整个POST数据。将postData视为编码的名称/值对QueryString。由于您正在传递多个名称/值对,因此必须将它们组合在一起。我已修改上面的示例。 - eduncan911
目前为止一切都很好,但是我如何在保持登录状态的同时发出进一步的请求呢? - Shimmy Weitzhandler
一切都取决于授权机制。例如,如果使用ASP.NET FormAuthentication,您可以检索在登录时设置的cookie。然后,对于每个提交,您将始终在WebRequest对象上包含该cookie。如果我没记错的话,它是WebRequest上的某种CookieContainer(它本身只是NVP的集合,也称为Cookies)。如果使用一种查询字符串会话形式(例如PHP会话),则需要捕获查询字符串变量并在每个WebRequest中包含它以“保持登录状态”。 - eduncan911

0

类似这样:

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(); 

0

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接