gpt4 book ai didi

c# - HTTP 发布错误 : An existing connection was forcibly closed by the remote host

转载 作者:行者123 更新时间:2023-12-01 22:19:14 26 4
gpt4 key购买 nike

我意识到已经有很多类似的帖子,但我还没有找到解决方案。我尝试将一些 xml 发布到 MPI 网关,但不断收到以下错误:

Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.

下面是我当前正在使用的代码,但尝试了我能想到的几乎所有不同的方法,它们都返回相同的错误:

        string result = "";

string xml = "<TNSAuthRequest><CardNumber>0123456789</CardNumber><ExpiryDate>1801</ExpiryDate><PurchaseAmt>750</PurchaseAmt><CurrencyCode>826</CurrencyCode><CurrencyExponent>2</CurrencyExponent><CountryCode>826</CountryCode><MerchantName>Mayflower</MerchantName><MerchantId>0123456789</MerchantId><MerchantData>abcdefghijklmnopqrstuvwxyz0123456789</MerchantData><MerchantUrl>example.com</MerchantUrl><NotificationURL>example.com/basket</NotificationURL></TNSAuthRequest>";

var url = "https://mpi.securecxl.com";
byte[] bytes = System.Text.Encoding.ASCII.GetBytes("xmldata=" + xml.ToString());

ServicePointManager.ServerCertificateValidationCallback += new System.Net.Security.RemoteCertificateValidationCallback(ValidateRemoteCertificate);

var req = (HttpWebRequest)WebRequest.Create(url);
req.AllowWriteStreamBuffering = true;
req.ContentType = "text/xml";
req.Method = "POST";
//req.ContentLength = bytes.Length;
req.KeepAlive = false;
req.ProtocolVersion = HttpVersion.Version10;
req.ServicePoint.ConnectionLimit = 1;
//req.Timeout = -1;

try
{
using (var writer = new StreamWriter(req.GetRequestStream(), Encoding.ASCII))
{
writer.WriteLine(bytes);
}
using (WebResponse resp = req.GetResponse())
{
using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
{
result = sr.ReadToEnd().Trim();
}
}
}
catch (Exception ex)
{
result = ex.Message + "<br />" + ex.InnerException.Message + "<br /><br />" + xml.Replace("<", "&lt;");
}

ViewBag.result = result;

我基本上在徘徊,是否有人能看到可能导致此错误的代码中的任何问题,或者是否最有可能是我的问题?尝试在我的本地主机、我们的实时服务器和我自己的私有(private)服务器(具有完全不同的 IP)上运行,但仍然得到相同的结果。

有什么想法吗?

最佳答案

我认为这是因为您正在连接到“https”网址。在这种情况下,您必须将以下行添加到您的代码中。

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

它将接受您的请求的“ssl”协议(protocol)。 “ServicePointManager.ServerCertificateValidationCallback”处理程序仅控制证书有效性。

关于c# - HTTP 发布错误 : An existing connection was forcibly closed by the remote host,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28453353/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com