WebClient - 检测响应重定向 [英] WebClient - detect response redirect

查看:62
本文介绍了WebClient - 检测响应重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个控制台实用程序,用于从网站下载特定文件

,具体取决于命令行选项。在大多数情况下,当文件不可用时,我可以捕获404

错误,因为操作员错误输入了

URL或者因为某种原因它处于脱机状态。我遇到的问题是
,某些网站管理员设置了重定向来处理404条件并将请求重定向到另一个页面。


在这种情况下,重定向的页面会被下载并保存,这不是所需的结果。该实用程序用于调度过程

从公共网站下载和处理特定数据文件和

文件必须存在,或返回错误以停止处理。


有没有告诉WebClient或WebRequest对象不允许

重定向内容?或者,是否有任何一个对象

中的属性反映了源重定向时的实际URL?或者,

有一个替代对象,可以用于此目的从网站下载文件吗?


任意非常感谢帮助。


- Glen

I''m writing a console utility to download specific files from web sites
based on the command line options. In most cases, I can trap the 404
error when the file isn''t available because the operator mistyped the
URL or it''s offline for whatever reason. The problem I''m running into
is with certain sites where the admin has set up a redirect to handle
the 404 condition and redirects the request to another page.

In this case, the redirected page gets downloaded and saved which is not
the desired result. This utility is being used in a scheduling process
to download and process specific data files from public web sites and
the files must exist, or return an error to halt processing.

Is there anyway to tell the WebClient or WebRequest objects to not allow
redirected content? Or, is there a property in either of the objects
that reflects the actual URL of the source when it''s redirected? Or, is
there an alternative object that can be used for this purpose to
download files from web sites?

Any help would be greatly appreciated.

- Glen

推荐答案

我认为没有使用WebClient类的方式。


如果您使用的是HttpWebRequest,那么AllowAutoRedirect属性

可以让您完成所需的工作。


-

Adam Clauss
ca ***** @ tamu.edu

" Glen" <卜**** @ hotmail.com>在留言中写道

news:u4 **************** @ TK2MSFTNGP14.phx.gbl ...
I don''t think there is a way using the WebClient class.

If you are using HttpWebRequest, there is the AllowAutoRedirect property
which may allow you to accomplish what you need.

--
Adam Clauss
ca*****@tamu.edu
"Glen" <Bu****@hotmail.com> wrote in message
news:u4****************@TK2MSFTNGP14.phx.gbl...
我'' m编写控制台实用程序以从网站下载特定文件
基于命令行选项。在大多数情况下,当文件不可用时,我可以捕获404
错误,因为操作员错误输入了URL
或者由于某种原因它处于脱机状态。我遇到的问题是
某些网站管理员设置了重定向来处理404状态并将请求重定向到另一个页面。

在这种情况下,重定向的页面被下载并保存,这不是所需的结果。此实用程序用于调度过程,以便从公共网站下载和处理特定数据文件,并且
文件必须存在,或者返回错误以停止处理。

反正有没有告诉WebClient或WebRequest对象不允许重定向内容?或者,是否有任何一个对象中的属性在重定向时反映了源的实际URL?或者,是否有可用于此目的的替代对象从网站下载文件?

任何帮助将不胜感激。

- Glen
I''m writing a console utility to download specific files from web sites
based on the command line options. In most cases, I can trap the 404
error when the file isn''t available because the operator mistyped the URL
or it''s offline for whatever reason. The problem I''m running into is with
certain sites where the admin has set up a redirect to handle the 404
condition and redirects the request to another page.

In this case, the redirected page gets downloaded and saved which is not
the desired result. This utility is being used in a scheduling process to
download and process specific data files from public web sites and the
files must exist, or return an error to halt processing.

Is there anyway to tell the WebClient or WebRequest objects to not allow
redirected content? Or, is there a property in either of the objects that
reflects the actual URL of the source when it''s redirected? Or, is there
an alternative object that can be used for this purpose to download files
from web sites?

Any help would be greatly appreciated.

- Glen



Glen,


我知道很多网站只有一个URL作为开始点而不是

重定向,当你实施时,你如何处理这些。


不是我有答​​案但是可能发现它开了一个新问题

正在寻找这个解决方案无用..


只是我的想法,


Cor
Glen,

I know a lot of sites that have an URL only as a start point and than
redirect, how do you handle those when you have your implementation.

Not that I have the answer however maybe is finding it opening a new problem
and is searching for this solution useless..

Just my thought,

Cor


您好Cor,


因为我没有构建浏览器而且我正在寻找非常具体的

来自网络的数据,我希望系统运营商能够记录和处理远程数据源中的任何变化。同样,这是一个关键的

任务,就像通过FTP获取文件一样。我不希望当我登录到一个FTP站点时,我会得到一个不同于我要求的文件。


无论如何,感谢您的意见。


- Glen


" Cor Ligthert" <无************ @ planet.nl>在消息中写道

新闻:OJ **************** @ TK2MSFTNGP12.phx.gbl ...
Hi Cor,

Since I''m not building a browser and I''m looking to retrieve very specific
data from the web, I expect that any changes in the remote data source will
be documented and handled by the system operators. Again, this is a critical
task, much like getting files via FTP. I don''t expect that when I log into
an FTP site, I''m going to get a different file than the one I requested.

In any case, thank you for your input.

- Glen

"Cor Ligthert" <no************@planet.nl> wrote in message
news:OJ****************@TK2MSFTNGP12.phx.gbl...
Glen,

我知道很多站点只将URL作为起点而不是重定向,如何在实施时处理这些站点。

我有答案,但也许是发现它开了一个新的
问题并且正在寻找这个解决方案没用..

只是我的想法,

Cor
Glen,

I know a lot of sites that have an URL only as a start point and than
redirect, how do you handle those when you have your implementation.

Not that I have the answer however maybe is finding it opening a new problem and is searching for this solution useless..

Just my thought,

Cor



这篇关于WebClient - 检测响应重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆