How to Send Request JSON Data to API In C# ASP.Net | Microsoft ASP Solutions

Share:

How to Send Request JSON Data to API In C# ASP.Net


  • How to make HTTP POST web request In C# ASP.Net
  • Sending a POST request to a URL from C#
  • How do I make calls to a REST api using C#? | Microsoft ASP Solutions
  • C# how to properly make a http web GET request
  • How make an HTTP request in C#
  • Call a Web API From a .NET Client (C#) - ASP.NET
  • Create HTTP GET and POST Request with C#
  • How to make an HTTP get request with parameters | Microsoft ASP Solutions
  • HttpGet with parameters using HttpWebResponse C#
  • C# how to properly make a http web GET request


CS Page Code Below


 
public void res()
    {
        string pname = "Rajat";
        string pmob = "9873045620";
        string dmob = "9929607023";
        string ptype = "NEW";
        try
        {

            
            var httpWebRequest = (HttpWebRequest)WebRequest.Create("Your API URL");
            httpWebRequest.Method = "POST";
            httpWebRequest.PreAuthenticate = true;                         
            httpWebRequest.Accept = "application/x-www-form-urlencoded";

                        
            httpWebRequest.Headers["key"] = "Your Header Key";
            
            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {                
                
                string json = new JavaScriptSerializer().Serialize(new
                {
                    patient_name = pname,
                    patient_mobile = pmob,
                    patient_type = ptype,
                    doctor_mobile = dmob
                });

                streamWriter.Write(json);
                streamWriter.Flush();
                streamWriter.Close();

                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var result = streamReader.ReadToEnd();
                    lblMess.Text = result;
                }
            }

        }
        catch (Exception ex)
        {
            lblMess.Text = ex.Message;            
        }
    }


No comments