.Net C#:从 HttpWebResponse 读取附件
是否可以从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能需要设置 User-Agent 标头。
运行 Fiddler 并比较请求。
You probably need to set a User-Agent header.
Run Fiddler and compare the requests.
在
UserAgent
中编写一些内容HttpWebRequest
的属性 在很多情况下确实会产生影响。 Web 服务的常见做法似乎是忽略带有空UserAgent
的请求。请参阅:网站管理员:空用户代理的解释
只需将
UserAgent
属性设置为非空字符串
。例如,您可以使用应用程序的名称、程序集信息、模拟 common UserAgent,或其他标识。示例:
仅给出一个完整的工作示例:
Writing something in the
UserAgent
property of theHttpWebRequest
does indeed make a difference in a lot of cases. A common practice for web services seem to be to ignore requests with an emptyUserAgent
.See: Webmasters: Interpretation of empty User-agent
Simply set the
UserAgent
property to a non-emptystring
. You can for example use the name of your application, assembly information, impersonate a common UserAgent, or something else identifying.Examples:
And just to give a full working example: