2

我正在尝试使用 WebClient 模拟发布请求;但是,当使用 Firefox 登录并使用 firebug 调试请求时,我发现在 POST 请求之后它会自动执行一些 GET 请求,而使用我的代码只执行 POST 请求

我的代码

//Handler is an overridden WebClient Class
        private async Task<byte[]> Post(string uri, string[] data)
        {
            var postData = new NameValueCollection();

            foreach (var info in data.Select(var => var.Split('=')))
            {
                postData.Add(info[0], info[1]);
            }

            return await Handler.UploadValuesTaskAsync(new Uri(uri), postData);
        }
4

1 回答 1

1

我知道这不是您所要求的,并且它在 VB 中,但希望它可以帮助您指出正确的方向。这是我用来在我们的一个网站上发出帖子请求的工具。它适用于模拟 POST 数据,希望您可以将其中的一些融入到您正在做的事情中。

   Dim postData As String = String.Format("RedirectLocation=RequestMethod=&username={0}&password={1}", _username, _password)

   Dim _loginRequest As HttpWebRequest = WebRequest.Create(loginurl)

           With _loginRequest
                .Method = "POST"
                .ContentLength = postData.Length
                .ContentType = "application/x-www-form-urlencoded"                        
                .KeepAlive = True
                .AllowAutoRedirect = False

                .CookieContainer =  New CookieContainer

                Using writer As New StreamWriter(.GetRequestStream)
                    writer.Write(postData)
                End Using
                .Timeout = tsTimeOut.TotalMilliseconds
                _loginResponse = .GetResponse()
            End With
于 2012-11-23T23:29:23.567 回答