Recent Posts

Thursday, December 10, 2009

How to post data to other site via "Post" in Asp.net?

If you need to post data to some other website or URL via post method then this article is the perfect solution.
This article references to the article provided in

I just followed the article and did it in vb. You can click on above link to get the c# code.

As per the above article




Possible Solutions

  1. One possible solution to this problem is to Create your own form control and use it on page this will allow you to change action of form, but again what if you do not want some existing input elements in current page to go to post.
  2. There is good way to post form data using HttpWebResponse & HttpWebRequest class if you want to post data behind the scenes, but if you want to post data using user browser then you are stuck

We have solved this by following way:

Create a RemotePost class (responsible for doing the job) as below:

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()

Dim i As Int16
'System.Web.HttpContext.Current.Response.Clear()
'System.Web.HttpContext.Current.Response.Write("")
'System.Web.HttpContext.Current.Response.Write(String.Format("", FormName))
'System.Web.HttpContext.Current.Response.Write(String.Format("", FormName, Method, URL))

System.Web.HttpContext.Current.Response.Clear()

System.Web.HttpContext.Current.Response.Write("")

System.Web.HttpContext.Current.Response.Write(String.Format(""))
System.Web.HttpContext.Current.Response.Write(String.Format("
"))

Dim str As String
For i = 0 To Inputs.Keys.Count - 1
str = Inputs.Keys(i)
str = Inputs(Inputs.Keys(i))
str = "input name="" type="" value=""" '(Note: put the <> tags)

System.Web.HttpContext.Current.Response.Write(String.Format(str))
Next
'System.Web.HttpContext.Current.Response.Write("")
'System.Web.HttpContext.Current.Response.Write("")

System.Web.HttpContext.Current.Response.Write("")
System.Web.HttpContext.Current.Response.Write("")

System.Web.HttpContext.Current.Response.End()

End Sub


End Class

Now The posting Page which has a linkbutton

asp:LinkButton id="lnkbtnSmartTickets" Runat="server" >test post< /asp:LinkButton

And the code looks like below:

Private Sub lnkbtnPostThis_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim myRemotePost As New RemotePost
myRemotePost.URL = "Receiver.aspx"
myRemotePost.Add("param1", "this is prateek 1")
myRemotePost.Add("param2", "this is regmi2")
myRemotePost.Post()

End Sub



Now Receiver.aspx will have following code on its form load

If Not Request.Form("param1") = Nothing Then
Response.Write("param1: " & Request.Form("param1"))
End If
If Not Request.Form("param2") = Nothing Then
Response.Write("param2: " & Request.Form("param2"))
End If







Related Posts by Categories




No comments:

Post a Comment