我想以编程方式将数据发送到网站,然后以编程方式单击“提交”按钮。这是我试图填写的HTML代码:
<textarea id="rpslBox:postRpsl:rpslObject" name="rpslBox:postRpsl:rpslObject" class="ripe-input-field ui-corner-all" rows="18" style="width:500px;"></textarea>
我正在使用这个C#代码:
WebRequest request = WebRequest.Create("https://apps.db.ripe.net/syncupdates/simple-rpsl.html ");
// Set the Method property of the request to POST.
request.Method = "POST";
string textarea = "text";
// Create POST data and convert it to a byte array.
string postData = string.Format("rpslBox:postRpsl:rpslObject{0}", textarea);
当我运行此代码时,它返回页面的HTML代码而不发送此文本。我该如何发送此文字?谢谢你的帮助!
答案 0 :(得分:0)
我无法测试这是否有效,但根据我对msdn的理解,以下代码会将您的postdata转换为bytedata并将其提交给网络服务器:
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 ();