Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# HttpWebRequest只发送Get请求,不发送post请求_C#_Asp.net_Http - Fatal编程技术网

C# HttpWebRequest只发送Get请求,不发送post请求

C# HttpWebRequest只发送Get请求,不发送post请求,c#,asp.net,http,C#,Asp.net,Http,我试图使用HttpWebRequest发送POST请求,fiddler向我显示我发送GET? 任何帮助都将被感激,因为我能看到我做错了什么 代码: HttpWebRequest request = (HttpWebRequest)WebRequest.Create(LOGIN_API_BASE_URL); string postString = string.Format("api_email={0}&api_password={1}", EMAIL, PASSWORD)

我试图使用HttpWebRequest发送POST请求,fiddler向我显示我发送GET? 任何帮助都将被感激,因为我能看到我做错了什么

代码:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(LOGIN_API_BASE_URL);
        string postString = string.Format("api_email={0}&api_password={1}", EMAIL, PASSWORD);
        ASCIIEncoding encoding = new ASCIIEncoding();
        byte[] data = encoding.GetBytes(postString);
        CookieContainer cookies = new CookieContainer();
        request.CookieContainer = cookies;
        //request.AllowWriteStreamBuffering = true;
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = data.Length;
        request.Timeout = i_timeout;
        //request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1";
        //request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
        //request.Referer = "https://accounts.craigslist.org";

        using (Stream writer  = request.GetRequestStream())
        {
            writer.Write(data, 0, data.Length);
            //writer.Close();
        }

        StreamReader responseReader = new StreamReader(request.GetResponse().GetResponseStream());
        string responseData = responseReader.ReadToEnd();

        responseReader.Close();
        request.GetResponse().Close();

好的,我发现了问题,我发送请求的url是,当我发送到它时,它工作并发送帖子。那么新的问题是为什么会这样?(答案将得到我问题的答案,因为我不喜欢投票给自己)

您是以明文形式发送密码吗?@Arran我根据API说明工作,并尝试以明文和字节形式发送密码,但这不是问题所在,因为服务器上可能会重新编写URL,从而重定向到www.domain.com的标准格式。计划不周的URL重写通常会忘记考虑POST请求,因此重定向会将其转换为GET请求。@Chris ok。但是当我尝试使用测试它时,请求发送正常,响应也正常,返回http 302(这很好,因为我只希望响应中有cookie),但在我的代码中,即使是post,我仍然收到404错误。。。。再次感谢您的帮助取决于所讨论的站点,情况可能仍然如此。例如,如果使用ASP.NET MVC,并且在domain.com/home/dostuff中定义的操作是POST only,尝试发出GET请求将导致404错误,因为没有接受GET请求的端点。不是MVC,它是一个java服务器,并且im发送相同的参数将抛出我的应用程序和请求生成器,在request maker中,它可以工作,在我的应用程序中,出于测试目的,它不能工作(两者现在都是POST),下面是url:如果不知道请求和响应的详细信息,很难说。您能否在问题中发布经过审查的请求/响应标题?