WebRequest webRequest = null;
HttpWebResponse webResponse = null;
try
{
string uri = Request.Url.ToString().Substring(0, Request.Url.ToString().LastIndexOf("/")) + "/Post.aspx";
webRequest = WebRequest.Create(txtLink.Text);
//webRequest.Proxy = WebProxy.GetDefaultProxy(); // Enable if using proxy
webRequest.Method = "POST"; // Post method
webRequest.ContentType = "text/xml"; // content type
//Wrap the request stream with a text-based writer
var writer = new StreamWriter(webRequest.GetRequestStream());
writer.WriteLine(InitializeXml());
writer.Close();
//read the response
webResponse = webRequest.GetResponse() as HttpWebResponse;
var reader = new StreamReader(webResponse.GetResponseStream());
var reply = reader.ReadToEnd();
reader.Close();
//load the reply in an xml node
var xmlDocument = new XmlDocument();
xmlDocument.LoadXml(reply);
}
catch (WebException we)
{
}
if (!IsPostBack)
{
// Read XML posted via HTTP
var reader = new StreamReader(Request.InputStream);
var xmlData = reader.ReadToEnd();
reader.Close();
reader.Dispose();
var xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xmlData);
//you can do something with the xmlData here
//Response
Response.ContentType = "text/xml";
Response.ContentEncoding = Encoding.UTF8;
var writer = new StreamWriter(Response.OutputStream);
writer.Write(xmlTextHere);
writer.Flush();
writer.Close();
writer.Dispose();
public static string HttpPost(String url)
{
// Create a request using a URL that can receive a post.
var request = WebRequest.Create(url);
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
//const string postData = "This is a test that posts this string to a Web server.";
//var byteArray = Encoding.UTF8.GetBytes(postData);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Set the ContentLength property of the WebRequest.
//request.ContentLength = byteArray.Length;
// Get the request stream.
var dataStream = request.GetRequestStream();
// Write the data to the request stream.
//dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
// Get the response.
var response = request.GetResponse();
// Display the status.
//Console.WriteLine(((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
var reader = new StreamReader(dataStream);
// Read the content.
var responseFromServer = reader.ReadToEnd();
// Display the content.
//Console.WriteLine(responseFromServer);
// Clean up the streams.
reader.Close();
if (dataStream != null) dataStream.Close();
response.Close();
responseFromServer = "";
return responseFromServer;
}