从.NET应用程序张贴形式 [英] POST a form from a .NET Application

查看:118
本文介绍了从.NET应用程序张贴形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不熟悉HTTP的东西,但我将如何能够将数据提交到一个网站?有一个提交按钮,我想新闻,从一个控制台应用程序。这是不是我自己的网站

I'm not familiar with http stuff, but how would I be able to submit data to a website? There is a submit button that I would like to "press" from a console app. This is not my own website.

这是网页源代码的一部分,不知道是否有任何关联:

This is part of the page source, not sure if it has any relevance:

<form action="rate.php" method="post">



我看着HttpWebRequest类,但我不熟悉什么样的属性,我需要填写

I looked at the HttpWebRequest class but I am unfamiliar with what properties I need to fill in.

对不起,我那么的模糊,但我不熟悉HTTP。

Sorry I'm so vague but I'm not familiar with http.

推荐答案

下面是从MSDN AC / p

Here is a c/p from MSDN.

    // 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 = "This is a test that posts this string to a Web server.";
    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 ();

链接到页面

这个过程是很容易的,但你必须首先弄清楚你所需要的发送和任何其他特殊编码饼干的/ etc ...这可能需要/。我建议你使用提琴手和/或的萤火为Firefox浏览器。一,你可以看到一切通过网页发生在工作要求,那么你可以模仿你的应用程序相同的行为。

The process is pretty easy but you need to first figure out what you need to send and any other special encoding/cookies/etc... that might be needed. I suggest you use Fiddler and/or Firebug for Firefox. One you can see everything taking place in a working request via the web page, then you can mimic the same behavior in your app.

这篇关于从.NET应用程序张贴形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆