/// <summary> /// 使用用户名和密码登录到Yggdrasil服务器 /// </summary> /// <param name="username">用户名</param> /// <param name="password">密码</param> /// <param name="clientToken">客户端标识</param> /// <param name="requestUser">是否请求账号数据</param> /// <returns></returns> public YggdrasilError Authenticate(string username, string password, Guid?clientToken = null, bool?requestUser = null) { JObject jsonData = new JObject(); jsonData["agent"] = new JObject(); jsonData["agent"]["name"] = "Minecraft"; jsonData["agent"]["version"] = 1; jsonData["username"] = username; jsonData["password"] = password; if (clientToken.HasValue) { jsonData["clientToken"] = clientToken.Value.ToString("N"); } if (requestUser.HasValue) { jsonData["requestUser"] = requestUser.Value; } byte[] jsonByteArray = Encoding.UTF8.GetBytes(jsonData.ToString()); try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + "/authenticate"); request.Method = "POST"; request.Timeout = 12000; request.ContentType = "application/json"; request.ContentLength = jsonByteArray.Length; Stream stream = request.GetRequestStream(); stream.Write(jsonByteArray, 0, jsonByteArray.Length); stream.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8); if (Convert.ToInt32(response.StatusCode) == 200) { data = YggdrasilInfo.Parse(JObject.Parse(reader.ReadToEnd())); return(YggdrasilError.Null()); } else { return(YggdrasilError.RequestError(JObject.Parse(reader.ReadToEnd()))); } } catch (Exception e) { return(YggdrasilError.Exception(e)); } }
public YggdrasilError Refresh(Guid accessToken, Guid clientToken, bool?requestUser = null) { JObject jsonData = new JObject(); jsonData["accessToken"] = accessToken.ToString("N"); jsonData["clientToken"] = clientToken.ToString("N"); if (requestUser.HasValue) { jsonData["requestUser"] = requestUser.Value; } byte[] jsonByteArray = Encoding.UTF8.GetBytes(jsonData.ToString()); try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + "/refresh"); request.Method = "POST"; request.Timeout = 12000; request.ContentType = "application/json"; request.ContentLength = jsonByteArray.Length; Stream stream = request.GetRequestStream(); stream.Write(jsonByteArray, 0, jsonByteArray.Length); stream.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8); if (Convert.ToInt32(response.StatusCode) == 200) { data = YggdrasilInfo.Parse(JObject.Parse(reader.ReadToEnd())); return(YggdrasilError.Null()); } else { return(YggdrasilError.RequestError(JObject.Parse(reader.ReadToEnd()))); } } catch (Exception e) { return(YggdrasilError.Exception(e)); } }