如何在 C# 中使用 HttpWebRequest 传递标头和正文值? [英] How to pass header and Body value using HttpWebRequest in C#?

查看:33
本文介绍了如何在 C# 中使用 HttpWebRequest 传递标头和正文值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过 C# 中的 HttpWebRequest 类将 API 凭据数据发布到 API,就像我必须在标题中传递 "Content-Type:application/x-www-form-urlencoded" 一样,然后有在正文中传递 "grant_type:client_credentials,client_id:ruban123,client_secret:123456" 以获取响应/令牌(如 API 参考中所述).

Im trying to POST API credentials data to API through HttpWebRequest class in C#, like i have to pass "Content-Type:application/x-www-form-urlencoded" in Header, then have to pass "grant_type:client_credentials,client_id:ruban123,client_secret:123456" in Body to get response/token (as described in API reference).

    public class DataModel
    {
        public string grant_type { get; set; }
        public string client_id { get; set; }
        public string client_secret { get; set; }
    }
    static void Main(string[] args)
    {

        try
        {
            DataModel dm = new DataModel();
            dm.grant_type = "client_credentials";
            dm.client_id = "ruban123";
            dm.client_secret = "123456";

            var credentials = dm.grant_type + dm.client_id + dm.client_secret;

            #region Http Post
            string messageUri = "https://sampleurl.apivision.com:8493/abc/oauth/token";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(messageUri);
            request.Headers.Add("Authorization", "Basic " + credentials);
            request.ContentType = "application/json";
            request.Method = "POST";
            using (var streamWriter = new StreamWriter(request.GetRequestStream()))
            {
                string jsonModel = Newtonsoft.Json.JsonConvert.SerializeObject(dm);
                streamWriter.Write(jsonModel);
                streamWriter.Flush();
                streamWriter.Close();
            }
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            Stream responseStream = response.GetResponseStream();

            string jsonString = null;

            using (StreamReader reader = new StreamReader(responseStream))
            {
                jsonString = reader.ReadToEnd();
                Console.WriteLine();
                reader.Close();
            }

            #endregion Http Post
        }

        catch (Exception ex)
        {
        }
    }[API REFERENCE][1]

推荐答案

这是正确的 HttpWebRequest 使用:

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(pathapi);
    request.Method = "POST";
    string postData = "grant_type=client_credentials&client_id=ruban123&client_secret=123456";
    ASCIIEncoding encoding = new ASCIIEncoding();
    byte[] bytes = encoding.GetBytes(postData);

    request.ContentType = "application/x-www-form-urlencoded";

    request.ContentLength = bytes.Length;
    Stream newStream = request.GetRequestStream();
    newStream.Write(bytes, 0, bytes.Length);

    HttpWebResponse response = request.GetResponse() as HttpWebResponse;

HttpWebRequest 方法不相关.看这个问题设置HttpClient的授权头

HttpWebRequest approach is not relevant. Look at this question Setting Authorization Header of HttpClient

这篇关于如何在 C# 中使用 HttpWebRequest 传递标头和正文值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆