欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 手游 > C# httpclient 和 Flurl.Http 的测试

C# httpclient 和 Flurl.Http 的测试

2025/2/26 15:45:24 来源:https://blog.csdn.net/ot512csdn/article/details/145823125  浏览:    关键词:C# httpclient 和 Flurl.Http 的测试

关于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;}}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词