Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
615 views
in Technique[技术] by (71.8m points)

c# - Http post error: An existing connection was forcibly closed by the remote host

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;

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.

Any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...