Muaadeeb
asked on
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-ur lencoded";
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.GetS tring(resu lt));
}
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"
<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.
request.ContentType = "application/x-www-form-ur
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream()
dataStream.Write(byteArray
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
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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...
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...
ASKER
Thank you!
ASKER