C# httpclient 和 Flurl.Http 的测试

news/2025/2/27 0:51:36

关于C#调用接口或Post,Flurl封装了httpclient, CSDN有哥们提供了一个公网的测试网站,可以测试Post调用,我写了2个函数,测试httpclient和Flurl使用Post:

async 和 await 是成对使用的,为了接受web异步返回的数据,winform的button控件中使用,只需要加上async关键字,然后在await中等待函数执行返回。

using Flurl;
using Flurl.Http;
using System.Net.Http.Headers;
using System.Net;
using System.Text;
using Newtonsoft.Json;
using System.Xml.Linq;

namespace mdyPost
{
    public partial class Form1 : Form
    {    
        public string txt = "";

        public Form1()
        {
            InitializeComponent();
        }

        private async void toolStripButton1_Click(object sender, EventArgs e)
        {
            await PostFlurl();
            textBox1.Text = txt;
        }

        public async Task PostFlurl()
        {
            var url = "https://api.apiopen.top/api/login";
            var objData = new { account = "309324904@qq.com", password = "123456" };         

            txt = await url.PostJsonAsync(objData).ReceiveString(); 
        }

        public async Task PostHttpclient()
        {
            string url = "https://api.apiopen.top/api/login";
            var objData = new  { account = "309324904@qq.com", password = "123456"  };

            using (var client = new HttpClient())
            {   
                HttpResponseMessage response = await client.PostAsync(url, new StringContent(JsonConvert.SerializeObject(objData), Encoding.UTF8, "application/json"));           

                textBox1.Text = await response.Content.ReadAsStringAsync();
            }
        }




        public async Task<string> Post()
        {         
           
            string url = "https://devops.seres.cn/v1/completions";
            var ddd = new { Bearer = "sk-3ssmz98iy1r65cOp6cCeEcB1884c4929B583Ab4e8cE0674c", content = "你是" };
            var headers = new { Authorization = "Bearer your_token_here" }; 


            var r = await url.WithHeader("Content-Type", "application/json").PostJsonAsync(ddd).ReceiveString(); 
                                 

            /*
            string url = "https://ldmdy-seres-cn-9wcweeu8000011n.ztna-dingtalk.com/api/v2/open/worksheet/getFilterRows";
            var ddd = new 
            { 
                appKey = "8f93f8b3dc7d4d3d", 
                sign = "MjBlYWQ2OTE3NWVkYzYyOWYwYzRjM2E2MTcxZGNhYjM0MjhhYjUyNDRmNDU4ZWExOWUwOTA2YzRhMjJhOWRjNg==",
                worksheetId = "ibfxmglptb"
            };*/
         
          
            return r;
        }
    }
}


http://www.niftyadmin.cn/n/5869328.html

相关文章

The difference of sort() and sorted() in Python

This is my first time writing a blog in English, so please forgive me for not writing well The sort() method and sorted() function in Python both sort elements, but they differ in usage and behavior: ​1. Type and Usage: ​sort(): A ​list method​ (only …

Jtti.cc:站群服务器SEO优化建议,如何分配多IP?

站群优化的核心目标之一是尽可能通过多个网站互相引导流量&#xff0c;从而提升主站的权重。这时候&#xff0c;多IP的分配至关重要&#xff0c;因为搜索引擎会检测到同一IP下的网站之间的关联性。如果一个IP地址下有过多的相似站点&#xff0c;搜索引擎可能会认为这些站点存在…

‌XPath vs CSS Selector 深度对比

&#x1f4ca; ‌核心差异总览‌ ‌对比维度‌‌XPath‌‌CSS Selector‌‌语法复杂度‌较高&#xff08;需路径表达式&#xff09;简洁&#xff08;类似前端开发习惯&#xff09;‌性能‌较慢&#xff08;全局遍历&#xff09;更快&#xff08;浏览器原生优化&#xff09;‌文…

后端返回文件流,前端导出excel文件

1、当后端接口返回文件流时&#xff0c;需前端导出excel文件&#xff0c;在请求中添加 responseType: blob限制条件&#xff0c;根据返回的文件流导出 封装的方法&#xff1a; /** * 公共的导出excel方法 * param {*} content 后端接口返回的二进制文件 * param {*} name 导出…

【Uniapp-Vue3】在uniapp中使用pinia的基本用法

引入pinia&#xff1a; 在main.js中对pinia进行引入&#xff0c;使用和导出 import * as Pinia from pinia; // 引入pinia app.use(Pinia.createPinia()); // 使用pinia 在项目根目录下创建一个stores文件夹&#xff0c;里面创建一个counter.js文件 我们在counter.js中定义…

​腾讯云 轻量云对象存储

腾讯云轻量云对象存储&#xff08;COS&#xff09;是一款为中小企业、开发者及个人用户提供的简化、低成本、易用的云存储服务。它提供高效、灵活的对象存储解决方案&#xff0c;用户可以通过腾讯云轻量云对象存储轻松存储、管理和访问海量非结构化数据。通过简单的操作&#x…

Windows Server 搭建 RADIUS 认证服务器

Windows Server 搭建 RADIUS 认证服务器 1.搭建 AD CS 证书服务器 2.配置 Active Directory 证书服务 3.搭建 NPS 认证服务器 4.为 NPS 服务器申请证书 5.配置 RADIUS 服务搭建 AD CS 证书服务器 1、打开「服务器管理器」&#xff0c;选择右上角的「管理」>「添加角色和功能…

Selenium 与 Coze 集成

涵盖两者的基本概念、集成步骤、代码示例以及相关注意事项。 基本概念 Selenium:是一个用于自动化浏览器操作的工具集,支持多种浏览器(如 Chrome、Firefox 等),能够模拟用户在浏览器中的各种操作,如点击、输入文本、选择下拉框等,常用于 Web 应用的自动化测试。Coze:它…