string url="http://www.contoso.com/default.html"; string requestData = ""; string RequestMethod = "POST"; protected void Page_Load(object sender, EventArgs e) { try { /* Create a request using a URL that can receive a post*/ HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); /*set header*/ request.Method = RequestMethod; //request.UserAgent="Mozilla/5.0"; //request.Accept=""; //request.Connection=""; //request.ContentLength=requestData.Length; //request.ContentType="application/x-www-form-urlencoded"; //request.Date=""; //request.Expect=""; //request.Host=""; //request.Referer=""; //request.TransferEncoding=""; //request.Proxy=null; //request.Timeout=""; //request.Credentials = CredentialCache.DefaultCredentials; //********************* Byte[] postDataByte = Encoding.ASCII.GetBytes(requestData); /* Get the request stream.*/ Stream requestStream = request.GetRequestStream(); /*Write the data to the request stream*/ requestStream.Write(postDataByte, 0, postDataByte.Length); /*Close the Stream object*/ requestStream.Close(); /*To send the request to the server, call GetResponse*/ HttpWebResponse response = (HttpWebResponse)request.GetResponse(); /*Get Some Respone info*/ Response.Write(((HttpWebResponse)response).StatusDescription); Response.Write(((HttpWebResponse)response).Headers); /*Get the stream containing content returned by the server*/ Stream dataStream = response.GetResponseStream(); /*Open the stream using a StreamReader for easy access*/ StreamReader reader = new StreamReader(dataStream); /* Read the content.*/ string responseFromServer = reader.ReadToEnd(); /*Display the content.*/ Response.Write(responseFromServer); /*Clean up the streams*/ reader.Close(); dataStream.Close(); response.Close(); } catch (Exception) { Response.Write("An error occured"); } }
Byte[] postDataByte = Encoding.ASCII.GetBytes(requestData); /* Get the request stream.*/ Stream requestStream = request.GetRequestStream(); /*Write the data to the request stream*/ requestStream.Write(postDataByte, 0, postDataByte.Length); /*Close the Stream object*/ requestStream.Close();
Quote:The GetRequestStream method initiates a request to send data to the Internet resource and returns a Stream instance for sending data to the Internet resource. The GetRequestStream method provides synchronous access to the Stream. For asynchronous access, use the BeginGetRequestStream and EndGetRequestStream methods.
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)