其他分享
首页 > 其他分享> > .NET HttpWebRequest POST form-data数据格式

.NET HttpWebRequest POST form-data数据格式

作者:互联网

  

using System;


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

            #region http-post-fromData Test

            string reqUrl = "http://#################/oauth/token";
            System.Collections.Generic.Dictionary<string, string> parameters = new System.Collections.Generic.Dictionary<string, string>();
            parameters.Add("grant_type", "client_credentials");
            parameters.Add("client_id", "RDClient");
            parameters.Add("client_secret", "secret");
            System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(reqUrl);
            var boundary = "---------------" + DateTime.Now.Ticks.ToString("x"); // 边界符
            webRequest.Method = "POST";
            webRequest.Timeout = 60000;
            webRequest.ContentType = "multipart/form-data; boundary=" + boundary;
            var beginBoundary = System.Text.Encoding.ASCII.GetBytes("--" + boundary + "\r\n");// 开始边界符
            var endBoundary = System.Text.Encoding.ASCII.GetBytes("--" + boundary + "--\r\n");// 结束结束符
            var newLineBytes = System.Text.Encoding.UTF8.GetBytes("\r\n");
            using (var stream = new System.IO.MemoryStream())
            {
                stream.Write(beginBoundary, 0, beginBoundary.Length);// 写入开始边界符

                // 写入字符串
                var keyValue = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}\r\n";
                foreach (string key in parameters.Keys)
                {
                    var keyValueBytes = System.Text.Encoding.UTF8.GetBytes(string.Format(keyValue, key, parameters[key]));
                    stream.Write(beginBoundary, 0, beginBoundary.Length);
                    stream.Write(keyValueBytes, 0, keyValueBytes.Length);
                }
                //写入结束边界符
                stream.Write(endBoundary, 0, endBoundary.Length);
                webRequest.ContentLength = stream.Length;
                stream.Position = 0;
                var tempBuffer = new byte[stream.Length];
                stream.Read(tempBuffer, 0, tempBuffer.Length);
                using (System.IO.Stream requestStream = webRequest.GetRequestStream())
                {
                    requestStream.Write(tempBuffer, 0, tempBuffer.Length);
                    using (var response = webRequest.GetResponse())
                    using (System.IO.StreamReader httpStreamReader = new System.IO.StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8))
                    {
                        string strResponse = httpStreamReader.ReadToEnd();
                        //var obj = JsonConvert.DeserializeAnonymousType(strResponse, new { access_token = "" });
                        Console.WriteLine(strResponse);

                        Console.ReadKey();
                    }
                }

            }

            #endregion
        }
    }
}

 

标签:form,stream,System,Length,webRequest,HttpWebRequest,var,数据格式,parameters
来源: https://www.cnblogs.com/suqifeng/p/14152024.html