C#HttpWebRequest POST不发送参数

时间:2018-03-20 01:42:52

标签: c# post parameters

我正在尝试使用下一个代码执行带参数的POST请求。我使用本地php脚本来接收参数,但是当我执行请求时,php脚本没有收到从我的C#函数发送的参数;它说

  

注意:未定义的索引:detalle。

     

注意:未定义索引:方法:paginar

     

注意:未定义的索引:f_num_pagina

功能:

public async Task<string> GetHttpStream(Uri HtmlPage, string method, byte[] postData)
    {
        HttpWebRequest httpRequest;
        string Payload = string.Empty;
        httpRequest = WebRequest.CreateHttp(HtmlPage);
        try
        {
            httpRequest.CookieContainer = CookieJar;
            httpRequest.KeepAlive = true;
            httpRequest.ConnectionGroupName = Guid.NewGuid().ToString();
            httpRequest.AllowAutoRedirect = true;
            httpRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
            httpRequest.ServicePoint.MaxIdleTime = 30000;
            httpRequest.ServicePoint.Expect100Continue = false;
            httpRequest.UserAgent = "Mozilla/5.0 (Windows NT 10; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0";
            httpRequest.Accept = "ext/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            httpRequest.Headers.Add(HttpRequestHeader.AcceptLanguage, "es-ES,es;q=0.8,en-US;q=0.5,en;q=0.3");
            httpRequest.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate;q=0.8");
            httpRequest.Headers.Add(HttpRequestHeader.CacheControl, "no-cache");
            httpRequest.Method = method;
            if (method == "POST")
            {
                httpRequest.ContentType = "application/x-www-form-urlencoded";
                httpRequest.ContentLength = postData.Length;
                using (var stream = httpRequest.GetRequestStream())
                {
                    stream.Write(postData, 0, postData.Length);
                }
            }
            using (HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse())
            {
                Stream ResponseStream = httpResponse.GetResponseStream();

                if (httpResponse.StatusCode == HttpStatusCode.OK)
                {
                    try
                    {
                        //ResponseStream.Position = 0;
                        Encoding encoding = Encoding.GetEncoding(httpResponse.CharacterSet);

                        using (MemoryStream _memStream = new MemoryStream())
                        {
                            if (httpResponse.ContentEncoding.Contains("gzip"))
                            {
                                using (GZipStream _gzipStream = new GZipStream(ResponseStream, System.IO.Compression.CompressionMode.Decompress))
                                {
                                    _gzipStream.CopyTo(_memStream);
                                };
                            }
                            else if (httpResponse.ContentEncoding.Contains("deflate"))
                            {
                                using (DeflateStream _deflStream = new DeflateStream(ResponseStream, System.IO.Compression.CompressionMode.Decompress))
                                {
                                    _deflStream.CopyTo(_memStream);
                                };
                            }
                            else
                            {
                                ResponseStream.CopyTo(_memStream);
                            }
                            _memStream.Position = 0;
                            using (StreamReader _reader = new StreamReader(_memStream, encoding))
                            {
                                Payload = _reader.ReadToEnd().Trim();
                            };
                        };
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error");
                        Payload = string.Empty;
                    }
                }
            }
        }
        catch (WebException exW)
        {
            if (exW.Response != null)
            {
                MessageBox.Show(exW.Message, "Error");
            }
        }
        catch (System.Exception exS)
        {
            MessageBox.Show(exS.Message, "Error");
        }
        CookieJar = httpRequest.CookieContainer;
        return Payload;
    }

通话功能:

var postData = "detalle=1&method:paginar=2&f_num_pagina=2";
byte[] parameters= Encoding.UTF8.GetBytes(postData);
string HtmlPage = await GetHttpStream(url, "POST", parameters);

1 个答案:

答案 0 :(得分:0)

谢谢大家的帮助和原谅,但我只是感到困惑,我正在向/inicio.action发送请求,但是分页形式的行为是/busqueda.action。