C# 如何在 Asp.NET 中获取 url 响应值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9890886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 11:05:44  来源:igfitidea点击:

How to get the url response values in Asp.NET

c#asp.nettwitter

提问by Gigi2m02

I almost dare to ask, but how can i get the response data of a URL? I just can't remember anymore.

我几乎敢问,但我怎样才能得到一个 URL 的响应数据呢?我只是想不起来了。

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:

我的场景:我使用 twitter API 来获取用户的个人资料图片。该 API URL 返回 JPEG 位置。所以如果我真的在我的视图中写了这个 HTML:

<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:

浏览器自动使用响应 JPEG 作为 SRC 属性。像这样:

Now is my question very simple: how can I get that .jpg location in C# to put in my database?

现在我的问题很简单:如何将 C# 中的 .jpg 位置放入我的数据库中?

采纳答案by Timmerz

I'm not exactly sure what you are asking.

我不确定你在问什么。

I think you can use WebClient.DownloadDatain c# to call that url. Once you download the file, you can then place it in the database.

我认为您可以WebClient.DownloadData在 c# 中使用来调用该 url。下载文件后,您可以将其放入数据库中。

byte[] response = new System.Net.WebClient().DownloadData(url);

Download a file over HTTP into a byte array in C#?

通过 HTTP 将文件下载到 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

一种找出重定向 URL 的方法

EDIT: THIS IS ANOTHER METHOD I THINK...using show.json from Read the absolute redirected SRC attribute URL for an image

编辑:这是我认为的另一种方法......使用 show.json 来自Read the absolute redirected SRC attribute URL for an image

http://api.twitter.com/1/users/show.json?screen_name=twitterapi

http://api.twitter.com/1/users/show.json?screen_name=twitterapi

回答by David Peden

You can also do it using HttpClient:

您也可以使用 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);
    }
}

回答by dash

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

您可以使用 HttpWebRequest 和 HttpWebResponse 类(通过using System.Net)来实现这一点;

  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"

url 现在包含字符串 "https://si0.twimg.com/profile_images/1438634086/avatar_bigger.png"