Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用C#WebClient伪造表单提交_C#_Asp.net Mvc - Fatal编程技术网

使用C#WebClient伪造表单提交

使用C#WebClient伪造表单提交,c#,asp.net-mvc,C#,Asp.net Mvc,我需要在asp.net mvc应用程序中调用web并从模型中检索结果数据。在web上访问时,表单如下所示: <form id="textEntryForm" name="textEntryForm" method="post" action="/project/evaluate_to_pdf"> <textarea id="p" rows="20" name="p" cols="132"/><br/> &l

我需要在asp.net mvc应用程序中调用web并从模型中检索结果数据。在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>
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(); 


我需要将要输入的数据传递到textarea id=“p”。如何使用WebClient进行连接来添加该功能

谢谢


编辑这不是出于测试目的,我需要检索数据以便在我的应用程序中使用。

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

另一个选项是这些东西有一个变得越来越复杂的习惯,例如,如果您需要处理cookie、身份验证或上传文件的多部分表单等。我建议使用curl()

我刚才用过这样的方法:

类似这样的东西:

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

同意是另一种选择。

谢谢Eric。这是如何将数据与它将要查找的“p”id关联起来的?上例中的“string postData=”是您要发送的全部post数据。将postData视为名称/值对的编码查询字符串。由于要传递多个名称/值对,因此必须将它们组合起来。我已经修改了上面的示例。到目前为止还不错,但如何在保持登录的同时发出进一步的请求?这一切都取决于授权机制。例如,如果使用ASP.NET FormAuthentication,则可以检索登录时设置的cookie。然后,对于以后的每次提交,您将始终在WebRequest对象上包含该cookie。如果内存起作用,那么它就是WebRequest上的某种CookieContainer(它本身只是NVP的集合,又称cookie)。如果使用一种形式的querystring会话(例如PHP会话),您需要捕获querystring变量,并将其包含在每个WebRequest中以“保持登录”。我不认为他试图进行测试。我不是——我编辑了这篇文章以反映这一点——很抱歉,您需要从应用程序检索数据,这太过分了。