无法处理来自HTTP / HTTPS协议等不同的人重定向的人、重定向、协议、不同

2023-09-05 02:19:11 作者:我请你让我滚丶算不算诚恳

基本上,我试图抓住从CNET的Download.com

一个EXE

所以我创建的网络分析器,到目前为止,一切进展顺利。

下面是一个简单的链接直接拉到从他们的网站:

http://dw.com.com/redir?edId=3&siteId=4&oId=3001-20_4-10308491&ontId=20_4&spi=e6323e8d83a8b4374d43d519f1bd6757&lop=txt&tag=idl2&pid=10566981&mfgId=6250549&merId=6250549&pguid=PlvcGQoPjAEAAH5rQL0AAABv&destUrl=ftp%3A%2F%2F202.190.201.108%2Fpub%2Fryl2%2Fclient%2Finstaller-ryl2_v1673.exe

下面的问题是:当你尝试下载,它以HTTP开始,然后重定向到FTP站点。我曾尝试.NET的Web客户端和HttpWebRequest的对象,而且看起来也不能够支持重定向。

Nginx服务的安全加密访问https和重定向

这code处的GetResponse()失败;

  HttpWebRequest的REQ =(HttpWebRequest的)WebRequest.Create(http://dw.com.com/redir);
WebResponse的响应= req.GetResponse();
 

现在,我也尝试过这样的:

  HttpWebRequest的REQ =(HttpWebRequest的)WebRequest.Create(http://dw.com.com/redir);
req.AllowAutoRedirect = FALSE;
WebResponse的响应= req.GetResponse();
字符串s =新的StreamReader(response.GetResponseStream())ReadToEnd()。
 

和它不会抛出错误了,但是变量s原来是一个空字符串。

我不知所措!谁能帮帮忙?

解决方案

您可以得到的位置头球response.headers的值,然后创建一个新的FtpWebRequest下载该资源。

Basically, I'm trying to grab an EXE from CNet's Download.com

So i created web parser and so far all is going well.

Here is a sample link pulled directly from their site:

http://dw.com.com/redir?edId=3&siteId=4&oId=3001-20_4-10308491&ontId=20_4&spi=e6323e8d83a8b4374d43d519f1bd6757&lop=txt&tag=idl2&pid=10566981&mfgId=6250549&merId=6250549&pguid=PlvcGQoPjAEAAH5rQL0AAABv&destUrl=ftp%3A%2F%2F202.190.201.108%2Fpub%2Fryl2%2Fclient%2Finstaller-ryl2_v1673.exe

Here is the problem: When you attempt to download, it begins with HTTP, then redirects to an FTP site. I have tried .NET's WebClient and HttpWebRequest Objects, and it looks like Neither can support Redirects.

This Code Fails at GetResponse();

HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://dw.com.com/redir");
WebResponse response = req.GetResponse();

Now, I also tried this:

HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://dw.com.com/redir");
req.AllowAutoRedirect = false;
WebResponse response = req.GetResponse();
string s = new StreamReader(response.GetResponseStream()).ReadToEnd();

And it does not throw the error anymore, however variable s turns out to be an empty string.

I'm at a loss! Can anyone help out?

解决方案

You can get the value of the "Location" header from the response.headers, and then create a new FtpWebRequest to download that resource.