c# - HttpResponseMessage.Content.ReasAsString 返回空字符串

标签 c# httpclient blazor httpresponsemessage

我有一个 WS,我这样称呼:

HttpResponseMessage response = await client.PostAsync(url, new StringContent(json));

WS 将抛出异常(禁止),并且在进行 PostAsync 调用的 Blazor 应用程序中,我将收到 HttpResponseMessage响应代码为 405,不知道为什么是 405,它应该是 403(Postman 返回 403)。

我已启用 CORS(ServiceStack 代码):

Plugins.Add(new CorsFeature(allowedOrigins: "*",
  allowedMethods: "GET, POST, PUT, DELETE, OPTIONS",
  allowedHeaders: "*",
  allowCredentials: true));

这是我在 PostAsync 之前所做的一些 Console.Writeline:

enter image description here

Failed to load resource: the server responded with a status of 405 ()

** 已更新 **

这是两种方法:

    public async Task<TResponse> PostAsync<TResponse, TRequest>(string requestUri, TRequest request)
    {
        string url = GetUrl(requestUri);
        Console.WriteLine("URI: " + url);
        string json = JsonConvert.SerializeObject(request);
        Console.WriteLine($"{url} | {json}");
        HttpResponseMessage response = await client.PostAsync(url, new StringContent(json));
        return await GetResponseOrThrowHttpException<TResponse>(response);
    }


    private async Task<T> GetResponseOrThrowHttpException<T>(HttpResponseMessage response)
    {
        Console.WriteLine($"GetResponseOrThrowHttpException: {response.StatusCode}");
        string responseString = await response.Content.ReadAsStringAsync();
        Console.WriteLine($"GetResponseOrThrowHttpException ContentStringResult: |{responseString}|");
        if (!response.IsSuccessStatusCode)
        {
            Newtonsoft.Json.Linq.JObject jsonObject = Newtonsoft.Json.Linq.JObject.Parse(responseString);
            string responseStatusString = jsonObject["ResponseStatus"].ToString();

            Console.WriteLine($"GetResponseOrThrowHttpException 4: {responseStatusString}");
            ResponseStatus responseStatus = JsonConvert.DeserializeObject<ResponseStatus>(responseStatusString);
            Console.WriteLine($"Throwing HttpException: {response.StatusCode} {responseStatus.Message}");
            throw new HttpException(response.StatusCode, responseStatus.Message);
        }
        return JsonConvert.DeserializeObject<T>(responseString);
    }

当我尝试获取响应的字符串值时,它是空的:

string responseString = await response.Content.ReadAsStringAsync();

responseString是一个空(长度为 0)字符串。

如果我在 Postman 中运行完全相同的请求,我会得到有效的响应:

enter image description here

因此,响应 JSON(如上图底部所示)是我想要在 Blazor 应用程序中使用的内容,并将其解析为 JSON 对象并从那里继续。 我还注意到,我收到了 403 错误,这是我所预料的,而在 Blazor 应用程序中,我收到了 405 错误。

即使我在 WS 端启用了 CORS,这是否是 CORS 问题?

最佳答案

我想你的内容应该这样发布:

new StringContent(json, Encoding.UTF8, "application/json")

即使这不是你痛苦的原因,最好使用它......

希望这有帮助...

关于c# - HttpResponseMessage.Content.ReasAsString 返回空字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56597458/

相关文章:

c# - 为什么此代码适用于不同类型?

c# - 在 C# 中更新 XML 节点值的最佳方法是什么?

java - HttpClient 4.1 出现 SSL "Peer Not Authenticated"错误

asp.net - Blazor WeatherForecast 示例如何工作?

c# - 一个或多个空格、制表符或换行符的正则表达式

c# - 滥用最小起订量来测试 if 条件

c# - 可以作为参数传递给 POST 方法的对象的最大大小

android - HttpClient 在执行方法时失败

c# - 如何在 Blazor 服务器中动态添加、删除输入文本框和获取值?

c# - 动态生成 blazor mudblazor 表