zoukankan      html  css  js  c++  java
  • .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 + "
    ");// 开始边界符
                var endBoundary = System.Text.Encoding.ASCII.GetBytes("--" + boundary + "--
    ");// 结束结束符
                var newLineBytes = System.Text.Encoding.UTF8.GetBytes("
    ");
                using (var stream = new System.IO.MemoryStream())
                {
                    stream.Write(beginBoundary, 0, beginBoundary.Length);// 写入开始边界符
    
                    // 写入字符串
                    var keyValue = "Content-Disposition: form-data; name="{0}"
    
    {1}
    ";
                    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
            }
        }
    }
  • 相关阅读:
    今天还做了点有意义的事
    读“记当年的公开课”
    无语
    小议如何控制学生机结束学生端多媒体控制平台程序
    今天去了中山
    Windows服务创建及安装
    SQL Server数据库表锁定原理以及如何解除表的锁定示例演示
    本地SQL脚本操作外部服务器结果集
    list.FindAll
    一个高效的数据分页的存储过程 可以轻松应付百万数据
  • 原文地址:https://www.cnblogs.com/suqifeng/p/14152024.html
Copyright © 2011-2022 走看看