none
Urgent: Read data thats returned to a URL. RRS feed

  • Question

  • The problem is simple and I cant figure out how to read it:

    Mine is a C# program - I post some data to a website using HTTP POST (httpwebrequest).

    The website that I pose to, is not controlled by me, and they say they will return the answer to a URL accessible to me. How do I capture the URL and read its data. Just q querystring wont work.

    Its simple, please dont post any basic links as I have tried them out all.

    Tuesday, May 7, 2013 2:26 AM

Answers

  • using System;
    using System.IO;
    using System.Net;
    
    namespace ConsoleApplication2
    {
        class Program
        {
            static void Main(string[] args)
            {
                // create request
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("[URL]");
                request.Method = "PUT";
                request.AllowAutoRedirect = true;
                string result = string.Empty;
    
                // get response
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                try
                {
                    // get response stream
                    StreamReader streamReader = new StreamReader(response.GetResponseStream(), true);
                    try
                    {
                        // read data from response stream
                        result = streamReader.ReadToEnd();
    
                        // print result
                        Console.WriteLine("ResponseUri = " + response.ResponseUri);
                        Console.WriteLine("Data = " + result);
                    }
                    finally
                    {
                        streamReader.Close();
                    }
                }
                finally
                {
                    response.Close();
                }
    
                Console.ReadKey();     
            }
        }
    }
    


    Tuesday, May 7, 2013 3:37 AM

All replies

  • You simply need to expose the hidden properties of the httpwebrequest by casting the object to httpwebresponse.  See webpage below

    http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.aspx

    The webpage below contains a more complete example which shows how to get the response

    http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.getresponsestream.aspx


    jdweng

    Tuesday, May 7, 2013 3:02 AM
  • using System;
    using System.IO;
    using System.Net;
    
    namespace ConsoleApplication2
    {
        class Program
        {
            static void Main(string[] args)
            {
                // create request
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("[URL]");
                request.Method = "PUT";
                request.AllowAutoRedirect = true;
                string result = string.Empty;
    
                // get response
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                try
                {
                    // get response stream
                    StreamReader streamReader = new StreamReader(response.GetResponseStream(), true);
                    try
                    {
                        // read data from response stream
                        result = streamReader.ReadToEnd();
    
                        // print result
                        Console.WriteLine("ResponseUri = " + response.ResponseUri);
                        Console.WriteLine("Data = " + result);
                    }
                    finally
                    {
                        streamReader.Close();
                    }
                }
                finally
                {
                    response.Close();
                }
    
                Console.ReadKey();     
            }
        }
    }
    


    Tuesday, May 7, 2013 3:37 AM