(400)错误请求,无法获得响应(自定义Minecraft发射器)

时间:2015-11-15 07:33:13

标签: c# http-post

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

  

远程服务器返回错误:(400)错误请求

我已经通过其他人编写了许多不同的脚本,并通过将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没有正确的数据

1 个答案:

答案 0 :(得分:0)

试试这个:

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