How to make an HTTP POST web request

后端 未结 14 2808
有刺的猬
有刺的猬 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 05:04

    In .net core you can make post-call with following code, here I added some extra features to this code so can make your code work behind a proxy and with network credentials if any, also here I mention that you can change the encoding of your message. I hope this explains all and help you in coding.

    HttpClient client = GetHttpClient(_config);
    
            if (headers != null)
            {
                foreach (var header in headers)
                {
                    client.DefaultRequestHeaders.TryAddWithoutValidation(header.Key, header.Value);
                }
            }
    
            client.BaseAddress = new Uri(baseAddress);
    
            Encoding encoding = Encoding.UTF8;
    
    
            var result = await client.PostAsync(url, new StringContent(body, encoding, "application/json")).ConfigureAwait(false);
            if (result.IsSuccessStatusCode)
            {
                return new RequestResponse { severity = "Success", httpResponse = result.Content.ReadAsStringAsync().Result, StatusCode = result.StatusCode };
            }
            else
            {
                return new RequestResponse { severity = "failure", httpResponse = result.Content.ReadAsStringAsync().Result, StatusCode = result.StatusCode };
            }
    
    
     public HttpClient GetHttpClient(IConfiguration _config)
            {
                bool ProxyEnable = Convert.ToBoolean(_config["GlobalSettings:ProxyEnable"]);
    
                HttpClient client = null;
                if (!ProxyEnable)
                {
                    client = new HttpClient();
                }
                else
                {
                    string ProxyURL = _config["GlobalSettings:ProxyURL"];
                    string ProxyUserName = _config["GlobalSettings:ProxyUserName"];
                    string ProxyPassword = _config["GlobalSettings:ProxyPassword"];
                    string[] ExceptionURL = _config["GlobalSettings:ExceptionURL"].Split(';');
                    bool BypassProxyOnLocal = Convert.ToBoolean(_config["GlobalSettings:BypassProxyOnLocal"]);
                    bool UseDefaultCredentials = Convert.ToBoolean(_config["GlobalSettings:UseDefaultCredentials"]);
    
                    WebProxy proxy = new WebProxy
                    {
                        Address = new Uri(ProxyURL),
                        BypassProxyOnLocal = BypassProxyOnLocal,
                        UseDefaultCredentials = UseDefaultCredentials,
                        BypassList = ExceptionURL,
                        Credentials = new NetworkCredential(ProxyUserName, ProxyPassword)
    
                    };
    
                    HttpClientHandler handler = new HttpClientHandler { Proxy = proxy };
                    client = new HttpClient(handler,true);
                }
                return client;
            }
    

提交回复
热议问题