14

How is it possible to make a POST request to ASP.Net web api from C#. I have used Newtonsoft dll files to create the json, but, I am not able to send it to the api.

My code is :

        Login login = new Login
        {
            userid = username.Text,
            pass = pass.Text
        };
        string json = JsonConvert.SerializeObject(login, Formatting.Indented);

How to send this json to web api, and get a response ?

Edit

string url = "myurl";


        var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
        httpWebRequest.ContentType = "application/json; charset=utf-8";
        httpWebRequest.Method = "POST";
        httpWebRequest.Accept = "application/json; charset=utf-8";

        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {

            json = json.Replace("\r\n","");
            //json = json.Replace("\",", "\","   + "\"" +"\u002B");
            streamWriter.Write(json);
            streamWriter.Flush();
            streamWriter.Close();
        }

        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            var result = streamReader.ReadToEnd();
            pass.Text = result.ToString();
        }
1
  • I would use WebClient. There are tons of examples on StackOverflow how to use it. It's less involved than HttpWebRequest Commented Jul 14, 2014 at 6:40

2 Answers 2

14

I made it work, by having a few edits. Instead of directly serializing the json object, I used a class, and assigned values to it, and serialized it using JavaScriptSerializer. Thanks for your help Shekhar.

        var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
        httpWebRequest.ContentType = "application/json; charset=utf-8";
        httpWebRequest.Method = "POST";
        httpWebRequest.Accept = "application/json; charset=utf-8";

        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {
            string loginjson = new JavaScriptSerializer().Serialize(new
            {
                userid = username.Text,
                password = pass.Text
            });

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

            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var result = streamReader.ReadToEnd();
                pass.Text = result.ToString();
            }
        }
Sign up to request clarification or add additional context in comments.

Comments

3

You can use HttpWebRequest class to create a request and use StreamWriter to write your Json with the request and finally get HttpWebResponse from the Web API.

var httpWebRequest = (HttpWebRequest)WebRequest.Create("path/api");
httpWebRequest.ContentType = "text/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
   streamWriter.Write(json);
   streamWriter.Flush();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

2 Comments

I did this already. But I am not getting success message as false. Please see the edit.
Success:false was the message I used to get. I made it work. I am posting it as an answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.