.Net C#:从 HttpWebResponse 读取附件

发布于 2024-08-28 04:39:34 字数 492 浏览 5 评论 0原文

是否可以从 System.Net.HttpWebResponse 读取图像附件?

我有一个 java 页面的 url,它生成图像。

当我在 Firefox 中打开该 url 时,会出现下载对话框。内容类型为 application/png。
似乎有效。

当我在 c# 中尝试此操作并发出 GET 请求时,我检索内容类型:text/html 并且没有内容处置标头。

简单代码:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

response.GetResponseStream() 为空。

用java尝试一下,成功了。

我需要准备网络请求还是其他东西吗?

Is it possible to read an image attachment from System.Net.HttpWebResponse?

I have a url to a java page, which generates images.

When I open the url in firefox, the download dialog appears. Content-type is application/png.
Seems to work.

When I try this in c#, and make a GET request I retrieve the content-type: text/html and no content-disposition header.

Simple Code:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

response.GetResponseStream() is empty.

A try with java was successful.

Do I have to prepare webrequest or something else?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

叹沉浮 2024-09-04 04:39:34

您可能需要设置 User-Agent 标头。

运行 Fiddler 并比较请求。

You probably need to set a User-Agent header.

Run Fiddler and compare the requests.

谁把谁当真 2024-09-04 04:39:34

UserAgent 中编写一些内容HttpWebRequest 的属性 在很多情况下确实会产生影响。 Web 服务的常见做法似乎是忽略带有空 UserAgent 的请求。
请参阅:网站管理员:空用户代理的解释

只需将 UserAgent 属性设置为非空字符串。例如,您可以使用应用程序的名称、程序集信息、模拟 common UserAgent,或其他标识。

示例:

request.UserAgent = "my example program v1";
request.UserAgent = $"{System.Reflection.Assembly.GetExecutingAssembly().GetName().Name.ToString()} v{System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString()}";
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36";

仅给出一个完整的工作示例:

using System.IO;
using System.Net;

void DownloadFile(Uri uri, string filename)
{
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
    request.Timeout = 10000;
    request.Method = "GET";
    request.UserAgent = "my example program v1";
    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
        using (Stream receiveStream = response.GetResponseStream())
        {
            using (FileStream fileStream = File.Create(filename))
            {
                receiveStream.CopyTo(fileStream);
            }
        }
    }
}

Writing something in the UserAgent property of the HttpWebRequest does indeed make a difference in a lot of cases. A common practice for web services seem to be to ignore requests with an empty UserAgent.
See: Webmasters: Interpretation of empty User-agent

Simply set the UserAgent property to a non-empty string. You can for example use the name of your application, assembly information, impersonate a common UserAgent, or something else identifying.

Examples:

request.UserAgent = "my example program v1";
request.UserAgent = $"{System.Reflection.Assembly.GetExecutingAssembly().GetName().Name.ToString()} v{System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString()}";
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36";

And just to give a full working example:

using System.IO;
using System.Net;

void DownloadFile(Uri uri, string filename)
{
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
    request.Timeout = 10000;
    request.Method = "GET";
    request.UserAgent = "my example program v1";
    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
        using (Stream receiveStream = response.GetResponseStream())
        {
            using (FileStream fileStream = File.Create(filename))
            {
                receiveStream.CopyTo(fileStream);
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文