简体   繁体   English

HttpResponseMessage.Content.ReasAsString返回一个空字符串

[英]HttpResponseMessage.Content.ReasAsString returns an empty string

I have a WS that I call like this: 我有一个这样的WS:

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

the WS will throw an Exception (Forbidden) and in my Blazor application that made the PostAsync call, I will get an HttpResponseMessage with response code 405, not sure why its 405, it should be 403 (Postman returns 403). WS将抛出一个异常(禁止),在我的Blazor应用程序中,该应用程序进行了PostAsync调用,我将收到一个HttpResponseMessage ,其响应代码为405,不确定为什么它的405应该为403(邮递员返回403)。

I have enabled CORS ( ServiceStack code): 我启用了CORS( ServiceStack代码):

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

This is some Console.Writeline I did, just before the PostAsync: 这是我在PostAsync之前所做的一些Console.Writeline:

在此处输入图片说明

Failed to load resource: the server responded with a status of 405 () 加载资源失败:服务器响应状态为405()

** UPDATED ** ** 更新 **

This are the two methods: 这是两种方法:

    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);
    }

When I try to get the string value of the response, it is empty: 当我尝试获取响应的字符串值时,它为空:

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

and the responseString is an empty (length 0) string. 并且responseString是一个空(长度为0)字符串。

If I run the exact same request in Postman, I get a valid response: 如果我在Postman中运行完全相同的请求,则会收到有效的响应:

在此处输入图片说明

So, the response JSON, seen at the bottom in the image above, is what I want to work with in the Blazor app, and parse it as a JSON object and move on from there. 因此,我想在Blazor应用程序中使用响应JSON(位于上图底部),然后将其解析为JSON对象并从那里继续前进。 I also note that I get here a 403 error, which I expected, and in the Blazor app, I get a 405. 我还注意到,我在这里遇到了403错误,这是我所期望的,在Blazor应用中,我得到了405错误。

Is this a CORS issue even though I have enabled CORS on the WS side? 即使我在WS端启用了CORS,这也是CORS问题吗?

I guess your content should be posted like that: 我想您的内容应该这样发布:

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

Even if this is not the cause of your suffering, it's better to use this so... 即使这不是造成您痛苦的原因,还是最好使用此方法...

Hope this helps... 希望这可以帮助...

Verify that you have setup proper CORS configuration for the domain. 验证您是否为域设置了正确的CORS配置。 Looks like you made call to another domain:port combination from your Blazor application. 看来您是从Blazor应用程序调用了另一个domain:port组合。 Even if this C#, all security rules inside browser still applies. 即使使用此C#,浏览器内部的所有安全规则仍然适用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM