locked
2 http posts at the same time RRS feed

  • Question

  • User2070009070 posted

    Hi,

    Our website has a contact form.  When a user hits submit, it does an http post to a central "contact handling form" we have on another server.  That form gets the post, sees which website it's from, sees that it's a contact, adds the customer information to our leads database, and redirects the user back to the "thanks for contacting us" page.

    Recently, we started using AWeber, and I would like to do 2 http posts at the same time, one to our central form and one to AWeber.

    Separately, they both work but once I try to run them both on the button click (one remote post and then the other) it fails.  Maybe because it's "leaving" our site briefly to go to the central contact form?  Or AWeber?

    Here's an example of the code:

        Protected Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
    
            If Page.IsValid Then
    
                Dim myRemotePost As remotePost = New remotePost
    
                myRemotePost.url = "https://contact.ourdomain.com/OurContactHandlerPage.asp"
                myRemotePost.Add("FirstName", firstname_.Text)
                myRemotePost.Add("LastName", lastname_.Text)
                myRemotePost.Add("Company", txtCompany.Text)
                myRemotePost.Add("State", ddlStates.SelectedValue)
                myRemotePost.Add("Country", ddlCountry.SelectedItem.Value)
                myRemotePost.Add("email", email_.Text)
                myRemotePost.Add("Phone", phone_.Text)
                myRemotePost.Add("InterestedIn", ddlInterestedIn.SelectedItem.Value.ToString)
    
                myRemotePost.Add("Manufacturer", "Manufacturer")
                myRemotePost.Add("Website", "www.ourdomain.ca")
                myRemotePost.Add("LeadType", "Contact")
    
                myRemotePost.Add("comments", "Contact Request: " & Environment.NewLine & Environment.NewLine & message_.Text)
    
                myRemotePost.Post()
    
    
    
                ' Then, By Post to AWeber's API ===========================
    
                Dim myAWeberPost As remotePost = New remotePost With {.url = "https://www.aweber.com/scripts/addlead.pl"}
                myAWeberPost.Add("name", firstname_.Text & " " & lastname_.Text)
                myAWeberPost.Add("Company", txtCompany.Text)
                myAWeberPost.Add("State", ddlStates.SelectedValue)
                myAWeberPost.Add("Country", ddlCountry.SelectedItem.Value)
                myAWeberPost.Add("email", email_.Text)
                myAWeberPost.Add("Phone", phone_.Text)
    
                ' Hidden fields
                myAWeberPost.Add("meta_web_form_id", "123456")
                myAWeberPost.Add("listname", "awlist123456")
                myAWeberPost.Add("meta_adtracking", "Our_Test_Contact_Form")
                myAWeberPost.Add("meta_message", "1")
    
                myAWeberPost.Add("redirect", "https://www.ourdomain.ca/contact_thanks.aspx")
    
                myAWeberPost.Post()
    
    
            End If
    
    
    
        End Sub

    When I comment either one of the "remote posts" out, the other works. 

    "RemotePost" refers to a script that just creates and does the post - accepting variables of url, individual fields etc.

    If any of the experts on here have ideas I'd appreciate it.  (obviously all the domains are changed to "ourdomain.com")

    Thanks

    David

    Monday, May 25, 2020 7:51 PM

Answers

  • User-939850651 posted

    Hi, DavidLee

    Normally, we could send the 2 http post request at on button click event. Since you don’t provide the details source codes about the remotePost and the details error message, we couldn’t find the right reason and solution.

    If possible, please post more details codes and error message about the  remotePost.

    Besides, if  you want to make two post requests in one event, you could choose  HttpClient class to achieve this requirement.

    More details, you could refer to below codes:

    Test.aspx

    <form id="form1" runat="server">
            <div>
                <asp:Button ID="submitBtn" OnClick="submitBtn_Click" runat="server" Text="submit" />
            </div>
        </form>
    

    Test.aspx.vb

    Public Partial Class Test
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        End Sub
    
        Protected Sub submitBtn_Click(ByVal sender As Object, ByVal e As EventArgs)
            PostToFirstAsync()
        End Sub
    
        Public Shared Async Function PostToFirstAsync() As System.Threading.Tasks.Task
            Dim url = "https://localhost:44322/PostFirst.aspx"
            Dim client = New HttpClient()
            Dim data As StringContent = New StringContent("ABCD", Encoding.UTF8, "application/json")
            Dim response = client.PostAsync(url, data)
    
            If response.Result.IsSuccessStatusCode Then
                PostToSecondAsync()
            End If
        End Function
    
        Public Shared Async Function PostToSecondAsync() As System.Threading.Tasks.Task
            Dim url = "https://localhost:44322/PostSecond.aspx"
            Dim client = New HttpClient()
            Dim data As StringContent = New StringContent("EFGH", Encoding.UTF8, "application/json")
            Dim response = client.PostAsync(url, data)
    
            If response.Result.IsSuccessStatusCode Then
                HttpContext.Current.Response.Write("<script>alert('Second Page load success')</script>")
            End If
        End Function
    End Class
    

    Hope this can help you.

    Best regards,

    Xudong Peng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 27, 2020 7:55 AM

All replies

  • User475983607 posted
    You should be able to make two post requests. What is the error or unexpected result?
    Monday, May 25, 2020 8:08 PM
  • User-939850651 posted

    Hi, DavidLee

    Normally, we could send the 2 http post request at on button click event. Since you don’t provide the details source codes about the remotePost and the details error message, we couldn’t find the right reason and solution.

    If possible, please post more details codes and error message about the  remotePost.

    Besides, if  you want to make two post requests in one event, you could choose  HttpClient class to achieve this requirement.

    More details, you could refer to below codes:

    Test.aspx

    <form id="form1" runat="server">
            <div>
                <asp:Button ID="submitBtn" OnClick="submitBtn_Click" runat="server" Text="submit" />
            </div>
        </form>
    

    Test.aspx.vb

    Public Partial Class Test
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        End Sub
    
        Protected Sub submitBtn_Click(ByVal sender As Object, ByVal e As EventArgs)
            PostToFirstAsync()
        End Sub
    
        Public Shared Async Function PostToFirstAsync() As System.Threading.Tasks.Task
            Dim url = "https://localhost:44322/PostFirst.aspx"
            Dim client = New HttpClient()
            Dim data As StringContent = New StringContent("ABCD", Encoding.UTF8, "application/json")
            Dim response = client.PostAsync(url, data)
    
            If response.Result.IsSuccessStatusCode Then
                PostToSecondAsync()
            End If
        End Function
    
        Public Shared Async Function PostToSecondAsync() As System.Threading.Tasks.Task
            Dim url = "https://localhost:44322/PostSecond.aspx"
            Dim client = New HttpClient()
            Dim data As StringContent = New StringContent("EFGH", Encoding.UTF8, "application/json")
            Dim response = client.PostAsync(url, data)
    
            If response.Result.IsSuccessStatusCode Then
                HttpContext.Current.Response.Write("<script>alert('Second Page load success')</script>")
            End If
        End Function
    End Class
    

    Hope this can help you.

    Best regards,

    Xudong Peng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 27, 2020 7:55 AM
  • User753101303 posted

    Hi,

    Maybe your remotePost does something special from the Post method that prevents the 2nd one to run ? Show maybe the code for the Post method.

    As pointed already you have built-in classes that could handle that (doing something in Post was the purpose of creating this remotePost class ???)

    Wednesday, May 27, 2020 10:11 AM
  • User2070009070 posted

    Hi,

    Here it is:

    Public Class remotePost
    
        Private Inputs as System.Collections.Specialized.NameValueCollection = New System.Collections.Specialized.NameValueCollection
    
        Public url as String=""
        Public method as String = "post"
        Public formName as String="form1"
        
        Public sub Add(ByVal name as String, byVal value as String)
            Inputs.Add(name, value)
            
        End Sub
    
        Public sub Post()
            System.Web.HttpContext.Current.Response.Clear()
            System.Web.HttpContext.Current.Response.Write("<html><head>")
            System.Web.HttpContext.Current.Response.Write(String.Format("</head><body onload=""document.{0}.submit()"">", FormName))
            System.Web.HttpContext.Current.Response.Write(String.Format("<form name=""{0}"" method=""{1}"" action=""{2}"">", FormName, Method, Url))
    
            Dim I as Integer=0
            
            Do While i < Inputs.Keys.Count
                system.Web.HttpContext.Current.Response.Write(string.Format("<input name=""{0}"" type=""hidden"" value=""{1}"">", inputs.Keys(i), Inputs (Inputs.Keys(i))))
                i += 1
            Loop
            
            System.Web.HttpContext.Current.Response.Write("</form>")
    
            System.Web.HttpContext.Current.Response.Write("</body></html>")
    
            System.Web.HttpContext.Current.Response.End()
    
        End Sub
    
    End Class

    Wednesday, May 27, 2020 4:41 PM
  • User475983607 posted

    The browser can only submit one HTML form at a time.  Code, either JavaScript or C#, can submit two different HTTP requests.  You'll need to rethink the design.

    Wednesday, May 27, 2020 5:45 PM
  • User2070009070 posted

    Hi, and thanks for that excellent explaination!

    That makes sense - when I got the application I'm working on, the original develper was using the remotepost.vb to run posts on all the websites.  It's basically just a simple http post of multiple values from textboxes.

    Your way is much simpler - just do the post yourself in the same code!

    Now, I just have to figure out how to make this string "ABCD":

    Dim data As StringContent = New StringContent("ABCD", Encoding.UTF8, "application/json")

    into a post for several different fields like the ones below:

    myRemotePost.Add("FirstName", firstname_.Text)
    myRemotePost.Add("LastName", lastname_.Text)
    myRemotePost.Add("Company", txtCompany.Text)

    Basically a group of about 7 variables like above to post.

    I appreciate your assistance.

    Best regards,

    David

    Wednesday, May 27, 2020 6:11 PM
  • User2070009070 posted

    posted in error

    Thursday, May 28, 2020 2:57 PM