如何使用C#提交http表单

JC.*_*JC. 30 .net html c# forms http-post

我有一个简单的html文件,如

<form action="http://www.someurl.com/page.php" method="POST">
   <input type="text" name="test"><br/>
   <input type="submit" name="submit">
</form>
Run Code Online (Sandbox Code Playgroud)

编辑:我可能不太清楚这个问题

我想编写C#代码,提交此表单的方式与我将上述html粘贴到文件中时完全相同,用IE打开并用浏览器提交.

sha*_*bus 30

这是我最近在接收GET响应的Gateway POST事务中使用的示例脚本.您是否使用自定义C#表单?无论您的目的是什么,只需使用表单中的参数替换字符串字段(用户名,密码等).

private String readHtmlPage(string url)
   {

    //setup some variables

    String username  = "demo";
    String password  = "password";
    String firstname = "John";
    String lastname  = "Smith";

    //setup some variables end

      String result = "";
      String strPost = "username="+username+"&password="+password+"&firstname="+firstname+"&lastname="+lastname;
      StreamWriter myWriter = null;

      HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);
      objRequest.Method = "POST";
      objRequest.ContentLength = strPost.Length;
      objRequest.ContentType = "application/x-www-form-urlencoded";

      try
      {
         myWriter = new StreamWriter(objRequest.GetRequestStream());
         myWriter.Write(strPost);
      }
      catch (Exception e) 
      {
         return e.Message;
      }
      finally {
         myWriter.Close();
      }

      HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
      using (StreamReader sr = 
         new StreamReader(objResponse.GetResponseStream()) )
      {
         result = sr.ReadToEnd();

         // Close and clean up the StreamReader
         sr.Close();
      }
      return result;
   } 
Run Code Online (Sandbox Code Playgroud)


Jef*_*ang 12

您的HTML文件不会直接与C#交互,但您可以编写一些C#,就像它是HTML文件一样.

例如:有一个名为System.Net.WebClient的类,其方法很简单:

using System.Net;
using System.Collections.Specialized;

...
using(WebClient client = new WebClient()) {

    NameValueCollection vals = new NameValueCollection();
    vals.Add("test", "test string");
    client.UploadValues("http://www.someurl.com/page.php", vals);
}
Run Code Online (Sandbox Code Playgroud)

有关更多文档和功能,请参阅MSDN页面.

  • 您的浏览器会发送一堆标题.嗅探您的浏览器使用Fiddler或Firebug发送的HTTP请求.然后使用client.Headers属性复制这些标头. (3认同)

Skl*_*vvz 5

您可以使用HttpWebRequest类来执行此操作.

这里的例子:

using System;
using System.Net;
using System.Text;
using System.IO;


    public class Test
    {
        // Specify the URL to receive the request.
        public static void Main (string[] args)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create (args[0]);

            // Set some reasonable limits on resources used by this request
            request.MaximumAutomaticRedirections = 4;
            request.MaximumResponseHeadersLength = 4;
            // Set credentials to use for this request.
            request.Credentials = CredentialCache.DefaultCredentials;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse ();

            Console.WriteLine ("Content length is {0}", response.ContentLength);
            Console.WriteLine ("Content type is {0}", response.ContentType);

            // Get the stream associated with the response.
            Stream receiveStream = response.GetResponseStream ();

            // Pipes the stream to a higher level stream reader with the required encoding format. 
            StreamReader readStream = new StreamReader (receiveStream, Encoding.UTF8);

            Console.WriteLine ("Response stream received.");
            Console.WriteLine (readStream.ReadToEnd ());
            response.Close ();
            readStream.Close ();
        }
    }

/*
The output from this example will vary depending on the value passed into Main 
but will be similar to the following:

Content length is 1542
Content type is text/html; charset=utf-8
Response stream received.
<html>
...
</html>

*/
Run Code Online (Sandbox Code Playgroud)