C# HttpWebRequest POST not sending parameters

[亡魂溺海] 提交于 2019-12-23 21:57:52

问题


I am trying to perform a POST request with parameters using the next code. I'm using a local php script to receive the parameters, but when I perform the request the php script is not receiving the parameters sent form my C# function; it say

Notice: Undefined index: detalle.

Notice: Undefined index: method:paginar

Notice: Undefined index: f_num_pagina

Function:

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;
    }

Calling function:

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:


Thank you everybody for helping and excuse me, but I just was confused, I was sending request to /inicio.action but the action of the pagination form was /busqueda.action.



来源:https://stackoverflow.com/questions/49375109/c-sharp-httpwebrequest-post-not-sending-parameters

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!