Alex Rivera | Logout

How to get the url response values in Asp.NET

Asked 2012-03-27T13:50:05.273
9

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?

Edit
Report

1 Answer

2

You can use the HttpWebRequest and HttpWebResponse classes (via using System.Net)to achieve this;

  HttpWebRequest webRequest =
    WebRequest.Create("https://api.twitter.com/1/users/profile_image?screen_name=twitterapi&size=bigger") as HttpWebRequest;

  webRequest.Credentials = CredentialCache.DefaultCredentials;

  HttpWebResponse response = webRequest.GetResponse() as HttpWebResponse;

  string url = response.ResponseUri.OriginalString;

url now contains the string "https://si0.twimg.com/profile_images/1438634086/avatar_bigger.png"

answered 2012-03-27T14:38:36.697

Your Answer