post programmatically

P

Peter

Hi

I want to programmatically perform a post. Can some one please give me
some pointers to which classes I need to use to achieve this?

The form which is normally posted from the website looks like the
following, but I want to do it programmatically.


<form action="https://intl.payments/recdpay" method="POST"
target="_top">
<input type="hidden" name="merchantID" value="xx7223.189" />
<input type="hidden" name="orderID" value="UK_Z0000287" />
<input type="hidden" name="amount" value="59995" />
<input type="hidden" name="currencyCode" value="GBP" />
<input type="hidden" name="cardType" value="6" />
<input type="submit" value="Pay" />
</form>



Thanks,
Peter
 
P

Pavel Minaev

Peter said:
Hi

I want to programmatically perform a post. Can some one please give me
some pointers to which classes I need to use to achieve this?

The form which is normally posted from the website looks like the
following, but I want to do it programmatically.

HttpWebRequest should do the trick, just don't forget to set its Method to
"POST". Values of form controls go into the request body, which is just a
stream for WebRequest, so you'll need to format it yourself. W3C HTML 4.01
spec has detailed description of the format:

http://www.w3.org/TR/html4/interact/forms.html#h-17.13
 
S

Sin Jeong-hun

Hi

I want to programmatically perform a post. Can some one please give me
some pointers to which classes I need to use to achieve this?

The form which is normally posted from the website looks like the
following, but I want to do it programmatically.

<form action="https://intl.payments/recdpay" method="POST"
target="_top">
<input type="hidden" name="merchantID" value="xx7223.189" />
<input type="hidden" name="orderID" value="UK_Z0000287" />
<input type="hidden" name="amount" value="59995" />
<input type="hidden" name="currencyCode" value="GBP" />
<input type="hidden" name="cardType" value="6" />
<input type="submit" value="Pay" />
</form>

Thanks,
Peter

As Pavel Minaev said, HttpWebRequest would be good enough. But
WebRequest is also good, I think. Basically, as far as I know,
HttpWebRequest is a superset of WebRequest. You can find many samples
for it, even the .NET documentation had a section about how to POST a
request at the How do I section.

To save your time, I will write a quick code for you, may contain some
errors, but you can start it from there.

WebRequest req = WebReqest.Create("https://....");
req.Method="POST"; <-Default is GET, so need to specify "POST"
req.ContentType="application/x-www-form-urlencoded"; <-This is
required because there are other types of forms like multipart or
something.
Stream s = req.GetRequestStream();
string data="merchantID=xx7223.189&orderID....";
byte[] binaryData=Encoding.UTF8.GetBytes(data); <-Supposing the page's
encoding is UTF-8. Need to change it in other cases.
s.Write(binaryData,0,binaryData.Length);
s.close();
req.ContentLength=binaryData.Length;
WebResponse res =req.GetResponse();
 
P

Pavel Minaev

As Pavel Minaev said, HttpWebRequest would be good enough. But
WebRequest is also good, I think. Basically, as far as I know,
HttpWebRequest is a superset of WebRequest. You can find many samples
for it, even the .NET documentation had a section about how to POST a
request at the How do I section.

WebRequest is an abstract class, HttpWebRequest is one specific
implementation.

It is generally advised to use WebRequest.Create() factory method to
instantiate any implementation of WebRequest. But it is explicitly allowed
to cast the return value of WebRequest.Create() to the specific type you
expect to get.

In this case though, I've missed the fact that WebRequest has property
Method already (strange, since it's really HTTP-specific, and cannot be
properly used in a generic fashion anyway), so no need for cast.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top