我知道有很多类似的帖子,但我还没有找到解决方案。我试图将一些xml发布到MPI网关,但不断收到以下错误:
无法从传输连接中读取数据:现有数据 连接被远程主机强行关闭。
下面是我目前正在使用的代码,但我已经尝试了几乎所有我能想到的不同方法,它们都返回了同样的错误:
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("<", "<");
}
ViewBag.result = result;
如果任何人都可以看到可能导致此错误的代码可能出现任何问题,或者如果它最有可能在我们的问题上出现问题,那么我基本上会徘徊吗?尝试在我的本地主机,我们的实时服务器和我自己的私人服务器(具有完全不同的IP)上运行,并且仍然得到相同的结果。
有什么想法吗?
答案 0 :(得分:25)
我认为是因为你正在连接到&#34; https&#34;网址。在这种情况下,您必须在代码中添加以下行。
button
它会接受&#34; ssl&#34;您的请求的协议。 &#34; ServicePointManager.ServerCertificateValidationCallback&#34;处理程序只是控制证书的有效性。
答案 1 :(得分:0)
也许稍微好一点:
System.Net.ServicePointManager.SecurityProtocol = System.Net.ServicePointManager.SecurityProtocol | System.Net.SecurityProtocolType.Tls12;
答案 2 :(得分:0)
@AlisettarHuseynli 是对的,这有时与 https
有关。最有可能发生在基础架构获得更新时,这可能意味着 TLS
获得更新,例如从 TLS1.0
更新到 TLS1.2
通常发生在某些 API 等中。
如果您尝试访问的服务可以通过 http
访问,请执行此操作。将方案从 https
更改为 http
。在我的情况下工作。否则,您将不得不添加代码以支持更高版本的 TLS
。流行软件通常有一个选择使用 TLS1.2
而不是旧的 TLS1.0
的选项。