0

I am trying to send data to the server and get the response. The only problem I am having is that I am unsure how the parameters work for passing in data. How do I send an HTTP POST with data AND get the response's content? I would be sending a username and a company name to the server and getting the information back.

public enum Verb
{
    GET,
    POST,
    PUT,
    DELETE
}

namespace API_Call_Tests
{
    class Program
    {
        static void Main(string[] args)
        {

            var client = new Client();
            client.EndPoint = @"http://stackoverflow.com/getToken.groovy";
            client.Method = Verb.GET;

            var response = client.Request();
            var result = JsonConvert.DeserializeObject<Result>(response);
            Console.WriteLine("This is seperate");
            Console.WriteLine( result.token +result.message +result.success);
        }

        public class Result
        {
            public string token { get; set; }
            public string message { get; set; }
            public bool success { get; set; }
        }
        public class Client
        {
            public string EndPoint { get; set; }
            public Verb Method { get; set; }
            public string ContentType { get; set; }
            public string PostData { get; set; }

            public Client()
            {
                EndPoint = "";
                Method = Verb.GET;
                ContentType = "application/JSON";
                PostData = "";
            }

            public Client(string endpoint, Verb method, string postData)
            {
                EndPoint = endpoint;
                Method = method;
                ContentType = "text/json";
                PostData = postData;
            }


            public string Request()
            {
                return Request("");
            }

            public string Request(string parameters)
            {
                var request = (HttpWebRequest) WebRequest.Create(EndPoint);
                request.Method = Method.ToString();
                request.ContentLength = 0;
                request.ContentType = ContentType;

                using (var response = (HttpWebResponse) request.GetResponse())
                {
                    var responseValue = string.Empty;

                    if (response.StatusCode != HttpStatusCode.OK)
                    {
                        var message = String.Format("Faile: Received HTTP {0}", response.StatusCode);
                        throw new ApplicationException(message);
                    }

                    using (var responseStream = response.GetResponseStream())
                    {
                        if (responseStream != null)
                            using (var reader = new StreamReader(responseStream))
                            {
                                responseValue = reader.ReadToEnd();
                            }
                    }

                    return responseValue;
                }
            }
        }
    }
}
  • see https://stackoverflow.com/questions/2796084/how-to-simulate-browser-file-upload-with-httpwebrequest – rene Aug 22 '17 at 15:09

1 Answers1

0

Use HttpClient instead as it makes it much easier. See an example here: http://www.infoworld.com/article/3198673/application-development/my-two-cents-on-webclient-vs-httpclient-vs-httpwebrequest.html.

If you want to follow your current approach, you need to get the request stream before you get the response and serialize your content yourself.

Ricardo Peres
  • 13,724
  • 5
  • 57
  • 74