错误:基础连接已关闭:连接意外关闭。

时间:2013-08-26 09:06:55

标签: asp.net httpwebrequest

我在其他帖子中尝试过建议但没有用。当我尝试使用GetResponse()发送请求并获得响应时,我收到此错误。

我的代码是这样的,

protected string CreateSaleRequest(double amount, int installment)
  {
    string request = "";
    try
    {
      XmlNode node = null;
      XmlDocument _msgTemplate = new XmlDocument();
      _msgTemplate.LoadXml("<?xml version=\"1.0\" encoding=\"UTF-8\" ?><ePaymentMsg VersionInfo=\"2.0\" TT=\"Request\" RM=\"Direct\" CT=\"Money\">" +
      "<Operation ActionType=\"Sale\"><OpData><MerchantInfo MerchantId=\"\" MerchantPassword=\"\" />" +
      "<ActionInfo><TrnxCommon TrnxID=\"\" Protocol=\"156\"><AmountInfo Amount=\"0\" Currency=\"792\" /></TrnxCommon><PaymentTypeInfo>" +
      "<InstallmentInfo NumberOfInstallments=\"0\" /></PaymentTypeInfo></ActionInfo><PANInfo PAN=\"\" ExpiryDate=\"\" CVV2=\"\" BrandID=\"\" />" +
      "<OrderInfo><OrderLine>0</OrderLine></OrderInfo><OrgTrnxInfo /><CustomData></CustomData><CardHolderIp></CardHolderIp></OpData></Operation></ePaymentMsg>");
      node = _msgTemplate.SelectSingleNode("//ePaymentMsg/Operation/OpData/MerchantInfo");
      node.Attributes["MerchantId"].Value = "006100200140200";
      node.Attributes["MerchantPassword"].Value = "123";
      node = _msgTemplate.SelectSingleNode("//ePaymentMsg/Operation/OpData/ActionInfo/TrnxCommon");
      node.Attributes["TrnxID"].Value = Guid.NewGuid().ToString();
      node = _msgTemplate.SelectSingleNode("//ePaymentMsg/Operation/OpData/ActionInfo/TrnxCommon/AmountInfo");
      string gonderilecekAmount = amount.ToString("####.00");
      gonderilecekAmount = gonderilecekAmount.Replace(",", ".");
      node.Attributes["Amount"].Value = gonderilecekAmount;
      node.Attributes["Currency"].Value = "949";
      node = _msgTemplate.SelectSingleNode("//ePaymentMsg/Operation/OpData/ActionInfo/PaymentTypeInfo/InstallmentInfo");
      node.Attributes["NumberOfInstallments"].Value = "0";
      node = _msgTemplate.SelectSingleNode("//ePaymentMsg/Operation/OpData/PANInfo");
      node.Attributes["PAN"].Value = "4022751585445574";
      node.Attributes["ExpiryDate"].Value = "201406";
      node.Attributes["CVV2"].Value = "408";
      node.Attributes["BrandID"].Value = "VISA";
      node = _msgTemplate.SelectSingleNode("//ePaymentMsg/Operation/OpData/CardHolderIp");
      node.InnerText = "10.20.30.40";
      request = _msgTemplate.OuterXml;
      return request;
    }
    catch (Exception ex)
    {
      throw ex;
    }
  }

public string Send(string request)
    {
        try
        {
            string postData = "";
            string responseData = "";
            System.Text.Encoding encoding = System.Text.Encoding.GetEncoding("ISO-8859-9");

            postData = "https://vpstest.bankasya.com.tr/iposnet/sposnet.aspx?prmstr=[DATA]";
            postData = postData.Replace("[DATA]", request);
            HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(postData);
            webReq.Timeout = Convert.ToInt32(20000);
            webReq.KeepAlive = false;
            webReq.ProtocolVersion = HttpVersion.Version10;
            WebResponse webResp = webReq.GetResponse();
            Stream respStream = webResp.GetResponseStream();

            byte[] buffer = new byte[10000];
            int len = 0, r = 1;
            while (r > 0)
            {
                r = respStream.Read(buffer, len, 10000 - len);
                len += r;
            }
            respStream.Close();
            responseData = encoding.GetString(buffer, 0, len).Replace("\r", "").Replace("\n", "");
            return responseData;
        }
        catch (System.Net.Sockets.SocketException ex)
        {
            throw ex;
        }
        catch (WebException ex)
        {
            if (ex.Status == WebExceptionStatus.ServerProtocolViolation)
            {
                Response.Write("Status Code : {0} " + ((HttpWebResponse)ex.Response).StatusCode);
                Response.Write("Status Description : {0} " + ((HttpWebResponse)ex.Response).StatusDescription);
            }
            throw ex;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

我试过了,

<httpRuntime maxRequestLength="409600" executionTimeout="900"/>

 <system.net>
   <settings>
     <httpWebRequest useUnsafeHeaderParsing="true" />
   </settings>
 </system.net>

webReq.KeepAlive = false;
webReq.ProtocolVersion = HttpVersion.Version10;
webReq.Timeout = 1000000000; 
webReq.ReadWriteTimeout = 1000000000; 

注意:代码中有测试信息。不是真正的价值观。

1 个答案:

答案 0 :(得分:0)

银行的WCF系统出错了。此代码现在正在运行。