Search code examples
c#asp.nettwitter

How to get the url response values in Asp.NET


I almost dare to ask, but how can i get the response data of a URL? I just can't remember anymore.

My scenario: I'm using the twitter API to get the profile picture of an user. That API URL returns the JPEG location. So if I actually write this HTML in my views:

<img src="https://api.twitter.com/1/users/profile_image?screen_name=twitterapi&size=bigger"/> 

The browser auto uses the response JPEG for the SRC property. Like this:

Now is my question very simple: how can I get that .jpg location in C# to put in my database?


Solution

  • I'm not exactly sure what you are asking.

    I think you can use WebClient.DownloadData in c# to call that url. Once you download the file, you can then place it in the database.

    byte[] response = new System.Net.WebClient().DownloadData(url);
    

    Download a file over HTTP into a byte array in C#?

    EDIT: THIS IS WORKING FOR ME

    WebRequest request = WebRequest.Create("https://api.twitter.com/1/users/profile_image?screen_name=twitterapi&size=bigger");
    WebResponse response = request.GetResponse();
    Console.WriteLine(response.ResponseUri);
    
    Console.Read( );
    

    from A way to figure out redirection URL

    EDIT: THIS IS ANOTHER METHOD I THINK...using show.json from Read the absolute redirected SRC attribute URL for an image

    http://api.twitter.com/1/users/show.json?screen_name=twitterapi