By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,921 Members | 1,479 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,921 IT Pros & Developers. It's quick & easy.

POST Data to Another Server

P: n/a
I am in a situation where I need to package some information from
Page1, submit it via POST to another server which will process the
information and then send the user to another page on my server
(Page2).
I have looked at HttpWebRequest on numerous coding websites but have
not found a method that works. Does anyone have a solution for this or
has seen an example that works for this situation?
This is one very frustrating issue that I have with ASP.NET--the
postback architecture gets wrapped around itself for something like
this.
Thanks,
Mike
Nov 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
someone wrote:
I am in a situation where I need to package some information from
Page1, submit it via POST to another server which will process the
information and then send the user to another page on my server
(Page2).
I have looked at HttpWebRequest on numerous coding websites but have
not found a method that works. Does anyone have a solution for this or
has seen an example that works for this situation?


While I don't hand a working sample at hand, what you want to do is

-- create a handler for your submit button, e.g. OnSubmit() in Page1's
code-behind
-- in ObSubmit(), retrieve all form data you need to post to the other web
site
-- post the form data to the other web site using HttpWebRequest
-- if the POST succeeds, redirect the user to Page2, or to an error page if
the POST failed.

Cheers,
--
Joerg Jooss
jo*********@gmx.net

Nov 18 '05 #2

P: n/a
Joerg,
I got the first couple of steps down cold. That kind of stuff I am
used to. Here is my Click handler for this code:

private void btnSubmit_Click(object sender, System.EventArgs
e)
{
string bname = tbName.Text;
string baddr1 = tbAddress.Text;
string bcity = tbCity.Text;
string bstate = ddlState.SelectedItem.Value;
string bzip = tbZip.Text;
string phone = tbPhone.Text;
string email = tbEMail.Text;
string cctype = ddlCCType.SelectedItem.Value;
string cardnumber = tbCCNumber.Text;
string expmonth = ddlExpMonth.SelectedItem.Value;
string expyear = ddlExpYear.SelectedItem.Value;
string subtotal = lblCost.Text.Substring(1);
string tax = lblTax.Text.Substring(1);
string chargetotal = lblTotal.Text.Substring(1);

string postData = "bname=" + Server.UrlEncode(bname);
postData += "&baddr1=" + Server.UrlEncode(baddr1);
postData += "&bcity=" + Server.UrlEncode(bcity);
postData += "&bzip=" + Server.UrlEncode(bzip);
postData += "&phone=" + Server.UrlEncode(phone);
postData += "&email=" + Server.UrlEncode(email);
postData += "&cctype=" + Server.UrlEncode(cctype);
postData += "&cardnumber=" + Server.UrlEncode(cardnumber);
postData += "&expmonth=" + Server.UrlEncode(expmonth);
postData += "&expyear=" + Server.UrlEncode(expyear);
postData += "&subtotal=" + Server.UrlEncode(subtotal);
postData += "&tax=" + Server.UrlEncode(tax);
postData += "&chargetotal=" +
Server.UrlEncode(chargetotal);
postData += "&storename=XXXXXXXXXX";
postData += "&mode=payplus";
postData += "&txnorg=eci";
postData += "&bcountry=US";
postData += "&2000=submit";

HttpWebRequest postReq = (HttpWebRequest)
WebRequest.Create("https://staging.linkpt.net/lpc/servlet/lppay");
postReq.Method = "POST";
postReq.AllowAutoRedirect = false;
postReq.ContentType = "application/x-www-form-encoded";
postReq.ContentLength = postData.Length;

byte[] buffer = Encoding.UTF8.GetBytes(postData);
using (Stream reqst = postReq.GetRequestStream())
{
reqst.Write(buffer, 0, buffer.Length);
}
}

Is there any way to just send the data to the receiving page and be
done with this one? Normally, I use an HTML page that POSTs the data
to the receiving server and obviously it goes away. The receiving
server is configured to come back to the next page in the order
process.
Thanks,
Mike

On Wed, 11 Aug 2004 08:15:36 +0200, "Joerg Jooss"
<jo*********@gmx.net> wrote:
someone wrote:
I am in a situation where I need to package some information from
Page1, submit it via POST to another server which will process the
information and then send the user to another page on my server
(Page2).
I have looked at HttpWebRequest on numerous coding websites but have
not found a method that works. Does anyone have a solution for this or
has seen an example that works for this situation?


While I don't hand a working sample at hand, what you want to do is

-- create a handler for your submit button, e.g. OnSubmit() in Page1's
code-behind
-- in ObSubmit(), retrieve all form data you need to post to the other web
site
-- post the form data to the other web site using HttpWebRequest
-- if the POST succeeds, redirect the user to Page2, or to an error page if
the POST failed.

Cheers,


Nov 18 '05 #3

P: n/a
someone wrote:
Joerg,
I got the first couple of steps down cold. That kind of stuff I am
used to. Here is my Click handler for this code:

private void btnSubmit_Click(object sender, System.EventArgs
e)
{
string bname = tbName.Text;
string baddr1 = tbAddress.Text;
string bcity = tbCity.Text;
string bstate = ddlState.SelectedItem.Value;
string bzip = tbZip.Text;
string phone = tbPhone.Text;
string email = tbEMail.Text;
string cctype = ddlCCType.SelectedItem.Value;
string cardnumber = tbCCNumber.Text;
string expmonth = ddlExpMonth.SelectedItem.Value;
string expyear = ddlExpYear.SelectedItem.Value;
string subtotal = lblCost.Text.Substring(1);
string tax = lblTax.Text.Substring(1);
string chargetotal = lblTotal.Text.Substring(1);

string postData = "bname=" + Server.UrlEncode(bname);
postData += "&baddr1=" + Server.UrlEncode(baddr1);
postData += "&bcity=" + Server.UrlEncode(bcity);
postData += "&bzip=" + Server.UrlEncode(bzip);
postData += "&phone=" + Server.UrlEncode(phone);
postData += "&email=" + Server.UrlEncode(email);
postData += "&cctype=" + Server.UrlEncode(cctype);
postData += "&cardnumber=" + Server.UrlEncode(cardnumber);
postData += "&expmonth=" + Server.UrlEncode(expmonth);
postData += "&expyear=" + Server.UrlEncode(expyear);
postData += "&subtotal=" + Server.UrlEncode(subtotal);
postData += "&tax=" + Server.UrlEncode(tax);
postData += "&chargetotal=" +
Server.UrlEncode(chargetotal);
postData += "&storename=XXXXXXXXXX";
postData += "&mode=payplus";
postData += "&txnorg=eci";
postData += "&bcountry=US";
postData += "&2000=submit";

HttpWebRequest postReq = (HttpWebRequest)
WebRequest.Create("https://staging.linkpt.net/lpc/servlet/lppay");
postReq.Method = "POST";
postReq.AllowAutoRedirect = false;
postReq.ContentType = "application/x-www-form-encoded";
postReq.ContentLength = postData.Length;

byte[] buffer = Encoding.UTF8.GetBytes(postData);
Note: There's a typical bug pattern here. Since you're using UTF-8,
non-ASCII characters will be encoded as two or more bytes. Thus, the content
length of your post data is potentially greater than the postData's string
length. You should set postReq.ContentLength to buffer.length.

In your case it won't matter, because all data is alreay URL-encoded and
thus contains only ASCII-characters -- which means that applying any
character encoding after URL-encoding is meaningless at best. You should
apply the character encoding together with URL-encoding (see
HttpUtility.UrlEncode(String, Encoding)).

using (Stream reqst = postReq.GetRequestStream())
{
reqst.Write(buffer, 0, buffer.Length);
}
}

Is there any way to just send the data to the receiving page and be
done with this one?


I hope your code doesn't end here, because you still have to get the
response:

using (HttpWebResponse response = (HttpWebResponse) postReq.GetResponse()) {
if (response.StatusCode != HttpStatusCode.OK) {
string message = String.Format(
"POST failed. Received HTTP {0}",
response.StatusCode);
throw new ApplicationException(message);
}
}

Cheers,

--
Joerg Jooss
jo*********@gmx.net

Nov 18 '05 #4

P: n/a
On Thu, 12 Aug 2004 10:02:34 +0200, "Joerg Jooss"
<jo*********@gmx.net> wrote:
someone wrote:
Joerg,
I got the first couple of steps down cold. That kind of stuff I am
used to. Here is my Click handler for this code:

private void btnSubmit_Click(object sender, System.EventArgs
e)
{
string bname = tbName.Text;
string baddr1 = tbAddress.Text;
string bcity = tbCity.Text;
string bstate = ddlState.SelectedItem.Value;
string bzip = tbZip.Text;
string phone = tbPhone.Text;
string email = tbEMail.Text;
string cctype = ddlCCType.SelectedItem.Value;
string cardnumber = tbCCNumber.Text;
string expmonth = ddlExpMonth.SelectedItem.Value;
string expyear = ddlExpYear.SelectedItem.Value;
string subtotal = lblCost.Text.Substring(1);
string tax = lblTax.Text.Substring(1);
string chargetotal = lblTotal.Text.Substring(1);

string postData = "bname=" + Server.UrlEncode(bname);
postData += "&baddr1=" + Server.UrlEncode(baddr1);
postData += "&bcity=" + Server.UrlEncode(bcity);
postData += "&bzip=" + Server.UrlEncode(bzip);
postData += "&phone=" + Server.UrlEncode(phone);
postData += "&email=" + Server.UrlEncode(email);
postData += "&cctype=" + Server.UrlEncode(cctype);
postData += "&cardnumber=" + Server.UrlEncode(cardnumber);
postData += "&expmonth=" + Server.UrlEncode(expmonth);
postData += "&expyear=" + Server.UrlEncode(expyear);
postData += "&subtotal=" + Server.UrlEncode(subtotal);
postData += "&tax=" + Server.UrlEncode(tax);
postData += "&chargetotal=" +
Server.UrlEncode(chargetotal);
postData += "&storename=XXXXXXXXXX";
postData += "&mode=payplus";
postData += "&txnorg=eci";
postData += "&bcountry=US";
postData += "&2000=submit";

HttpWebRequest postReq = (HttpWebRequest)
WebRequest.Create("https://staging.linkpt.net/lpc/servlet/lppay");
postReq.Method = "POST";
postReq.AllowAutoRedirect = false;
postReq.ContentType = "application/x-www-form-encoded";
postReq.ContentLength = postData.Length;

byte[] buffer = Encoding.UTF8.GetBytes(postData);


Note: There's a typical bug pattern here. Since you're using UTF-8,
non-ASCII characters will be encoded as two or more bytes. Thus, the content
length of your post data is potentially greater than the postData's string
length. You should set postReq.ContentLength to buffer.length.

In your case it won't matter, because all data is alreay URL-encoded and
thus contains only ASCII-characters -- which means that applying any
character encoding after URL-encoding is meaningless at best. You should
apply the character encoding together with URL-encoding (see
HttpUtility.UrlEncode(String, Encoding)).

using (Stream reqst = postReq.GetRequestStream())
{
reqst.Write(buffer, 0, buffer.Length);
}
}

Is there any way to just send the data to the receiving page and be
done with this one?


I hope your code doesn't end here, because you still have to get the
response:

using (HttpWebResponse response = (HttpWebResponse) postReq.GetResponse()) {
if (response.StatusCode != HttpStatusCode.OK) {
string message = String.Format(
"POST failed. Received HTTP {0}",
response.StatusCode);
throw new ApplicationException(message);
}
}

Cheers,


Joerg,
Thanks for the info on the UrlEncoding. Was following blindly from
someone else's code. What you say here makes sense.
The last comment as me a little confused. Do I have to wait and verify
response even if I need none? I will try your addition and see what is
going on.
Thanks,
Mike
Nov 18 '05 #5

P: n/a
On Fri, 13 Aug 2004 01:17:34 GMT, someone <so*****@somewhere.com>
wrote:
On Thu, 12 Aug 2004 10:02:34 +0200, "Joerg Jooss"
<jo*********@gmx.net> wrote:
someone wrote:
Joerg,
I got the first couple of steps down cold. That kind of stuff I am
used to. Here is my Click handler for this code:

private void btnSubmit_Click(object sender, System.EventArgs
e)
{
string bname = tbName.Text;
string baddr1 = tbAddress.Text;
string bcity = tbCity.Text;
string bstate = ddlState.SelectedItem.Value;
string bzip = tbZip.Text;
string phone = tbPhone.Text;
string email = tbEMail.Text;
string cctype = ddlCCType.SelectedItem.Value;
string cardnumber = tbCCNumber.Text;
string expmonth = ddlExpMonth.SelectedItem.Value;
string expyear = ddlExpYear.SelectedItem.Value;
string subtotal = lblCost.Text.Substring(1);
string tax = lblTax.Text.Substring(1);
string chargetotal = lblTotal.Text.Substring(1);

string postData = "bname=" + Server.UrlEncode(bname);
postData += "&baddr1=" + Server.UrlEncode(baddr1);
postData += "&bcity=" + Server.UrlEncode(bcity);
postData += "&bzip=" + Server.UrlEncode(bzip);
postData += "&phone=" + Server.UrlEncode(phone);
postData += "&email=" + Server.UrlEncode(email);
postData += "&cctype=" + Server.UrlEncode(cctype);
postData += "&cardnumber=" + Server.UrlEncode(cardnumber);
postData += "&expmonth=" + Server.UrlEncode(expmonth);
postData += "&expyear=" + Server.UrlEncode(expyear);
postData += "&subtotal=" + Server.UrlEncode(subtotal);
postData += "&tax=" + Server.UrlEncode(tax);
postData += "&chargetotal=" +
Server.UrlEncode(chargetotal);
postData += "&storename=XXXXXXXXXX";
postData += "&mode=payplus";
postData += "&txnorg=eci";
postData += "&bcountry=US";
postData += "&2000=submit";

HttpWebRequest postReq = (HttpWebRequest)
WebRequest.Create("https://staging.linkpt.net/lpc/servlet/lppay");
postReq.Method = "POST";
postReq.AllowAutoRedirect = false;
postReq.ContentType = "application/x-www-form-encoded";
postReq.ContentLength = postData.Length;

byte[] buffer = Encoding.UTF8.GetBytes(postData);


Note: There's a typical bug pattern here. Since you're using UTF-8,
non-ASCII characters will be encoded as two or more bytes. Thus, the content
length of your post data is potentially greater than the postData's string
length. You should set postReq.ContentLength to buffer.length.

In your case it won't matter, because all data is alreay URL-encoded and
thus contains only ASCII-characters -- which means that applying any
character encoding after URL-encoding is meaningless at best. You should
apply the character encoding together with URL-encoding (see
HttpUtility.UrlEncode(String, Encoding)).

using (Stream reqst = postReq.GetRequestStream())
{
reqst.Write(buffer, 0, buffer.Length);
}
}

Is there any way to just send the data to the receiving page and be
done with this one?


I hope your code doesn't end here, because you still have to get the
response:

using (HttpWebResponse response = (HttpWebResponse) postReq.GetResponse()) {
if (response.StatusCode != HttpStatusCode.OK) {
string message = String.Format(
"POST failed. Received HTTP {0}",
response.StatusCode);
throw new ApplicationException(message);
}
}

Cheers,


Joerg,
Thanks for the info on the UrlEncoding. Was following blindly from
someone else's code. What you say here makes sense.
The last comment as me a little confused. Do I have to wait and verify
response even if I need none? I will try your addition and see what is
going on.
Thanks,
Mike

Joerg,
I tried this and still doesn't work. I get no response back from the
code above that indicates any problems.
Regards,
Mike
Nov 18 '05 #6

P: n/a
someone wrote:
On Thu, 12 Aug 2004 10:02:34 +0200, "Joerg Jooss"
<jo*********@gmx.net> wrote:

[...]
I hope your code doesn't end here, because you still have to get the
response:

using (HttpWebResponse response = (HttpWebResponse)
postReq.GetResponse()) { if (response.StatusCode !=
HttpStatusCode.OK) { string message = String.Format(
"POST failed. Received HTTP {0}",
response.StatusCode);
throw new ApplicationException(message);
}
}

Cheers,


Joerg,
Thanks for the info on the UrlEncoding. Was following blindly from
someone else's code. What you say here makes sense.
The last comment as me a little confused. Do I have to wait and verify
response even if I need none? I will try your addition and see what is
going on.


HTTP is based on the notion of requests and responses. You cannot blindly
fire a request away in this model (technically yes, but you'll have no clue
what happened with your request). The code I posted checks the HTTP status
message without reading the reponse stream -- it's the minimum sanity check
you should do.

Cheers,

--
Joerg Jooss
jo*********@gmx.net

Nov 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.