'WebRequest.Create(string)' 已过时:。请改用 HttpClient。'

问题描述 投票:0回答:1

嗨,我正在将代码从 .net core 迁移到 .net 6.0 并高于 mesprivate static HttpWebRequest

CreateSoapRequest(string url, string action)
        {
            var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);

            httpWebRequest.ContentType = "text/xml;charset=\"utf-8\"";
            httpWebRequest.Accept = "text/xml";
            httpWebRequest.Method = "POST";
            httpWebRequest.Headers.Add("SOAPAction", string.Format("\"{0}\"", action));
            return httpWebRequest;
        }

我做了下面的事情来解决这个问题

CreateSoapRequest(string url, string action)
        {
 var client = new HttpClient();
            var httpResponse = client.PostAsync(url, httpWebRequest);
            var httpWebRequest = new StringContent("", Encoding.UTF8, "text/xml");
            httpWebRequest.Headers.Add("SOAPAction", string.Format("\"{0}\"", action));
            httpWebRequest.Headers.ContentType = new MediaTypeHeaderValue("text/xml;charset=\"utf-8\"");
            return httpResponse;
}

但主要问题是在进行此更改后,我在使用此请求的方法上遇到错误

private static void AddMessageToRequest(string soapMessage, WebRequest request)
        {
            var requestStream = request.GetRequestStream();
            
            LoadXml(soapMessage).Save(requestStream);
            requestStream.Close();
        }

private static string GetResponse(WebRequest request)
        {
            try
            {
                return Read(request.GetResponse().GetResponseStream());
            }
            catch (WebException ex)
            {
                if (ex.Response != null)
                {
                    return Read(ex.Response.GetResponseStream());
                }
                throw;
            }
        }




private static string PostSoap(string url, string soapMessage, string action)
        {
            var soapRequest = CreateSoapRequest(url, action);

            AddMessageToRequest(soapMessage, soapRequest);
            return GetResponse(soapRequest);
        }

任何人都可以帮助我在这种情况下使用httpclient

c# migration httpclient .net-6.0 webclient
1个回答
0
投票

您在使用

HttpClient
时遇到很多问题。

  • 您需要使用
    HttpRequestMessage
    ,并且需要一个
    Content
    ,其中已经内部有一个流。您不会向它“索取”流媒体,而是“给予”它一个流媒体。所以你需要StreamContent
    PostAsync
    只是上面的一个简短版本,但你不能设置标题。
  • 您需要
    using
    来请求和响应。
  • 缓存
    HttpClient
    以避免套接字耗尽。
  • 使用
    async
  • await
  • 
    
    static HttpClient _client = new();
    
    HttpRequestMessage CreateSoapRequest(string url, string action)
    {
        var message = new HttpRequestMessage(HttpMethod.Post, url);
        message.Headers.Add("SOAPAction", "\"{action}\"");
        message.Headers.ContentType = new MediaTypeHeaderValue("text/xml;charset=\"utf-8\"");
        return httpResponse;
    }
    
    private static void AddMessageToRequest(string soapMessage, HttpRequestMessage request)
    {
        var mem = new MemoryStream();
        LoadXml(soapMessage).Save(requestStream);
        mem.Position = 0;
        message.Content = new StreamContent(mem, Encoding.UTF8, "text/xml");
    }
    
    private static async Task<string> GetResponse(HttpRequestMessage request)
    {
        using var response = await _client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
        // maybe call response.EnsureSuccessStatusCode ??
        var str = await response.ReadAsStringAsync();
        return str;
    }
    
    private static string PostSoap(string url, string soapMessage, string action)
    {
        using var soapRequest = CreateSoapRequest(url, action);
        AddMessageToRequest(soapMessage, soapRequest);
        return GetResponse(soapRequest);
    }
    
© www.soinside.com 2019 - 2024. All rights reserved.