0

I'm trying to get the url of a tumblr user's avatar . The problem is, what I get back is not the url, but the image itself. This is the link from which I should get the url:

api.tumblr.com/v2/blog/{blog-identifier}/avatar[/size]

This is how I tried to get the url of the avatar. Note : "item" here is a blog (eg: blog.tumblr.com)

WebRequest req = WebRequest.Create("http://api.tumblr.com/v2/blog/" + item + "/avatar/");
WebResponse res = req.GetResponse();
StreamReader rd = new StreamReader(res.GetResponseStream(),Encoding.ASCII);
var end = rd.ReadToEnd();

I read an response on a forum which said :

the /avatar route redirects you to the avatar url.

Don't follow the redirect and pull the url out of the Location header or the body of the request, which contains the same url.

But I don't know how to do that, can you help me ? There are some answers to some similar questions to mine, but those are for a different programming language..

Garnyatar
  • 163
  • 1
  • 4
  • 10
  • 1
    Possible duplicate of [Tumblr API get current AVATAR URL](http://stackoverflow.com/questions/6289236/tumblr-api-get-current-avatar-url) – Mr. T Mar 16 '16 at 17:32
  • @Mr.T that answer is for php. As specified in my title I'm having trouble doing this in C#. – Garnyatar Mar 16 '16 at 17:35

1 Answers1

1
WebRequest req = WebRequest.Create("http://api.tumblr.com/v2/blog/" + item + "/avatar/");
            WebResponse res = req.GetResponse();
            string avatarUrl = res.ResponseUri.ToString();
Nikki9696
  • 6,260
  • 1
  • 28
  • 23
  • Thank you so much, that solved my problem. Can you explain me how this works ? What does ResponseUri do exactly ? – Garnyatar Mar 17 '16 at 12:01
  • It gets that information from the response. If you request the blog avatar url using Chrome and have the devtools open, you can look at the headers. It shows up in those. That's how you get the url before it redirects you to the actual image http://stackoverflow.com/questions/690587/using-webclient-in-c-sharp-is-there-a-way-to-get-the-url-of-a-site-after-being-r – Nikki9696 Mar 17 '16 at 13:51
  • Basically it uses the location header https://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.responseuri(v=vs.110).aspx – Nikki9696 Mar 17 '16 at 13:53