Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在Asp.NET中获取url响应值_C#_Asp.net_Twitter - Fatal编程技术网

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

C# 如何在Asp.NET中获取url响应值,c#,asp.net,twitter,C#,Asp.net,Twitter,我几乎不敢问,但我如何才能获得URL的响应数据? 我再也记不起来了 我的场景:我正在使用twitter API获取用户的个人资料图片。该API URL返回JPEG位置。 因此,如果我真的在视图中编写此HTML: <img src="https://api.twitter.com/1/users/profile_image?screen_name=twitterapi&size=bigger"/> 浏览器自动将响应JPEG用于SRC属性。像这样: 现在我的问题很简单:

我几乎不敢问,但我如何才能获得URL的响应数据? 我再也记不起来了

我的场景:我正在使用twitter API获取用户的个人资料图片。该API URL返回JPEG位置。 因此,如果我真的在视图中编写此HTML:

<img src="https://api.twitter.com/1/users/profile_image?screen_name=twitterapi&size=bigger"/> 

浏览器自动将响应JPEG用于SRC属性。像这样:


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

我不太清楚你在问什么

我想你可以用c语言中的
WebClient.DownloadData
来调用这个url。下载文件后,可以将其放入数据库中

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

编辑:这对我有用

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( );

编辑:我认为这是另一种方法……使用来自的show.json


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

您可以使用HttpWebRequest和HttpWebResponse类(通过
使用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现在包含字符串
“https://si0.twimg.com/profile_images/1438634086/avatar_bigger.png“

谢谢您的回复。但是字节数据并不是真正必要的。通过调用API url,twitter将返回profilepicture的位置。如果在浏览器中发布url“”,则可以对其进行测试。我想存储Twitter APINice答案返回的jpg位置+1作为一个简单的例子和一个测试:-)Thx也是你的答案,但我觉得使用Timmerz代码更舒服。注意:你需要使用System.Net.Http-.Net framework 4.5或更高版本。为什么你敢:)…这是一个非常愚蠢的问题!