1
public static async Task<T> GetDataFromWebServiceWithCookies<T>(string serviceName, string methodName, Dictionary<string, object> parameters)
{
    HttpResponseMessage httpResponseMessage;
    string httpResponseMessageData = "";

    HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "ServiceModule/" + serviceName + "/" + methodName);
    byte[] json = JsonSerializer.SerializeToUtf8Bytes(parameters);
    httpRequestMessage.Content = new ByteArrayContent(json);

    httpResponseMessage = await HttpClient.SendAsync(httpRequestMessage);

    if (httpResponseMessage.IsSuccessStatusCode)
    {
        httpResponseMessageData = await httpResponseMessage.Content.ReadAsStringAsync();
    }

    return JsonSerializer.Deserialize<T>(httpResponseMessageData);
}

First of all this is my very first post on Stack Overflow, so I hope I am not screwing up. I am using the above code to communicate with a web service using Blazor WASM HttpClient. Everything works fine including setting the call parameters as message content, but I didn't find any way to set cookies in my httpRequestMessage. The .NET classes that can be used for cookie handling like 'CookieContainer', or setting the 'UseCookies' property on a HttpClientHandler are not supported on Blazor WASM applications. Is there any way to send a cookie using Blazor WebAssembly yet? The easiest way would be to just add the cookie as a header to httpRequestMessage, but this doesn't work either.

  • [Here](https://stackoverflow.com/q/12373738/60761) is a general answer but it won't work for Blazor Wasm, that uses a different HttpClient. – H H Apr 14 '22 at 15:58
  • That is the problem. The .NET class CookieContainer which is used in this example is "not supported on browser". – Marcel Klein Apr 17 '22 at 14:02

0 Answers0