将 HTTP 文件附件读取为字符串
Posted
技术标签:
【中文标题】将 HTTP 文件附件读取为字符串【英文标题】:Reading HTTP file attachment as string 【发布时间】:2014-05-13 23:52:26 【问题描述】:我正在使用WebRequest
来创建GET
,响应中包含一个附件。
附件是一个 html 文件,我想在标签之间去除内容。我已经设法使用以下代码进行调用:
string URI = "http://www.sample.com/ReportServer/Pages/ReportViewer.aspx?%2fReports&rs:Command=Render&rs:Format=MHTML&OrganisationID=" + organisationID;
CredentialCache cc = new CredentialCache();
cc.Add(new Uri(URI), "NTLM", new NetworkCredential(userName, userPassword, userDomain));
WebRequest req = WebRequest.Create(URI);
req.Credentials = cc;
WebResponse resp = req.GetResponse();
StreamReader reader = new StreamReader(resp.GetResponseStream());
string response = reader.ReadToEnd().Trim();
当我查看 Fiddler 时的响应是:
HTTP/1.1 200 OK
Cache-Control: private
Content-Type: multipart/related
Expires: Wed, 02 Apr 2014 14:35:15 GMT
Set-Cookie: RSExecutionSession%3a%2fPuborts%2fSecreal%2fClub+Meip+Ret=0yu4f1455xnmznu55; path=/
Server: Microsoft-HTTPAPI/2.0
X-AspNet-Version: 2.0.50727
FileExtension: mhtml
Content-Disposition: attachment; filename="Blah Report.mhtml"
Date: Wed, 02 Apr 2014 14:36:15 GMT
Content-Length: 84215
MIME-Version: 1.0
Content-Type: multipart/related;
boundary="----=_NextPart_01C35DB7.4B204430"
X-MSSQLRS-ProducerVersion: V10.50.4000.0
This is a multi-part message in MIME format.
------=_NextPart_01C35DB7.4B204430
Content-Disposition: inline; filename="Blah Membership Report"
Content-Type: text/html;
name="Club Membership Report";
charset="utf-8"
Content-Transfer-Encoding: base64
PCFET0NUWVBFIEhUTUwgUFVCTElDICItLy9XM0MvL0RURCBIVE1MIDQuMDEgVHJhbnNpdGlvbmFs
------=_NextPart_01C35DB7.4B204430--
请问我怎样才能只获取附件并将内容读入字符串?
【问题讨论】:
【参考方案1】:“附件”部分只是为了在浏览器中强制另存为屏幕。
你可以使用普通的WebClient
var client = new WebClient();
using (client)
client.Credentials = blablabla
var result = client.DownloadString("http://blablabla.com");
【讨论】:
感谢您的建议。我刚刚尝试了您的示例,得到了相同的结果:(以上是关于将 HTTP 文件附件读取为字符串的主要内容,如果未能解决你的问题,请参考以下文章
从 python 读取 wav 文件并将帧转换为 RAW s16le 字符串