How to make an HTTP POST web request

后端 未结 14 2801
有刺的猬
有刺的猬 2020-11-21 04:31

Canonical
How can I make an HTTP request and send some data using the POST method?

14条回答
  •  遇见更好的自我
    2020-11-21 04:53

    This is a complete working example of sending/receiving data in JSON format, I used Visual Studio 2013 Express Edition:

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.OleDb;
    using System.IO;
    using System.Linq;
    using System.Net.Http;
    using System.Text;
    using System.Threading.Tasks;
    using System.Web.Script.Serialization;
    
    namespace ConsoleApplication1
    {
        class Customer
        {
            public string Name { get; set; }
            public string Address { get; set; }
            public string Phone { get; set; }
        }
    
        public class Program
        {
            private static readonly HttpClient _Client = new HttpClient();
            private static JavaScriptSerializer _Serializer = new JavaScriptSerializer();
    
            static void Main(string[] args)
            {
                Run().Wait();
            }
    
            static async Task Run()
            {
                string url = "http://www.example.com/api/Customer";
                Customer cust = new Customer() { Name = "Example Customer", Address = "Some example address", Phone = "Some phone number" };
                var json = _Serializer.Serialize(cust);
                var response = await Request(HttpMethod.Post, url, json, new Dictionary());
                string responseText = await response.Content.ReadAsStringAsync();
    
                List serializedResult = _Serializer.Deserialize>(responseText);
    
                Console.WriteLine(responseText);
                Console.ReadLine();
            }
    
            /// 
            /// Makes an async HTTP Request
            /// 
            /// Those methods you know: GET, POST, HEAD, etc...
            /// Very predictable...
            /// String data to POST on the server
            /// If you use some kind of Authorization you should use this
            /// 
            static async Task Request(HttpMethod pMethod, string pUrl, string pJsonContent, Dictionary pHeaders)
            {
                var httpRequestMessage = new HttpRequestMessage();
                httpRequestMessage.Method = pMethod;
                httpRequestMessage.RequestUri = new Uri(pUrl);
                foreach (var head in pHeaders)
                {
                    httpRequestMessage.Headers.Add(head.Key, head.Value);
                }
                switch (pMethod.Method)
                {
                    case "POST":
                        HttpContent httpContent = new StringContent(pJsonContent, Encoding.UTF8, "application/json");
                        httpRequestMessage.Content = httpContent;
                        break;
    
                }
    
                return await _Client.SendAsync(httpRequestMessage);
            }
        }
    }
    

提交回复
热议问题