【发布时间】:2017-10-25 12:53:37
【问题描述】:
我已经在 IIS 上部署了一个网站(在本地机器上总是可以正常工作)并且发现当我运行我的一个例程时它失败并出现 401 未授权错误:
代码如下:
string html = String.Empty;
string CurrentURL = Request.Url.Scheme + Uri.SchemeDelimiter + Request.Url.Host + Request.Url.PathAndQuery;
Byte[] sAttachment;
using (WebClient client = new WebClient())
{
client.UseDefaultCredentials = true;
_logger.Debug("Getting page content ready to fax...");
--> fails here: html = client.DownloadString(new Uri(CurrentURL));
html = html.Replace("Add Recipient", "");
html = html.Replace("Send Fax", "");
html = html.Replace("Close", "");
//make sure the destination fax number and recipient is in the html
string toFaxDetailsHTML = "insertFaxDetails\">";
toFaxDetailsHTML += "<label class=\"form-label col-md-2 text-right\">Fax: </label>" + FVM.ToFaxNumber + "<br/>";
toFaxDetailsHTML += "<label class=\"form-label col-md-2 text-right\">To: </label>" + FVM.ToName + "<br/><br/>";
html = html.Replace("insertFaxDetails\">", toFaxDetailsHTML);
sAttachment = FVM.PdfSharpConvert(html);
FVM.Attachment = sAttachment;
}
任何帮助表示赞赏。
环境是 Windows 2008 R2 标准上的 IIS 7。 使用 MVC 5 c# .NET 4.5.2
【问题讨论】:
-
如果您尝试从另一个操作(例如,同一 URL 的 GET 版本)中获取控制器操作的呈现内容以将其转换为 PDF,那么可能还有更多无需对自己进行网络请求的有效方法(您受主机名、传出互联网访问、网络代理等支配)。一些电子邮件操作会做类似的事情 - 建议您从那里开始谷歌搜索,例如 codemag.com/article/1312081
-
顶!谢谢@Reddog
-
@Reddog - 我使用了您在上面链接的文章中描述的方法 - 第一次工作......(有什么问题??):-) - 感谢您的意见!
-
这是个好消息!我没有在文章中尝试过那种具体的方法,所以我不能保证它,所以如果有更好的方法,它是值得研究的。
标签: c# .net model-view-controller webclient downloadstring