HttpWebRequest.GetRequestStream() 连接在可执行文件中被主动拒绝,但在 WPF 独立应用程序中却没有

HttpWebRequest.GetRequestStream() connection gets actively refused in executable but not in WPF standalone application

我正在使用 Web 服务器通过 HttpWebRequests 调用它的 API。我出于测试目的编写了一个独立的 WPF 应用程序,并且我的所有请求都正常运行。当我在我的生产应用程序中引用工作项目文件时,它现在返回请求被服务器主动拒绝。

public string Post(string xmlData, Transaction transaction)
{
        var result = "";

        try
        {
            var webReq = (HttpWebRequest)WebRequest.Create(BaseUrl);
            webReq.Accept = "application/xml";
            webReq.ContentType = "application/xml";
            webReq.Method = "POST";
            webReq.KeepAlive = false;
            webReq.Proxy = WebRequest.DefaultWebProxy;
            webReq.ProtocolVersion = HttpVersion.Version10;

            // If we passed in data to be written to the body of the request add it
            if (!string.IsNullOrEmpty(xmlData))
            {
                webReq.ContentLength = xmlData.Length;
                using (var streamWriter = new StreamWriter(webReq.GetRequestStream())) /**CONNECTION REFUSED EXCEPTION HERE**/
                {

                    streamWriter.Write(xmlData);
                    streamWriter.Flush();
                    streamWriter.Close();
                }
            }
            else //Otherwise write empty string as body
            {
                webReq.ContentLength = 0;
                var data = "";
                using (var streamWriter = new StreamWriter(webReq.GetRequestStream()))
                {

                    streamWriter.Write(data);
                    streamWriter.Flush();
                    streamWriter.Close();
                }
            }

            //Attempt to get response from web request, catch exception if there is one
            using (var response = (HttpWebResponse)webReq.GetResponse())
            {
                using (var streamreader =
                    new StreamReader(response.GetResponseStream() ?? throw new InvalidOperationException()))
                {
                    result = streamreader.ReadToEnd();
                }
            }

            return result;
        }
        catch (WebException e)
        {
            //Handle web exceptions here               
        }
        catch (Exception e)
        {
            //Handle other exceptions here
        }
    }

有没有其他人遇到过这个问题?

在查看了您的 fiddler 请求后,我可以说原因可能是 IP 地址不同。

您第一次使用 192.168.1.186:44000,第二次使用 192.168.1.86:44000