Register  |  Login




Advertisement

Start Your Own Q&A Site

Create your own Q&A site easily, allowing you to quickly grow a new community around any subject matter or generate new organic traffic for your existing website.

Question

Status: Closed Points: 75 Time: 09:51 - Jul 19, 2006  

theDude

Create a post request programmatically in ASP.NET?

How can i create a post request programmatically in asp.net (C#) without having to actually submit an html-form? Is it possible?

Categories

Answer Discussion
Tutorials

 

PeterNZ

Date:: Jul 19, 2006

Time:: 21:56

Hi,

I am not sure what exactly you want to do. A post request always submits the web form. Do you want to use Ajax style programming where you don't submit the webform but you have a request - response to the web server? Do you use ASP.NET? Java? JavaScript?

In a nutshell?

You use a XMLHttpRequest object in a Java script on your page. You do for example an XMLHttpRequestObject.open("GET", "http://localhost/somewhere/some_data.txt); which returns the content of the text file. This is really just scratching the surface!! There are books about Ajax and the Microsoft adoption ATLAS. I suggest for a start to get the book "Ajax for Dummies" (Yes, experts are allowed to read Dummie books, too) by Steve Holzner (ISBN 0471785970)

Let me know if you need to know more.

Cheers

Peter

theDude

Date:: Jul 20, 2006

Time:: 23:18

Hi Peter,

What I wanted to do was using ASP.NET (C#) submitting a form serverside, i.e. without actually creating an htm page and submitting that.
I found the answer and ended up with the following code:

string url = "http://websiteToSubmitTo";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
string proxy = null;

string data = String.Format("parameter1={0}&parameter2={1}&parameter3={2}", parameter1, parameter2, parameter3);
byte[] buffer = Encoding.UTF8.GetBytes(data);

req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = buffer.Length;
req.Proxy = new WebProxy(proxy, true); // ignore for local addresses
req.CookieContainer = new CookieContainer(); // enable cookies

Stream reqst = req.GetRequestStream(); // add form data to request stream
reqst.Write(buffer, 0, buffer.Length);
reqst.Flush();
reqst.Close();

HttpWebResponse res = (HttpWebResponse)req.GetResponse();
   
Stream resst = res.GetResponseStream();
StreamReader sr = new StreamReader(resst);
string response = sr.ReadToEnd();

It's not so complicated after all.

theDude

PeterNZ

Date:: Jul 20, 2006

Time:: 23:48

Hey man, now I see what you wanted. Sorry that my answer wasn't helpful.

I don't know if you want to share this with us, but what actually is the requirement behind this? Just out of interest!

Cheers

Peter

theDude

Date:: Jul 20, 2006

Time:: 23:58

It was the implementation of a payment gateway.
It's called nochex (nochex.co.uk) and similar to Paypal it requires that you verify "callbacks" by returning the parameters from a transaction to them and they then respond with an authorization.

acollings

Date:: Sep 17, 2007

Time:: 19:12

Any way of using this code in a windows application and getting the result to display in a browser?

Question Answered

This question has been closed, and points have been rewarded to the following experts:


theDude: 75

You're welcome however to comment or give additional information or if you wish, you have the ability to write a Tutorial in the Tutorial Area.

Answer this Question

New User

Email:

Upon submission of this form, you will automatically be registered as a Quomon user and we will send your login information to this address

Registered User

Username:

Password:

Forgot Your Password?

No tutorials have been submitted yet. Want to be the first?

Answer this Question

New User

Email:

Upon submission of this form, you will automatically be registered as a Quomon user and we will send your login information to this address

Registered User

Username:

Password:

Forgot Your Password?

Ask a Question

Have a new question? Ask!

You have 100 characters to use



Top Experts

View More

Rank

Expert

Points

1.

nidhi

10354

2.

oracleofDelphi

6493

3.

rcastagna

5596

4.

LAGM

4848

5.

PeterNZ

3487

6.

gonzalo

2840

7.

Mason

2770

8.

jgivoni

2303

9.

xarcus

1820

10.

Anpanman

917

Become an Expert

Register today to share your knowledge with the community and be recognized and rewarded for your contributions.


Register Here




"Psst, Quomon is a great site. Pass it on."     Tell a Friend  |   Link To Us  |   Save to Delicious  |   Digg! Digg it



Language Options

English:

www.quomon.com

Español:

www.quomon.es