【问题标题】:How would I create a http post request with parameters using a username and company name?如何使用用户名和公司名称创建带有参数的 http post 请求?
【发布时间】:2018-01-30 22:30:47
【问题描述】:

我正在尝试向服务器发送数据并获得响应。我遇到的唯一问题是我不确定参数如何用于传递数据。 如何发送带有数据的 HTTP POST 并获取响应的内容? 我将向服务器发送用户名和公司名称并取回信息。

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;
                }
            }
        }
    }
}

【问题讨论】:

标签: c# json http


【解决方案1】:

改用HttpClient,因为它更容易。在此处查看示例:http://www.infoworld.com/article/3198673/application-development/my-two-cents-on-webclient-vs-httpclient-vs-httpwebrequest.html

如果您想遵循当前的方法,则需要先获取请求流,然后再获取响应并自己序列化您的内容。

【讨论】:

    猜你喜欢
    • 2012-05-02
    • 1970-01-01
    • 2020-11-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-28
    • 1970-01-01
    • 1970-01-01
    • 2018-11-11
    相关资源
    最近更新 更多