Http post error:远程主机强行关闭现有连接 [英] Http post error: An existing connection was forcibly closed by the remote host

查看:637
本文介绍了Http post error:远程主机强行关闭现有连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I realise there have been a number of similar posts to this but I haven't found a solution yet. Am trying to post some xml to an MPI gateway but keep getting the following error:


无法从传输连接读取数据:现有
连接被远程主机强行关闭。

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

以下是我目前正在使用的代码但是已尝试过我能想到的每种不同方法都会返回相同的错误:

Below is the code I'm currently using but have tried just about every different approach I can think of and they all return the same error:

        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;

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

Am basically wandering if anyone can see anything that might be wrong with the code that could be causing this error or if it's most likely I problem on the their end? Have tried running on my localhost, our live server and my own private server (with a completely different IP) and still get same result.

任何想法?

推荐答案

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

I think its because you are connecting to "https" url. In this case you have to add following line to your code.

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

它将接受您的请求的ssl协议。 ServicePointManager.ServerCertificateValidationCallback处理程序只控制证书有效性。

It will accept "ssl" protocol for your request. "ServicePointManager.ServerCertificateValidationCallback" handler just controls certificate validity.

这篇关于Http post error:远程主机强行关闭现有连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆