Click here to Skip to main content
15,920,603 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi guys! I have a problem, which I can't solve 2 day. :((

I need to send data in POST request:
1. to Microsoft Web browser
2. to system default browser

Help me, please: info, code line, whatever. Thanx!
Posted
Comments
AspDotNetDev 14-Sep-10 23:26pm    
Browsers don't accept data sent from an external sources... they send the server a request and it responds with data. Perhaps if you explain what you are trying to accomplish, we can better serve you. And include some code or something that shows you've at least done some work of your own.

To create a POST request you have to use the HTTPWebRequest class. The Request method has to be set to POST. You have to define the content type (text, xml or anything based on your requirement) and finally write the data that you need to send to the server in the stream of the Request object.
Refer the code below -

You have to write your data that you need to send in the message body of your POST request in the str string. This can be c# class, or simple string separated by '&' as per your need.

C#
// Convert string data into byte array 
byte[] dataByte = Encoding.UTF8.GetBytes(strData);
            
            HttpWebRequest POSTRequest = (HttpWebRequest)WebRequest.Create(strURL);
            POSTRequest.Method = "POST";
// Set the content type - Mine was xml.
            POSTRequest.ContentType = "text/xml";
            POSTRequest.KeepAlive = false;
            POSTRequest.Timeout = 5000;
            POSTRequest.ContentLength = dataByte.Length;
// Get the request stream
            Stream POSTstream = POSTRequest.GetRequestStream();
            // Write the data bytes in the request stream
            POSTstream.Write(dataByte, 0, dataByte.Length); 

//Get response from server
            HttpWebResponse POSTResponse = (HttpWebResponse)POSTRequest.GetResponse();


Hope this helps
 
Share this answer
 
v2
Comments
Dalek Dave 15-Sep-10 3:52am    
Code Blocks added.
Here's a tutorial that might help:

W3Schools Forms Lesson[^]. The POST method isn't defined by Microsoft; it's a universal standard and works the same in any browser. Browsers POST data to servers, and servers reply with a new page. The POST method of passing data is more secure than GET, and doesn't have the length limitation of a GET. Spend an hour reading and you'll find that it's easier than it first sounds. Good luck! :-D
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900