C#HttpWebRequest POST不发送参数 [英] C# HttpWebRequest POST not sending parameters

查看:517
本文介绍了C#HttpWebRequest POST不发送参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

Notice: Undefined index: method:paginar

注意:未定义索引:f_num_pagina

Notice: Undefined index: 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);

推荐答案

谢谢大家的帮助和原谅,但我很困惑,我正在向/inicio.action发送请求,但是分页表单的操作是/busqueda.action.

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.

这篇关于C#HttpWebRequest POST不发送参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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