Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1063
  • Last Modified:

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

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 behind....it 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="https://xxxx.xxxxxx.com/gavilon.asp">
        <input name="em"    id="em" size="25" value="Ima.Test@xxxxx.com"/> = 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"/>
    </form>

The code behind that is failing is:

StringBuilder sb = new StringBuilder();

sb.Append("&ln=");
sb.Append(ln);
sb.Append("&fn=");
sb.Append(fn);
sb.Append("&ph=");
sb.Append(ph);
sb.Append("&em=");
sb.Append(em);
sb.Append("&co=");
sb.Append(co);
sb.Append("&ad1=");
sb.Append(ad1);
sb.Append("&ct=");
sb.Append(ct);
sb.Append("&st=");
sb.Append(st);
sb.Append("&zip=");
sb.Append(zip);



Version #1:
WebRequest request = WebRequest.Create("https://xxxx.xxxxxx.com/xxxxx.asp");
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);
dataStream.Close();
WebResponse response = request.GetResponse();


Version #2:
This returns the EXACT SAME ERROR as #1
using (var client = new WebClient())
{
    var values = new NameValueCollection
   {
        { "key", "em=Ima.Test@Gavilon.com" }
    };
   string url = "https://xxxx.xxxxxx.com/xxxx.asp";
   byte[] result = client.UploadValues(url, values); // [[IT GETs THE ERROR MESSAGE HERE]].
   Console.WriteLine(Encoding.UTF8.GetString(result));
}
0
Muaadeeb
Asked:
Muaadeeb
  • 3
1 Solution
 
BardobraveCommented:
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?
0
 
MuaadeebAuthor Commented:
I'll give that a shot and let you know what happens.  I was thinking about going this approach.
0
 
MuaadeebAuthor Commented:
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...
0
 
MuaadeebAuthor Commented:
Thank you!
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now