如何使用WebClient.DownloadData(到本地DummyPage.aspx)

发布于 2024-10-02 10:57:36 字数 741 浏览 0 评论 0原文

我正在关注此链接上的教程 http://www.codeproject.com/KB/ aspnet/ASPNETService.aspx

现在我被这些代码困住了

private const string DummyPageUrl = 
    "http://localhost/TestCacheTimeout/WebForm1.aspx";

private void HitPage()
{
    WebClient client = new WebClient();
    client.DownloadData(DummyPageUrl);
}

我的本地应用程序地址在“localhost”后面有一个端口号,那么我怎样才能获得完整路径(可以在Application_Start方法中完成)?我希望它非常通用,以便它可以在任何情况下工作。

多谢!

更新

我在 Application_Start 中尝试了这个,它运行良好,但在发布到 IIS7 时立即返回错误

String path = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + VirtualPathUtility.ToAbsolute("~/");

I'm following a tutorial on this link http://www.codeproject.com/KB/aspnet/ASPNETService.aspx

Now I'm stuck at these codes

private const string DummyPageUrl = 
    "http://localhost/TestCacheTimeout/WebForm1.aspx";

private void HitPage()
{
    WebClient client = new WebClient();
    client.DownloadData(DummyPageUrl);
}

My local application address has a port number after "localhost", so how can I get the full path (can it be done in Application_Start method)? I want it to be very generic so that it can work in any cases.

Thanks a lot!

UPDATE

I tried this in the Application_Start and it runs fine, but return error right away when published to IIS7

String path = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + VirtualPathUtility.ToAbsolute("~/");

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

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

发布评论

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

评论(3

羁绊已千年 2024-10-09 10:57:36

如果它回调到同一服务器,也许可以使用 Request 对象:

var url = new Uri(Request.Url, "/TestCacheTimeout/WebForm1.aspx").AbsoluteUri;

否则,将其他服务器的详细信息存储在配置文件或数据库中,然后为其提供正确的值。

但更好的问题是:为什么你要通过 http 与自己交谈??为什么不直接调用类方法呢?就我个人而言,我会使用外部计划作业来执行此操作。

If it is calling back to the same server, perhaps use the Request object:

var url = new Uri(Request.Url, "/TestCacheTimeout/WebForm1.aspx").AbsoluteUri;

Otherwise, store the other server's details in a config file or the database, and just give it the right value.

But a better question would be: why would you talk via http to yourself? Why not just call a class method? Personally I'd be using an external scheduled job to do this.

月亮坠入山谷 2024-10-09 10:57:36

当您部署到可能具有虚拟应用程序文件夹的不同环境时,您需要一个有效的答案。

// r is Request.Url
var url = new Uri(r, System.Web.VirtualPathUtility.ToAbsolute("~/Folder/folder/page.aspx")).AbsoluteUri;

这适用于所有情况,并且在部署时不会出现令人讨厌的意外。

You need an answer that works when you roll out to a different environment that maybe has a virtual application folder.

// r is Request.Url
var url = new Uri(r, System.Web.VirtualPathUtility.ToAbsolute("~/Folder/folder/page.aspx")).AbsoluteUri;

This will work in all cases and no nasty surprises when you deploy.

又怨 2024-10-09 10:57:36

我怀疑您使用的是 Visual Studio 内置的 ASP.NET 开发服务器,默认情况下它倾向于更改端口号。如果是这种情况,那么您可以尝试简单地将开发服务器配置为始终使用相同的端口,如此处所述。然后只需将端口号添加到您的 URL,如下所示:

private const string DummyPageUrl = 
"http://localhost:42001/TestCacheTimeout/WebForm1.aspx";

I suspect you're using the ASP.NET development server that's built-in to Visual Studio, which has a tendency to change port numbers by default. If that's the case, then you might try simply configuring the development server to always use the same port, as described here. Then just add the port number to your URL, like so:

private const string DummyPageUrl = 
"http://localhost:42001/TestCacheTimeout/WebForm1.aspx";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文