简体   繁体   中英

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 in Header, then have to pass in Body to get response/token (as described in API reference). ,然后必须传递在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 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. Look at this question Setting Authorization Header of HttpClient

It looks like you missed setting of ContentLength property of HttpWebRequest. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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