Answer the question
In order to leave comments, you need to log in
Request cancelled. Is it possible to close the stream before all bytes have been written?
I have a mini-launcher for minecraft, I need to make a user authorization.
Here is the code:
var webRequest = System.Net.WebRequest.Create("https://authserver.mojang.com/authenticate");
if (webRequest != null)
{
string reqContent = "{\"agent\": {\"name\": \"Minecraft\",\"version\": 1},\"username\": \"" + auth_player_name.Text + "\",\"password\": \"" + auth_player_password.Text + "\",\"clientToken\": \"" + NewMCUUID().ToString() + "\",\"requestUser\": true}";
byte[] bytes = Encoding.UTF8.GetBytes(reqContent.ToArray());
webRequest.Method = "POST";
webRequest.Timeout = Timeout.Infinite;
webRequest.ContentType = "application/json";
webRequest.ContentLength = bytes.Length;
using (Stream s = webRequest.GetRequestStream())
{
using (StreamWriter sw = new StreamWriter(s))
{
sw.Write(bytes);
}
}
using (Stream s = webRequest.GetResponse().GetResponseStream())
{
using (StreamReader sr = new StreamReader(s))
{
MessageBox.Show(sr.ReadToEnd());
}
}
}
Answer the question
In order to leave comments, you need to log in
Such feeling that business in Using.
For an object that is wrapped inside a Using, Dispose() is automatically called when it exits.
And the stream to which you wrote the POST data is sent to the garbage collector, even before it is sent.
You don't need SW at all, if you write bytes there, you can write to the stream right away.
In general, remove using and explicitly call Dispose after the request is completed.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question