AI对话接口采用流式返回,使用Flurl处理返回的数据流
using Flurl; using Flurl.Http; [HttpPost] public async Task<string> GetLiushiChatLaw() { //1、请求参数,根据实际情况 YourModel request = new YourModel();string allStr = "";
string chatLawApiUrl = "http://请求地址";
//2、请求。接收类型流Stream,处理请求响应 using (var stream = await chatLawApiUrl.WithHeader("Content-Type", "application/json") .SetQueryParams(new { access_token = "token" }) .PostJsonAsync(request) .ReceiveStream()) { // 逐块读取并处理响应内容 var buffer = new byte[102]; int bytesRead; while ((bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length)) > 0) { // 3、结果转换为字符串 string content = System.Text.Encoding.UTF8.GetString(buffer, 0, bytesRead); allStr += content; _logger.LogInformation(content); } } return allStr; }
标签:http,请求,C#,content,buffer,Flurl,string From: https://www.cnblogs.com/handsomeziff/p/18343375