具有相同标题和有效负载的Python代码可以正常工作
.NET中的非正常示例与使用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;
}
(请问如果有的话请不要判断我的编码不好,我还是很笨)
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)
一切正常,直到
var webResponse = (HttpWebResponse)request.GetResponse();
,它只返回“ 400 Bad Request”。
答案 0 :(得分:1)
timestamp = (int)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds*1000
此指令返回-916073112
。
从1970年开始,它已经过去了1596811643000毫秒,但是int
是32位带符号的。 max value为2147483647。int
无法接收该值,并且为overflowed。时间戳记变为负数。
我认为API不接受否定的时间戳并返回错误请求(HTTP代码400)。
解决方案:
timestamp = (long)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds*1000
long
是64位带符号的。 The max value是9223372036854775807,可以接收该值。