Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 365
Gigi2m02 Avatar asked Mar 27 '12 13:03

Gigi2m02


People also ask

How to get Parameter Value from url in ASP net c#?

Answers. Actually in C# it would be: //this takes request parameters only from the query string Request. QueryString["parameter1"]; //this one works for bot - form and query string Request["parameter1"];

What is the Response URI?

The ResponseUri property contains the URI of the Internet resource that actually responded to the request. This URI might not be the same as the originally requested URI, if the original server redirected the request. The ResponseUri property will use the Content-Location header if present.

What is request URL AbsoluteUri?

Url. AbsoluteUri is returning "http://www.somesite.com/default.aspx" , when the Url in the client's browser looks like "http://www.somesite.com/". This small diffrence is causing a redirect loop.


3 Answers

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

like image 171
Timmerz Avatar answered Oct 02 '22 21:10

Timmerz


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);
    }
}
like image 37
David Peden Avatar answered Oct 02 '22 23:10

David Peden


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"

like image 27
dash Avatar answered Oct 02 '22 23:10

dash