(400) 错误请求,无法得到响应(自定义 minecraft 启动器)

(400) Bad Request, can't get response (Custom minecraft launcher)

我似乎无法 post JSON 到网页 https://authserver.mojang.com/authenticate 并获得回复。 当我 post JSON 它只是说

The remote server returned an error: (400) Bad Request

我研究了其他人的许多不同脚本,并通过将 Java 代码转换为 C# 创建了我自己的脚本。不管怎样,这是迄今为止效果最好的代码。

string authserver = "https://authserver.mojang.com/authenticate";
byte[] rawData = fs.GetBytes(**[JSON]**);

WebRequest request = WebRequest.Create(authserver);
request.ContentType = "application/json";
request.Method = "POST";
//request.ContentLength = rawData.LongLength;
WebResponse connection = request.GetResponse();
connection.ContentType = "application/json";
connection.ContentLength = rawData.LongLength;
Stream stream = connection.GetResponseStream();
stream.Write(rawData, 0, rawData.Length);
stream.Flush();
byte[] rawVerification = new byte[10000];
int count = stream.Read(rawVerification, 0, 10000);

编辑: 是否可以使用 webclient 执行此代码?

编辑: 它的输入无效,json 没有所需的正确数据

试试这个:

WebRequest request = WebRequest.Create (authserver);
request.Method = "POST";
string postData = "YourJSON";
byte[] byteArray = Encoding.UTF8.GetBytes (postData);
request.ContentType = "application/json";
request.ContentLength = byteArray.Length;
using(Stream s = request.GetRequestStream ()){
    s.Write (byteArray, 0, byteArray.Length);
}
WebResponse response = request.GetResponse ();
using(var dataStream = response.GetResponseStream ()){
    using(StreamReader reader = new StreamReader (dataStream)){
        string responseFromServer = reader.ReadToEnd ();
    }
}
response.Close();

基本上您不应该在提交数据之前调用 getResponse()