fcydj1984 2025-03-15 00:30 采纳率: 40%
浏览 17

asp.net 调用http接口无返回值

消息头如此,加了一个 参数
Authorization Bearer XR8JYDY-GETMVC1-NAWZGXQ-6DVJMKX

img

消息体
{"message":"健康产品","mode":"query"}

用postman可以成功,但是用asp.net没有返回值,代码如下,请帮忙检查下错在哪里


```c#

using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

public class AnyThingLLMClient
{
    private readonly HttpClient _httpClient;
    private readonly string _apiKey;

    public AnyThingLLMClient(string apiKey)
    {
        _httpClient = new HttpClient();
        _apiKey = apiKey;
    }

    public async Task<string> CallAnyThingLLMAsync(string prompt)
    {
        var requestUrl = "http://localhost:3001/api/v1/workspace/customer/chat"; // 替换为实际的API URL

        var requestBody = new
        {
            message = prompt,
            mode = "query"
        };

        var jsonContent = JsonConvert.SerializeObject(requestBody);
        var httpContent = new StringContent(jsonContent, Encoding.UTF8, "application/json");

        //_httpClient.DefaultRequestHeaders.Clear();
        //_httpClient.DefaultRequestHeaders.Add("CustomHeader", "HeaderValue");
        //_httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", "XR8JYDY-GETMVC1-NAWZGXQ-6DVJMKX");
        _httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer XR8JYDY-GETMVC1-NAWZGXQ-6DVJMKX");

        var response = await _httpClient.PostAsync(requestUrl, httpContent);

        if (response.IsSuccessStatusCode)
        {
            var responseContent = await response.Content.ReadAsStringAsync();
            return responseContent;
        }
        else
        {
            throw new HttpRequestException("Error: {response.StatusCode}");
        }
    }
}
        
```c#



  • 写回答

6条回答 默认 最新

  • 阿里嘎多学长 2025-03-15 00:30
    关注

    阿里嘎多学长整理AIGC生成,因移动端显示问题导致当前答案未能完全显示,请使用PC端查看更加详细的解答过程

    ASP.NET 调用 HTTP 接口无返回值

    你遇到了 ASP.NET 调用 HTTP 接口无返回值的问题。根据你的描述,你使用 ASP.NET 调用 HTTP 接口,添加了 Authorization 头和请求体,但是没有收到返回值。

    可能的解决方案:

    1. 检查 HTTP 接口是否正确:确保 HTTP 接口正确地处理请求,并且返回正确的响应。
    2. 检查 Authorization 头是否正确:确保 Authorization 头的值正确,包括 token 的格式和值。
    3. 检查请求体是否正确:确保请求体的格式和内容正确。
    4. 使用 Fiddler 或其他调试工具来检查请求和响应:使用 Fiddler 或其他调试工具来检查请求和响应,可以帮助你找到问题的根源。

    以下是一个简单的示例代码,使用 HttpClient 在 ASP.NET 中调用 HTTP 接口:

    using System;
    using System.Net.Http;
    using System.Text;
    
    public class MyController : Controller
    {
        public async Task<IActionResult> MyMethod()
        {
            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "XR8JYDY-GETMVC1-NAWZGXQ-6DVJMKX");
                var request = new HttpRequestMessage(HttpMethod.Post, "https://example.com/api/endpoint")
                {
                    Content = new StringContent("{\"message\":\"健康产品\",\"mode\":\"query\"}", Encoding.UTF8, "application/json")
                };
                var response = await client.SendAsync(request);
                if (response.IsSuccessStatusCode)
                {
                    // 处理响应
                }
                else
                {
                    // 处理错误
                }
            }
            return Ok();
        }
    }
    

    在上面的代码中,我们使用 HttpClient 创建了一个请求,并添加了 Authorization 头和请求体。然后,我们使用 SendAsync 方法发送请求,并检查响应的状态码。如果响应状态码为 200,则处理响应;否则,处理错误。

    评论

报告相同问题?

问题事件

  • 创建了问题 3月15日