My webrequest is not working get 400 error message?

杀马特。学长 韩版系。学妹 提交于 2020-01-25 07:54:06

问题


My webrequest is not working? I got an error. The website where I'm doing this request is: https://www.marktplaats.nl/account/login.html can anyone help me perform this request? I'm stuck with getting error message HTTP 400 Bad Request.

This is my method:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Text;
    using System.Threading.Tasks;

    namespace MPRequest
    {
        class HttpMethods
        {
            public static string Get(string url, string referer, ref CookieContainer cookies)
            {
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                req.Method = "GET";
                req.CookieContainer = cookies;
                req.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) Gecko/20100101 Firefox/71.0";
                req.Referer = referer;

                HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                cookies.Add(resp.Cookies);

                string pageSrc;
                using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
                {
                    pageSrc = sr.ReadToEnd();
                }
                return pageSrc;
            }

            public static bool Post(string url, string postData, string referer, CookieContainer cookies, string myToken)
            {
                string key = "Het e-mailadres of wachtwoord is niet juist.";
                byte[] postBytes = Encoding.ASCII.GetBytes(postData);

                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                req.Method = "POST";
                req.CookieContainer = cookies;
                req.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) Gecko/20100101 Firefox/71.0";
                req.Referer = referer;
                req.ContentType = "application/json; charset=utf-8";
                req.ContentLength = postBytes.Length;
                req.Accept = "application/json, text/javascript, */*; q=0.01";
                req.Headers.Add("x-mp-xsrf", myToken);


                Stream postStream = req.GetRequestStream();


                postStream.Write(postBytes, 0, postBytes.Length);
                postStream.Dispose();

                HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                cookies.Add(resp.Cookies);

                StreamReader sr = new StreamReader(resp.GetResponseStream());
                string pageSrc = sr.ReadToEnd();
                sr.Dispose();
                return (!pageSrc.Contains(key));
            }


        }
    }

This is my Main program.cs :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace MPRequest
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Net.CookieContainer myCookies = new System.Net.CookieContainer();
            string myStr = HttpMethods.Get("https://www.marktplaats.nl/account/login.html", "https://www.marktplaats.nl/account/login.html", ref myCookies);
            string regex = "<input type\\=\\\"hidden\\\" name=\\\"xsrf\\.token\\\" value\\=\\\"([^\\\"]*)\\\"";
            var myToken = Regex.Match(myStr, regex).Groups[1].Value;
            Console.WriteLine("Token is : " + myToken);


            Console.Write("Username: ");
            string username = Console.ReadLine();
            Console.Write("Password: ");
            string password = Console.ReadLine();


            string postData = "{ 'email':'" + username + "','password':'" + password + "','rememberMe':true,'successUrl':'https://www.marktplaats.nl'}";

            bool result = HttpMethods.Post("https://www.marktplaats.nl/account/login.html", postData, "https://www.marktplaats.nl/account/login.html", myCookies, myToken);
            Console.WriteLine(result.ToString());
            Console.ReadKey();
        }

    }
}

Also here is an Photo of the error :

BAD REQUEST HTTP 400

来源:https://stackoverflow.com/questions/59336746/my-webrequest-is-not-working-get-400-error-message

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