locked
new web request post RRS feed

  • Question

  • User944339287 posted

    Hi all, I wanna rewrite code for posting data to an online payment provider's url.

    The old code is working fine but the new code returned the following error. 
    An existing connection was forcibly closed by the remote host

    Old Code (RemotePost.vb)

    Imports System.Web
    Imports System.Collections.Specialized
    Imports System.Net
    
    Partial Public Class RemotePost
        Private inputValues As NameValueCollection
    
        Public Sub Add(ByVal name As String, ByVal value As String)
            inputValues.Add(name, value)
        End Sub
    
        Public Sub Post()
            Dim context = HttpContext.Current
            context.Response.Clear()
            context.Response.Write("<html><head>")
            context.Response.Write(String.Format("</head><body onload=""document.{0}.submit()"">", "ePayment"))
            context.Response.Write(String.Format("<form name=""{0}"" method=""{1}"" action=""{2}"" >", "ePayment", "post", "URL given by Online Payment Provider"))
            For i As Integer = 0 To inputValues.Keys.Count - 1
                context.Response.Write(String.Format("<input name=""{0}"" type=""hidden"" value=""{1}"">", HttpUtility.HtmlEncode(inputValues.Keys(i)), HttpUtility.HtmlEncode(inputValues(inputValues.Keys(i)))))
            Next
            context.Response.Write("</form>")
            context.Response.Write("</body></html>")
            context.Response.[End]()
        End Sub
    
    End Class
    

    New Code (WebRequestPost.vb)

    Imports System
    Imports System.IO
    Imports System.Net
    Imports System.Text
    
    Imports System.Web
    Imports System.Collections.Specialized
    
    Partial Public Class WebRequestPost
        Private inputValues As NameValueCollection
    
        Dim payment As New DBpayment
        Dim DT As New DataTable
    
        Public Sub Add(ByVal name As String, ByVal value As String)
            inputValues.Add(name, value)
        End Sub
    
        Public Sub Post()
    
            ' Create a request using a URL that can receive a post.
            Dim request As WebRequest = WebRequest.Create("URL given by Payment Gateway Provider")
            ' Set the Method property of the request to POST.
            request.Method = "POST"
    
            ' Create POST data and convert it to a byte array.
            Dim postData As String = ""
            For i As Integer = 0 To inputValues.Keys.Count - 1
                postData += String.Format("""{0}"" value=""{1}""", HttpUtility.HtmlEncode(inputValues.Keys(i)), HttpUtility.HtmlEncode(inputValues(inputValues.Keys(i))))
            Next
    
            Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
    
            ' Set the ContentType property of the WebRequest.
            request.ContentType = "application/x-www-form-urlencoded"
    
            ' Set the ContentLength property of the WebRequest.
            request.ContentLength = byteArray.Length
    
            ' Get the request stream.
            Dim dataStream As Stream = request.GetRequestStream()
    
            ' Write the data to the request stream.
            dataStream.Write(byteArray, 0, byteArray.Length)
    
            ' Close the Stream object.
            dataStream.Close()
            ' Get the response.
    
            Dim response As WebResponse = request.GetResponse()
            ' Display the status.
            Console.WriteLine(CType(response, HttpWebResponse).StatusDescription)
    
            ' Get the stream containing content returned by the server.
            dataStream = response.GetResponseStream()
    
            ' Open the stream using a StreamReader for easy access.
            Dim reader As New StreamReader(dataStream)
    
            ' Read the content.
            Dim responseFromServer As String = reader.ReadToEnd()
    
            ' Display the content.
            Console.WriteLine(responseFromServer)
    
            reader.Close()
            dataStream.Close()
            response.Close()
    
        End Sub
    
    End Class

    This is my payment function

        Private Sub payment_submit
    
            Dim TransactionType As String = "SALE"
            Dim PymtMethod As String = "ANY"
    
            Dim myPost As New WebRequestPost
            myPost.Add("TransactionType", TransactionType)
            myPost.Add("PymtMethod", PymtMethod)
            
            myPost.Post()
    
        End Sub







    Wednesday, April 29, 2020 3:58 AM

All replies

  • User944339287 posted

    Dear all,

    I have revised my code but still getting the following error message

    An existing connection was forcibly closed by the remote host



        Public Sub Post()
    
            Dim remoteUrl As String = "https://abc.com/test/Payment.aspx"
            Dim encoding As ASCIIEncoding = New ASCIIEncoding
    
            'Dim data As String = String.Format("FirstName={0}&LastName={1}", firstName, lastName)
    
            ' Create POST data and convert it to a byte array.
            Dim postData As String
            For i As Integer = 0 To inputValues.Keys.Count - 1
                postData += String.Format("{0}={1}&", HttpUtility.HtmlEncode(inputValues.Keys(i)), HttpUtility.HtmlEncode(inputValues(inputValues.Keys(i))))
            Next
    
            Dim bytes() As Byte = encoding.GetBytes(postData)
            Dim httpRequest As HttpWebRequest = CType(WebRequest.Create(remoteUrl), HttpWebRequest)
            httpRequest.Method = "POST"
            httpRequest.ContentType = "application/x-www-form-urlencoded"
            httpRequest.ContentLength = bytes.Length()
            Dim stream As Stream = httpRequest.GetRequestStream
            stream.Write(bytes, 0, bytes.Length)
            stream.Close()
    
        End Sub





    Wednesday, April 29, 2020 10:02 AM
  • User1120430333 posted

    Hi all, I wanna rewrite code for posting data to an online payment provider's url.

    You should talk to the provider about your issues.

    Wednesday, April 29, 2020 5:05 PM
  • User753101303 posted

    Hi,

    The main difference is that you are posting from the server rather than from the client. Which .NET version do you use? It means that something explicitely refused the connection such as a firewall or maybe a wrong TLS version (if using .NET 4.7 or later it should be fine). Are you supposed to be able to send http requests to other sites from this server?

    Wednesday, April 29, 2020 6:38 PM
  • User944339287 posted

    Hi, i'm using framework 4.0

    Yes. I am able to send HTTP request to other sites from this server (localhost)

    Friday, May 1, 2020 6:13 AM
  • User753101303 posted

    Try 

    ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;

    to force TLS 1.2 (I expect most if not all payments gateway to require this protocol and assuming you checked already this is not a firewall issue). Taken from https://stackoverflow.com/questions/33761919/tls-1-2-in-net-framework-4-0

    If the full exception details (rather than just the error message) doesn't give more clue my next step would be to enable network tracing using https://docs.microsoft.com/en-us/dotnet/framework/network-programming/how-to-configure-network-tracing

    Sunday, May 3, 2020 1:21 PM
  • User944339287 posted

    Hi, nothing happened even I've added the link provided.

    Wednesday, June 3, 2020 6:03 AM
  • User753101303 posted

    I understand this is not the actual url but rather than trying to post to  https://abc.com/test/Payment.aspx I would start by trying to get https://www.google.com/ and see what happens.

    If you have the same message it's likely that this server doesn't allow access to internet sites. See support for your hosting provider.

    If it works it could be rather that you are blocked by the payment gateway for some reason. See support on their side for possible condition. They expect a referrer or whatever ???

    Wednesday, June 3, 2020 7:30 PM