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?
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"];
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.
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.
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
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);
}
}
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With