.net post form in code behind

前端 未结 4 492
野性不改
野性不改 2020-12-21 07:06

I wanna make a post form in code behind. I have simple html post is working but when I try make it WebRequest I can\'t make it work.

Thanks for you time in advance.<

4条回答
  •  猫巷女王i
    2020-12-21 07:43

    Try like this.

    using (WebClient client = new WebClient())
       {
    
           byte[] response = client.UploadValues("http://dork.com/service", new NameValueCollection()
           {
               { "home", "Cosby" },
               { "favorite+flavor", "flies" }
           });
       }
    

    You will need these includes:

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

    If you're insistent on using a static method/class:

    public static class Http
    {
        public static byte[] Post(string uri, NameValueCollection pairs)
        {
            byte[] response = null;
            using (WebClient client = new WebClient())
            {
                response = client.UploadValues(uri, pairs);
            }
            return response;
        }
    }
    

    Then simply:

    Http.Post("http://dork.com/service", new NameValueCollection() {
        { "home", "Cosby" },
        { "favorite+flavor", "flies" }
    });
    

提交回复
热议问题