[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

ASP.net C# streamreader

Posted on 2011-10-26
1
Medium Priority
?
690 Views
Last Modified: 2012-05-12
Hi experts,

I am trying to build an asp.net website that uses the result of an API call (see the attached code) and I have come across two issues that I hope someone can help with.

(1)I have a piece of code where I load into a streamreader the result of the call to an API. I am struggling to display the contents of that streamreader in my asp.net page. I have only managed to output the entire string, but I would like to be able to show different substrings of that string in ASP.NET objects such as labels, update panels, etc...So the first questio is: How can I break up the streamreader to show in these ASP.Net objects?

(2)Additionally, this is a call to an streaming API, which means that it keeps updating continuously. The additional question would then be: What ASP.NET object would be best suited to include the results of my streaming API call? My first assumption is it would be some AJAX control, with the update panel, etc..

Thanks a lot in advance and regards.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Net;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

        string url = "https://stream.twitter.com/1/statuses/filter.json?track=bilbao";
        WebRequest request = WebRequest.Create(url);
        request.Credentials = new NetworkCredential("","");
        var webResponse = request.GetResponse();
        Encoding encode = Encoding.GetEncoding("utf-8");
        var responseStream = new StreamReader(webResponse.GetResponseStream(), encode);

            Response.write(responseStream.ReadLine());


        }
    }
}

Open in new window

0
Comment
Question by:robespierre_2010
1 Comment
 
LVL 3

Accepted Solution

by:
DaveKeyes earned 2000 total points
ID: 37036707
Here is a little console app I wrote to do what you are trying to do, you will need to fill in the NetworkCredentials, when I tried your URL, I got the invalid credentials, but was successful when I hit a public web site.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security;
using System.Text;
using System.Web;

namespace NetReader
    {
    class NetReaderClass
        {
        static void Main(string[] args)
            {
            try
                {
                string strUrl = "https://stream.twitter.com/1/statuses/filter.json?track=bilbao";

                if (args.Length > 0)
                    {
                    strUrl = args[0];
                    }

                NetReaderClass objNet = new NetReaderClass();

                objNet.ReadDataFromUrl(strUrl);
                }

            catch (Exception exp)
                {
                string strError;

                strError = String.Format("{0} {1} {2}",
                                         "Main",
                                         "experience an error:",
                                         exp.Message);

                Console.WriteLine(strError);
                }

            finally
                {
                }
            }

        private void ReadDataFromUrl(string strUrl)
            {
            StreamReader strmReader = null;

            try
                {
                Uri uriAddress = new Uri(strUrl);

                StringBuilder sbData = new StringBuilder();

                CredentialCache objCache = new CredentialCache();
                objCache.Add(uriAddress, "Basic", new NetworkCredential("", "", ""));

                HttpWebRequest objReq = WebRequest.Create(uriAddress) as HttpWebRequest;

                    // Set type to POST

                objReq.Method      = "POST";
                objReq.ContentType = "application/x-www-form-urlencoded";
                objReq.Credentials = objCache;

                    // Create a byte array of the data we want to send

                byte[] byteData = UTF8Encoding.UTF8.GetBytes("");

                    // Set the content length in the request headers

                objReq.ContentLength = byteData.Length;

                    // Write data

                using (Stream strmPost = objReq.GetRequestStream())
                    {
                    strmPost.Write(byteData, 0, byteData.Length);
                    }

                    // Get response

                using (HttpWebResponse objResponse = objReq.GetResponse() as HttpWebResponse)
                    {
                        // Get the response stream

                    strmReader = new StreamReader(objResponse.GetResponseStream());

                        // Console application output

                    sbData.AppendLine(strmReader.ReadToEnd());

                    if (strmReader != null)
                        {
                        strmReader.Close();
                        strmReader = null;
                        }
                    }

                Console.WriteLine(sbData);
                }

            catch (Exception exp)
                {
                string strError;

                strError = String.Format("{0} {1} {2}",
                                         "ReadDataFromUrl",
                                         "experience an error:",
                                         exp.Message);

                throw new ApplicationException(strError);
                }

            finally
                {
                if (strmReader != null)
                    {
                    strmReader.Close();
                    strmReader = null;
                    }
                }
            }
        }
    }

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month18 days, 16 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question