【问题标题】:(400) Bad Request, can't get response (Custom minecraft launcher)(400) 错误请求,无法得到响应(自定义 minecraft 启动器)
【发布时间】:2015-11-15 07:33:13
【问题描述】:

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

远程服务器返回错误:(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 没有所需的正确数据

【问题讨论】:

    标签: c# http-post


    【解决方案1】:

    试试这个:

    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()

    【讨论】:

    • 你能把你的代码更新成你现在的样子吗?您还知道提交数据中的服务器期望什么?
    • 当然,但我不知道你所说的服务器期望是什么意思,协议是 ssl 并且只使用 POST,其他任何东西都不允许使用 405 方法
    • 错误请求通常意味着您没有发送数据或您的数据看起来不正确。现在你得到 405,也许他们阻止了 CORS
    • 如果我不使用帖子,我只会得到 405,但当我这样做时会显示 400
    • 我建议您在使用官方启动器时先监控发送的内容。然后你就知道该怎么做了。
    猜你喜欢
    • 1970-01-01
    • 2013-11-24
    • 2017-06-22
    • 1970-01-01
    • 1970-01-01
    • 2018-10-22
    • 2013-08-09
    • 1970-01-01
    • 2014-08-19
    相关资源
    最近更新 更多