How to send XML file or data to url using httpWebRequest and httpWebResponse?

I need to send the XML data I receive from the database to a URL like ... http://www.rentals.com/aspx .. using ASP.NET

+1


a source to share


1 answer


you can write a function like this:

    private string SendRequest(Uri UriObj, string data)
    {
        string _result;

        var request = (HttpWebRequest) WebRequest.Create(UriObj);
        request.Method = "POST";
        request.ContentType = "text/xml";
        var writer = new StreamWriter(request.GetRequestStream());
        writer.Write(data);
        writer.Close();

        var response = (HttpWebResponse) request.GetResponse();

        var streamResponse = response.GetResponseStream();
        var streamRead = new StreamReader(streamResponse);

        _result = streamRead.ReadToEnd().Trim();
        streamRead.Close();
        streamResponse.Close();
        response.Close();
        return _result;
    }

      



The string data can be XML like "<xmla><..></..></xmla>"

in the .aspx page to get the data, you need to use Request.InputStream

and read the stream into a string, XML, etc.

+5


a source







All Articles