FORM POST in Code behind - Unable to connect to remote server

Posted on 2012-08-22
Last Modified: 2012-08-23
Here is the problem:

I need to be able to send a form post with data to a remote server from the code behind.  I build two possible solutions in the code behind but both fail for the same reason.  I then tested by building an actual Form Post page in (ASPX) and the post works great.  Am I missing something here or is there another way I can post this code from the code behind?  

The business case is that I my page is being hit via an internal business hyperlink from the company internal site and I need to redirect this page to a remote server/page passing the POST information.  I do not want to have the users see this page and then click the submit button.  I just want the page to be accessed then automatically redirect without any physical interaction on the part of the user.  The POST activity will do the redirect - and works great from the HTML side...but if I try to convert it into the code does not work.

The error message I am getting in the code behind is:

{"Unable to connect to the remote server"}
{"No connection could be made because the target machine actively refused it}

The Working HTML code is:
    <form id="form1" runat="server" method="post" action="">
        <input name="em"    id="em" size="25" value=""/> = em = Email<br/>
        <input name ="ut"   id="ut" size="25" value="SM"/> = ut = User Type <br/>
        <input name ="fid"  id="fid" size="25" value="252"/> = fid = Dept Number <br/>
        <input name ="fn"   id="fn" value="LiveFirstName"/> = fn = First Name <br/>
        <input name ="ln"   id="ln" value="LiveLastName"/> = ln = Last Name <br/>
        <input name ="ph"   id="ph" value="100-200-3000"/> = ph = PHone <br/>
        <input type="submit" value="login"/>

The code behind that is failing is:

StringBuilder sb = new StringBuilder();


Version #1:
WebRequest request = WebRequest.Create("");
request.ContentType = "text/xml";  // new
request.Method = "POST";
byte[] byteArray = Encoding.UTF8.GetBytes(sb.ToString());
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();  // [[IT GETs THE ERROR MESSAGE HERE]].
dataStream.Write(byteArray, 0, byteArray.Length);
WebResponse response = request.GetResponse();

Version #2:
This returns the EXACT SAME ERROR as #1
using (var client = new WebClient())
    var values = new NameValueCollection
        { "key", "" }
   string url = "";
   byte[] result = client.UploadValues(url, values); // [[IT GETs THE ERROR MESSAGE HERE]].
Question by:Muaadeeb
    LVL 19

    Accepted Solution

    Your problem description mades me think that there's something missing on your trials. Probably a header or some attribute that makes your request to be refused from the server.

    Anyway... given that what you are seeking is an automatic redirection and you can send the data succesfully from a HTML page... Why don't you try to just send that page automatically by firing your form submit event through javascript on page load?

    Author Comment

    I'll give that a shot and let you know what happens.  I was thinking about going this approach.

    Author Comment

    This did work - but I have one more follwo up question.  Using this method the Form data appears on the screen for 0-3 seconds while the page is being re-directed to the POST Website URL.  Is there a way to hide the values on the screen and still have the data be posted?

    Currenly when I mark the form visible = false it returns the error:
    "Microsoft JScript runtime error: 'document.forms.0' is null or not an object"

    I thought about placing a bunch of </br> to force the form data to be at the bottom of the page where normal users would not see - but I was thinking there has to be a better way...

    Author Closing Comment

    Thank you!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
    Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
    In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
    The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now