简体   繁体   English

如何在C#中使用HttpWebRequest传递header和Body值?

[英]How to pass header and Body value using HttpWebRequest in C#?

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). 我试图通过C#中的HttpWebRequest类将API凭证数据发布到API,就像我必须在Header中传递“Content-Type:application / x-www-form-urlencoded” ,然后必须传递“grant_type:client_credentials,client_id:ruban123 ,client_secret:123456“在Body中获取响应/令牌(如API参考中所述)。

Bellow i attached work which i did 贝娄我附上了我做过的工作

    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]

Here is correct HttpWebRequest using: 这是正确的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 approach is not relevant. HttpWebRequest方法不相关。 Look at this question Setting Authorization Header of HttpClient 看看这个问题设置HttpClient的授权标题

It looks like you missed setting of ContentLength property of HttpWebRequest. 看起来你错过了HttpWebRequest的ContentLength属性的设置。 It should be equal number of bytes to send. 它应该是相同的字节数发送。

Se this link for more information: 有关更多信息,请访问此链接:

https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebrequest.contentlength?view=netframework-4.8 https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebrequest.contentlength?view=netframework-4.8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM