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")
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.
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
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
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
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.
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:
Member
6 Points
56 Posts
2 http posts at the same time
May 25, 2020 07:51 PM|DavidLee|LINK
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:
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
All-Star
53131 Points
23681 Posts
Re: 2 http posts at the same time
May 25, 2020 08:08 PM|mgebhard|LINK
Contributor
2120 Points
675 Posts
Re: 2 http posts at the same time
May 27, 2020 07:55 AM|XuDong Peng|LINK
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
Test.aspx.vb
Hope this can help you.
Best regards,
Xudong Peng
All-Star
48570 Points
18086 Posts
Re: 2 http posts at the same time
May 27, 2020 10:11 AM|PatriceSc|LINK
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 ???)
Member
6 Points
56 Posts
Re: 2 http posts at the same time
May 27, 2020 04:41 PM|DavidLee|LINK
Hi,
Here it is:
All-Star
53131 Points
23681 Posts
Re: 2 http posts at the same time
May 27, 2020 05:45 PM|mgebhard|LINK
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.
Member
6 Points
56 Posts
Re: 2 http posts at the same time
May 27, 2020 06:11 PM|DavidLee|LINK
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
All-Star
53131 Points
23681 Posts
Re: 2 http posts at the same time
May 27, 2020 06:21 PM|mgebhard|LINK
Use the HttpClient if you are doing the request from C# as suggested above.
https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient?view=netcore-3.1
https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client
Member
6 Points
56 Posts
Re: 2 http posts at the same time
May 28, 2020 02:57 PM|DavidLee|LINK
posted in error