【问题标题】:WebRequest not working to an existing URLWebRequest 不适用于现有 URL
【发布时间】:2020-02-15 22:31:34
【问题描述】:

下面的代码有效,但现在无效。我要请求的 url 存在,但加载需要一段时间,因为它会生成 PDF 文件。它返回 404 not found 立即。就像等待异步不起作用。

var request = System.Net.WebRequest.Create($"http://moachingtestweb.azurewebsites.net/FoodPlanner/Pdf?foodplanId={foodplanId}");
//var request = System.Net.WebRequest.Create($"https://application.moaching.com/FoodPlanner/Pdf?foodplanId={foodplanId}"); //TODO for prod. Add to config.
request.Method = "GET";
var response = await request.GetResponseAsync();

var stream = response.GetResponseStream();

return new FileStreamResult(stream, "application/pdf");

【问题讨论】:

  • 您是否获取了应用程序正在使用的确切 URL 并将其放入浏览器中?
  • @howcheng 是的。它有效。我还尝试向基本 url 发出请求,并且可以。但是,当我加载您在代码中看到的 URL 时,它会返回 404 正确等待。如前所述,网址需要时间重新加载。该网站托管在 Azure 应用服务中。
  • 好吧,如果它返回 404,那就是服务器响应; Web 请求不会无缘无故地告诉您 Not Found。也许您需要设置request.Credentials?抱歉,我不使用 Azure 网站,所以我不太熟悉身份验证/授权的工作原理。

标签: c# asynchronous asp.net-core stream webrequest


【解决方案1】:

如果你认为它超时,那么增加timeout并尝试。

// Set the 'Timeout' property in Milliseconds.
myWebRequest.Timeout=10000;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-08
    • 1970-01-01
    • 2018-02-11
    • 2016-04-02
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多