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?

Mehdi
  • 499
  • 1
  • 7
  • 31
Gigi2m02
  • 1,238
  • 3
  • 17
  • 33

3 Answers3

15

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

Community
  • 1
  • 1
Timmerz
  • 6,090
  • 5
  • 36
  • 49
  • Thx for your reply. But the byte data isn't really necessary. By calling the API url twitter gives back the location of the profilepicture. You can test it if you post the url 'https://api.twitter.com/1/users/profile_image?screen_name=twitterapi&size=bigger' in your browser. I would like to store the jpg location returned by the Twitter API – Gigi2m02 Mar 27 '12 at 14:15
7

You can also do it using HttpClient:

public class UriFetcher
{
    public Uri Get(string apiUri)
    {
        using (var httpClient = new HttpClient())
        {
            var httpResponseMessage = httpClient.GetAsync(apiUri).Result;
            return httpResponseMessage.RequestMessage.RequestUri;
        }
    }
}

[TestFixture]
public class UriFetcherTester
{
    [Test]
    public void Get()
    {
        var uriFetcher = new UriFetcher();
        var fetchedUri = uriFetcher.Get("https://api.twitter.com/1/users/profile_image?screen_name=twitterapi&size=bigger");
        Console.WriteLine(fetchedUri);
    }
}
David Peden
  • 17,596
  • 6
  • 52
  • 72
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"

dash
  • 89,546
  • 4
  • 51
  • 71