简体   繁体   中英

HTTP POST Returns 400 Bad Request

Python code with same headers and payload works fine

Here non-working example in .NET vs working one with Python headers.cs

        public string Postheaders(string url, string data, string deviceID, string deviceID_sig, string userAgent)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.Headers["NDCDEVICEID"] = deviceID;
            request.Headers["NDC-MSG-SIG"] = deviceID_sig;
            request.Headers["Accept-Language"] = "en-US";
            request.UserAgent = userAgent;
            request.Host = host;
            request.ContentType = "application/json; charset=utf-8";
            request.ContentLength = data.Length;
            var requestStream = request.GetRequestStream();
            using (var streamWriter = new StreamWriter(requestStream))
            {
                streamWriter.Write(data);
            }
            var webResponse = (HttpWebResponse)request.GetResponse();
            var webStream = webResponse.GetResponseStream();
            var responseReader = new StreamReader(webStream);
            var response = responseReader.ReadToEnd();
            return response;
        }

client.cs

        public string Login(string email, string password)
        {
            var deviceinfo = new device();
            var headers = new headers();
            string data = new JavaScriptSerializer().Serialize(new
            {
                email = email,
                v = 2,
                secret = string.Format("0 {0}", password),
                deviceID = deviceinfo.deviceID,
                clientType = 100,
                action = "normal",
                timestamp = (int)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds*1000
        });
            string response = headers.Postheaders(string.Format("{0}/g/s/auth/login", api), data, deviceinfo.deviceID, deviceinfo.deviceID_sig, deviceinfo.userAgent);
            return response;
        }

(pls don't judge me for bad coding if I have any, I'm still dumb)

headers.py:

class Headers:
    def __init__(self, device = device.DeviceGenerator(), data = None, type = None):
        headers = {
            "NDCDEVICEID": device.device_id,
            "NDC-MSG-SIG": device.device_id_sig,
            "Accept-Language": "en-US",
            "Content-Type": "application/json; charset=utf-8",
            "User-Agent": device.user_agent,
            "Host": "service.narvii.com",
            "Accept-Encoding": "gzip",
            "Connection": "Keep-Alive"
        }

        if data: headers["Content-Length"] = str(len(data))
        if sid: headers["NDCAUTH"] = f"sid={sid}"
        if type: headers["Content-Type"] = type
        self.headers = headers

client.py

    def login(self, email: str, password: str):
        data = json.dumps({
            "email": email,
            "v": 2,
            "secret": f"0 {password}",
            "deviceID": self.device_id,
            "clientType": 100,
            "action": "normal",
            "timestamp": int(timestamp() * 1000)
        })

        response = requests.post(f"{self.api}/g/s/auth/login", headers=headers.Headers(data=data).headers, data=data)

Everything works fine until var webResponse = (HttpWebResponse)request.GetResponse(); , where it just returns "400 Bad Request".

timestamp = (int)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds*1000

This instruction return -916073112 .

From 1970, it's elapsed 1596811643000 milliseconds, but int is 32 bit signed. The max value is 2147483647. int can't not receive the value and be overflowed . timestamp became negative.

I think the API don't accept a negative timestamp and return a Bad Request (HTTP Code 400).

The solution:

timestamp = (long)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds*1000

long is 64 bit signed. The max value is 9223372036854775807 and can receive the value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM