---
title: "Make a post to URL and get the response"  
description: "Make a post to URL and get the response"  
author: "Anonymous User"  
published: 2014-11-03  
updated: 2014-11-03  
canonical: https://www.mindstick.com/forum/2495/make-a-post-to-url-and-get-the-response  
category: "asp.net"  
tags: ["c#", "http post"]  
reading_time: 2 minutes  

---

# Make a post to URL and get the response

I try to [post](https://www.mindstick.com/articles/12829/aspects-that-make-australia-post-shipping-extension-a-useful-tool) an [XML file](https://www.mindstick.com/forum/360/how-to-read-xml-file-in-php) to the [url](https://www.mindstick.com/forum/436/url-redirection) and get the [response](https://www.mindstick.com/forum/12719/how-to-make-response-write-display-special-character-like-lt-gt) back. I have this [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) to post. I am not really sure how to [check if](https://www.mindstick.com/forum/12878/how-to-check-if-an-asp-dot-net-file-upload-control-has-a-file-in-jquery) it is [posting](https://www.mindstick.com/forum/34719/cross-page-posting) correctly and how to get the response.

```
WebRequest req = null;
            WebResponse rsp = null;
          //  try
          //  {
                string fileName = @"C:\ApplicantApproved.xml";
                string uri = "http://stage.test.com/partners/wp/ajax/consumeXML.php";
                req = WebRequest.Create(uri);

                req.Method = "POST";        // Post method
                req.ContentType = "text/xml; encoding='utf-8'";

                // Wrap the request stream with a text-based writer
                StreamWriter writer = new StreamWriter(req.GetRequestStream());
                // Write the XML text into the stream
                writer.WriteLine(this.GetTextFromXMLFile(fileName));
                writer.Close();
                // Send the data to the webserver
                rsp = req.GetResponse();
```

I think I should have the response in rsp but I am not seeing anything usufull on it.

## Replies

### Reply by Lillian Martin

Please try following.\

```
WebRequest req = null;
string fileName = @"C:\ApplicantApproved.xml";
string uri = "http://stage.test.com/partners/wp/ajax/consumeXML.php";
req = WebRequest.Create(uri);

req.Method = "POST";        // Post method
req.ContentType = "text/xml; encoding='utf-8'";

// Write the XML text into the stream
byte[] byteArray = Encoding.UTF8.GetBytes(this.GetTextFromXMLFile(fileName));

// Set the ContentLength property of the WebRequest.
req.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = req.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();

WebResponse response = req.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.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
Console.WriteLine(responseFromServer);
// Clean up the streams.
reader.Close();
dataStream.Close();
response.Close();
```

\
\


---

Original Source: https://www.mindstick.com/forum/2495/make-a-post-to-url-and-get-the-response

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
