Link to home
Start Free TrialLog in
Avatar of mattegol
mattegolFlag for Sweden

asked on

Submit form from code behind

Hi,

I want to submit from code behind instead of javascript. Using  HttpWebRequest.

Javascript that works:

function HandleIT() {

            //alert('post to page')
            var __siteCode = document.getElementById('<%=HiddenField.ClientID %>').value;

            var __login = document.getElementById('<%=UserName.ClientID %>').value;

            var __pass = document.getElementById('<%=Password.ClientID %>').value;

            var form = document.createElement('FORM');
            form.action = 'https://testexample.com';
            form.method='post';
            document.body.appendChild(form);
            var fld1 = document.createElement('INPUT');
            fld1.name = 'SITE';
            fld1.value = __siteCode ;
            form.appendChild(fld1);
            var fld2 = document.createElement('INPUT');
            fld2.name = 'LOGINNAME';
            fld2.value = __login ;
            form.appendChild(fld2);
            var fld3 = document.createElement('INPUT');
            fld3.name = 'PASSWORD';
            fld3.value = __pass ;
            form.appendChild(fld3);

            document.forms[1].submit()
            
        }

Open in new window


This is what I tried to mimic and want to work in code behind:

// build post variables
                    ASCIIEncoding encoding = new ASCIIEncoding();
                    string postData = "SITE=" + HttpUtility.UrlEncode(__siteCode);
                    postData += "&LOGINNAME=" + HttpUtility.UrlEncode(__login);
                    postData += "&PASSWORD=" + HttpUtility.UrlEncode(__pass);
                    byte[] data = encoding.GetBytes(postData);

                    // Prepare web request...
                    HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("https://testexample.com");
                    myRequest.Method = "POST";
                    myRequest.ContentType = "application/x-www-form-urlencoded";
                    myRequest.ContentLength = data.Length;
                    Stream newStream = myRequest.GetRequestStream();
                    // Send the data.
                    newStream.Write(data, 0, data.Length);
                    newStream.Close();

                    Response.Redirect("https://testexample.com");

Open in new window



The Response.Redirect() is that part necessary? Or can the form be submitted and sent to the new page anyway?
Avatar of drypz
drypz
Flag of Philippines image

Hi,

You may use Response.Redirect or Server.Transfer to submit a page. For more details, please look at this Server.Transfer vs Response.Redirect.

Another way is to call a javascript method in your codebehind. Something like this
ScriptManager.RegisterStartupScript(this, GetType(), "submit", "document.forms[1].submit();", true);

Open in new window

Avatar of mattegol

ASKER

Hi,

I must use Response.Redirect because its not on the same server.

The javascript solution is the one that I have had success with already.

Its the part below that does not work

// build post variables
                    ASCIIEncoding encoding = new ASCIIEncoding();
                    string postData = "SITE=" + HttpUtility.UrlEncode(__siteCode);
                    postData += "&LOGINNAME=" + HttpUtility.UrlEncode(__login);
                    postData += "&PASSWORD=" + HttpUtility.UrlEncode(__pass);
                    byte[] data = encoding.GetBytes(postData);

                    // Prepare web request...
                    HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("https://testexample.com");
                    myRequest.Method = "POST";
                    myRequest.ContentType = "application/x-www-form-urlencoded";
                    myRequest.ContentLength = data.Length;
                    Stream newStream = myRequest.GetRequestStream();
                    // Send the data.
                    newStream.Write(data, 0, data.Length);
                    newStream.Close();

                    Response.Redirect("https://testexample.com");

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Hans Langer
Hans Langer

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial