C# json 传参发送 post 请求例子

2023-02-18 15:07:32 浏览数 (2)

代码语言:javascript复制
using Fiddler;
using System;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.IO;
using System.Net;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Util
{
    class DecryptionUtil
    {

        public class Rootobject
        {
            public Root[] Root { get; set; }
        }

        public class Root
        {

        }

        // 随机字符串
        private static Random random = new Random();
        public static string RandomString(int length)
        {
            const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
            return new string(Enumerable.Repeat(chars, length)
              .Select(s => s[random.Next(s.Length)]).ToArray());
        }

        // post 请求方法
        public static string PostUrl(string url, string postData)
        {
            string result = "";

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

            req.Method = "POST";

            req.ContentType = "application/json";

            byte[] data = Encoding.UTF8.GetBytes(postData);

            req.ContentLength = data.Length;

            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(data, 0, data.Length);
                reqStream.Close();
            }

            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            Stream stream = resp.GetResponseStream();

            //获取响应内容
            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
            {
                result = reader.ReadToEnd();
            }
            return result;
        }

        /// <summary>
        /// 获取JSON字符串中指定对象的值
        /// </summary>
        /// <param name="jsonString">输入需要解析的字符内容</param>
        /// <param name="index">对象索引</param>
        /// <returns>JSON字符串中指定索引的对象的值</returns>
        public static string GetValueFromJArray(string jsonString, int index)
        {
            JArray jArray = JArray.Parse(jsonString);
            return jArray[index].ToString();
        }
        /// <summary>
        /// 获取JSON字符串中指定对象的值
        /// </summary>
        /// <param name="jsonString">输入需要解析的字符内容</param>
        /// <param name="key">对象关键字</param>
        /// <returns>JSON字符串中指定关键字的对象的值</returns>
        public static string GetValueFromJObject(string jsonString, string key)
        {
            JObject jsonObj = JObject.Parse(jsonString);
            return jsonObj[key].ToString();
        }

        // 发起 post 请求
        public static string PostSaying(int page)
        {
            try
            {
                // 请求接口
                string url = "https://xieboke.net/api/saying/?format=json";
                
                // json 字符串传参
                String postData = "{"keys":"62517ce4d7832b","page":""   page   ""}";

                string result = PostUrl(url, postData);
                FiddlerApplication.Log.LogString(result.ToString());
                
                // json 取特定的 key 值
                var jObject = JObject.Parse(result);
                return jObject["results"].ToString();

            }
            catch (Exception ex)
            {
                // Console.WriteLine(ex.ToString());
                return null;
            }
        }

    }
}

我这边主要是在 fiddler 插件开发时使用到 post 请求,以及 json 解析

0 人点赞