Web客户端不自动重定向 [英] WebClient Does not automatically redirect

查看:161
本文介绍了Web客户端不自动重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在登录使用Firebug我看到它是这样的。

When logging the login process using Firebug i see that it is like this

POST //The normal post request
GET //Automatically made after the login
GET //Automatically made after the login
GET //Automatically made after the login

在使用我下面的代码并没有使该浏览器正在做自动GET请求的POST请求。

When making a post request using my code below it did not make the automatic GET requests that the browsers is doing.

我的Web客户端处理程序

MY WebClient Handler

using System;
using System.Net;

namespace Test
{
    class HttpHandler : WebClient
    {
        private CookieContainer _mContainer = new CookieContainer();

        protected override WebRequest GetWebRequest(Uri address)
        {
            var request = base.GetWebRequest(address);
            if (request is HttpWebRequest)
            {
                (request as HttpWebRequest).CookieContainer = _mContainer;
            }
            return request;
        }

        protected override WebResponse GetWebResponse(WebRequest request)
        {
            var response = base.GetWebResponse(request);
            if (response is HttpWebResponse)
                _mContainer.Add((response as HttpWebResponse).Cookies);
            return response;
        }

        public void ClearCookies()
        {
            _mContainer = new CookieContainer();
        }
    }
}



使用代码

Using Code

private static async Task<byte[]> LoginAsync(string username, string password)
{
    var postData = new NameValueCollection();
    var uri = new Uri(string.Format("http://{0}/", ServerName));

    postData.Add("name", username);
    postData.Add("password", password);

    return await HttpHandler.UploadValuesTaskAsync(uri, postData);
}

当试图跟踪我的应用程序的连接,它只是做POST请求而不是GET请求的其余部分。 [即是自动进行的浏览器]

When trying to track the connection of my application it is only doing the POST Request and not the rest of GET requests. [THAT ARE MADE AUTOMATICALLY IN THE BROWSER]

推荐答案

这并不奇怪,因为的HttpWebRequest 不是浏览器。如果您需要执行这些重定向,然后检查 HttpWebResponse。的StatusCode ,使另一个请求,如果它是一个在300的重定向代码。从链接注意下10.3重定向3xx的:

That shouldn't be surprising, given that HttpWebRequest is not a browser. If you need to perform these redirects, then check the HttpWebResponse.StatusCode, and make another request if it's a redirect code in the 300's. Note from the link under 10.3 Redirection 3xx:

本类的状态代码表示进一步的动作,才能采取由用户代理完成请求。所需的动作可以由用户代理,而不与用户交互来进行,当且仅当在所述第二请求所使用的方法是GET或HEAD。客户端应检测无穷的重定向循环,因为这种循环生成每个重定向网络流量。

This class of status code indicates that further action needs to be taken by the user agent in order to fulfill the request. The action required MAY be carried out by the user agent without interaction with the user if and only if the method used in the second request is GET or HEAD. A client SHOULD detect infinite redirection loops, since such loops generate network traffic for each redirection.

这篇关于Web客户端不自动重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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