Link to home
Create AccountLog in
Avatar of mayhem_07
mayhem_07

asked on

Posting form data from ASP.NET to CGI

I have a ASP.NET application (VB.net) and am trying to post data to another website to allow direct login and open in a new window.
Have tried the PostBackURL property of the button but it wont post the data, i'm guessing that has to do with the PreviousPage property.

Secondly i need to run code behind to dynamically change the url it's posting to depending on the login information entered.

I have also created another page to run the code below as it throws an error when run on the ajax enabled master page, but it's not giving the required result either.
Private Inputs As System.Collections.Specialized.NameValueCollection = New System.Collections.Specialized.NameValueCollection
    Public URL As String
    Public Method As String
    Public FormName As String
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        URL = Session.Item("URL")
        URL = "https://app.bluetie.com/cgi-bin/login.cgi"
 
        Method = Session.Item("Method")
        Method = "Post"
 
        FormName = Session.Item("FormName")
        FormName = "Form5"
 
        Add("Password", "testpwd")
        Add("User", "testuser")
        Post()
    End Sub
 
    Public Sub Add(ByVal Name As String, ByVal Value As String)
        Inputs.Add(Name, Value)
    End Sub
 
    Public Sub Post()
        Try
            Response.ClearContent()
            Response.Clear()
            Response.Write("<html><head>")
 
            Response.Write(String.Format("", FormName))
            Response.Write(String.Format("</head><body onload=" & Chr(34) & Chr(34) & "document.{0}.submit()" & Chr(34) & Chr(34) & ">", FormName))
            System.Web.HttpContext.Current.Response.Write(String.Format("<form name=" & Chr(34) & Chr(34) & "{0}" & Chr(34) & Chr(34) & " method=" & Chr(34) & Chr(34) & "{1}" & Chr(34) & Chr(34) & " action=" & Chr(34) & Chr(34) & "{2}" & Chr(34) & Chr(34) & " >", FormName, Method, URL))
            Dim i As Int32
            For i = 0 To Inputs.Keys.Count - 1
                Response.Write(String.Format("<input name=" & Chr(34) & Chr(34) & "{0}" & Chr(34) & Chr(34) & " type=" & Chr(34) & Chr(34) & "hidden" & Chr(34) & Chr(34) & " value=" & Chr(34) & Chr(34) & "{1}" & Chr(34) & Chr(34) & ">", Inputs.Keys(i), Inputs(Inputs.Keys(i))))
            Next
            System.Web.HttpContext.Current.Response.Write("</form>")
            System.Web.HttpContext.Current.Response.Write("</body></html>")
            System.Web.HttpContext.Current.Response.End()
 
        Catch ex As Exception
            'Web.HttpContext.Current.Response.Clear()
            'Web.HttpContext.Current.Response.Write(ex.Message)
 
        End Try
 
    End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of zzzxtreme
zzzxtreme

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of mayhem_07
mayhem_07

ASKER

I get the following script error when trying to run it with ajax:
Char:1
Error: Syntax Error
Code:0
URL:http://localhost:1174/website1/

when i put the second lot into a blank aspx page it returns this script error:
Line: 1
Char 269
Error: 'document.body' is null or not an object
Code: 0
URL: http://localhost:1174/website1/acc.aspx
I tried running it with the script I already had and it worked like a charm, just needs some fine tuning.

Thanks.
hi ok that's odd
instead of document.body.appendChild

could you try document.getElementsByTagName('form')[0].parentNode.appendChild