如何为Webclient启用自动重定向 [英] How to turn on autoredirect for Webclient

查看:90
本文介绍了如何为Webclient启用自动重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此代码:

class CustomWebclient: WebClient
{
  [System.Security.SecuritySafeCritical]
  public CustomWebclient(): base()
 {
 }
 public CookieContainer cookieContainer = new CookieContainer();


 protected override WebRequest GetWebRequest(Uri myAddress)
 {
       WebRequest request = base.GetWebRequest(myAddress);
       if (request is HttpWebRequest)
      {
           (request as HttpWebRequest).CookieContainer =   cookieContainer;
           (request as HttpWebRequest).AllowAutoRedirect = true;
      }
      return request;
  }
}

当我加载example.com页面时,我得到:

When i load page example.com, i get:

Server: nginx
Date: Fri, 23 Aug 2013 05:24:44 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: close
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
location: /examplePage

但是我的CustomWebclient不遵循重定向.为什么?该如何解决?

But my CustomWebclient doesnt follow redirect. Why? What to do to fix it?

可能是因为小写的位置"而不起作用.

May be it didnt work because "location" in lower case.

推荐答案

您的代码在我的测试下运行良好.

Your code runs fine under my test.

当提供适当的cookie时,我的测试网站weibo.com将强制通过HTTP 302进行重定向.

My test site weibo.com will force redirect by HTTP 302 when proper cookies are provided.

关闭自动重定向后,响应标题为

With autoredirect off, the Response Headers are

Transfer-Encoding: chunked
Connection: close
Pramga: no-cache
DPOOL_HEADER: dagda24
Cache-Control: no-cache, must-revalidate
Content-Type: text/html
Date: Thu, 13 Mar 2014 15:07:53 GMT
Expires: Mon, 26 Jul 1997 05:00:00 GMT
Last-Modified: Thu, 13 Mar 2014 15:07:53 GMT
Location: /u/2398332747/home?wvr=5
Server: nginx

并且网页没有明显重定向.

and the web page is not redirected obviously.

启用自动重定向后,响应标题为

With autoredirect on, the Response Headers are,

Transfer-Encoding: chunked
Connection: close
Vary: Accept-Encoding
Pragma: no-cache
DPOOL_HEADER: balor165
Cache-Control: no-cache, must-revalidate
Content-Type: text/html; charset=utf-8
Date: Thu, 13 Mar 2014 15:13:26 GMT
Expires: Sat, 26 Jul 1997 05:00:00 GMT
Server: nginx

网页内容验证请求已重定向.

the web page contents verifies that the request is redirected.

我确实注意到Location标头的首字母大写.尚未测试标头格式是否相关.

I do notice that the Location header's first letter is capital. This is not tested whether header format is related.

最后,对example.com进行了测试,但是在我的环境下,它根本不会触发重定向.这就是为什么我选择weibo.com作为替代.

At last, example.com is tested but under my environment it doesn't trigger a redirection at all. That's why i picked weibo.com as the alternative.

我对此进行了更多研究,根据RFC 2616,标头不区分大小写.您的代码对我来说很好,但仍然不知道为什么会出错.希望其他人能带头.

I did some more research on this and according to RFC 2616, headers are case-insensitive. Your code is fine to me, still don't know why it goes wrong. Hoping someone else give a lead.

这篇关于如何为Webclient启用自动重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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