Sending and receiving Xml through Http post
Tech-Today

Sending and receiving Xml through Http post


Usually, it's easier done on dotnet webservice but there are times when your client wants to do this using Http Post.

So I've created 2 test pages, one that will request and with for a response while the other will accept the request and return a response.

1.) Request Page (Request.aspx)
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)
{

}

2.) Accepts the request (Post.aspx.cs)
Note: It is very important to clean the content of the Post.aspx page, all the html tags except <%@ Page Language=...
So, Post.aspx has a single line of code and everything is in Post.aspx.cs
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();


Here's how to return a string:
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;
}




- How To Download A File In Angular2
There are 2 ways I use to download a file with Angular2 or greater. For this example, we will use a Java REST service. The first approach would be taking advantage of the HTTP download, where the Angular side will just call a URL that will initiate the...

- Creating Your First Enterprise Application Project On Eclipse-jee-helios
First you should create an ejb project as specified here: http://czetsuya-tech.blogspot.com/2011/03/creating-your-first-ejb3-project-in.html Take note that my interface were inside the ejb project. You can pull that out and place in its own project ipielEJB2Client....

- Rss Feed Reader Implementation In C#
Included in this page: 1.) Rss feed model (which you can customized according to your needs) 2.) Rss feed reader My Objective is to read an rss feed of a news site like google and yahoo: For example we want to read the RSS feed of yahoo news in the Philippines:...

- Download Image Given A Url In C# Using The System.net.httpwebrequest Class
Saves image url object into the local machine /// /// Saves the list of url object into the output directory /// /// list of url path /// root directory that will be used for saving internal static void PhotoDownloader(ArrayList aList, string outputDir)...

- Google Map Api - An Example Implementation In C#. Converts An Array Of String Addresses Into Longtitude, Latitude Pair
Objective: -To create a web application that will use the google map api to produce longtitude, latitude locations. The application will read string address values from an excel document. So it's also an excel reader. What you need: 1.) Visual Studio...



Tech-Today








.