You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

204 lines
5.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Runtime.InteropServices;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
public class EducoderUserCheck : MonoBehaviour
{
public string _cookie = "";
[DllImport("__Internal")]
private static extern string GetCookie(string url);
public string GetCookeByDllASB(string url)
{
Debug.Log(GetCookie(url));
return GetCookie(url);
}
// Start is called before the first frame update
void Start()
{
//var url = "https://hyl.educoder.net/api/users/get_user_info.json?objective_type=shixun&objective_id=xolfhc7f&school=1";
//var url2 = "https://hyl.educoder.net";
//StartCoroutine(GetCookieByIE(url2));
//GetCookeByDllASB("url");
//CheckEducoderUser();
string url = "https://edu-xnfz.educoder.net/";
string cookieHeader = "";
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
myHttpWebRequest.CookieContainer = new CookieContainer();
myHttpWebRequest.CookieContainer.SetCookies(new Uri(url), cookieHeader);
HttpWebResponse myresponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
_cookie = myresponse.Headers["Set-Cookie"];//获取验证码页面的Cookies
Debug.Log(_cookie);
}
private void Get(string url)
{
// 创建Cookie容器对象
CookieContainer cookieContainer = new CookieContainer();
// 创建Web请求对象并设置请求参数
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.CookieContainer = cookieContainer; // 将Cookie容器对象赋值给请求的Cookie容器
request.Method = "GET";
// 获取服务器响应对象
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// 读取响应数据
using (Stream stream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(stream, Encoding.UTF8);
string responseData = reader.ReadToEnd();
// 获取响应的Cookie并将其存储到Cookie容器中
CookieCollection cookies = response.Cookies;
cookieContainer.Add(cookies);
Debug.Log(cookies.Count);
Debug.Log(responseData);
Console.WriteLine(responseData);
//UserData jsonstr = JsonUtility.FromJson<UserData>(responseData);
//username = jsonstr.username;
reader.Close();
}
}
private void CheckEducoderUser()
{
var url = "https://hyl.educoder.net/api/users/get_user_info.json?objective_type=shixun&objective_id=xolfhc7f&school=1";
StartCoroutine(GetRequest(url));
////var url = "https://hyl.educoder.net";
//var cookie = Getcookie(url);
//HttpGetData(url, cookie);
//Get(url);
if (username == "游客" && !GameRoot.Instance.isFree)
{
Debug.Log("当前为游客");
GameRoot.Instance.userText.text = username;
GameRoot.Instance.userText.text = "游客";
#if !UNITY_EDITOR
GameRoot.Instance.userTip.SetActive(true);
#endif
}
}
#region 平台账号验证
private string HttpGetData(string url, string cookies)
{
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
myRequest.Headers.Add("cookie", cookies);
myRequest.Method = "GET";
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8);
string content = reader.ReadToEnd();
//UserData jsonstr = JsonUtility.FromJson<UserData>(content);
//username = jsonstr.username;
Debug.Log(cookies);
Debug.Log(username);
Debug.Log(content);
reader.Close();
return content;
}
IEnumerator GetCookieByIE(string url)
{
// 创建一个Web请求对象
UnityWebRequest request = UnityWebRequest.Get(url);
// 添加Cookie信息到请求头
//request.SetRequestHeader("Cookie", "name=value");
// 发送请求
yield return request.SendWebRequest();
// 检查是否有错误
if (request.isNetworkError)
{
Debug.Log(request.error);
}
else
{
// 解析响应数据获取Cookie的值
_cookie = request.GetResponseHeader("Set-Cookie");
var dict= request.GetResponseHeaders();
foreach (var item in dict)
{
if (item.Key.Equals("ETag")) continue;
//Debug.Log(item.Key + "-" + item.Value);
}
}
}
private string username = "";
IEnumerator GetRequest(string url)
{
UnityWebRequest webRequest = new UnityWebRequest();
webRequest = UnityWebRequest.Get(url);
yield return webRequest.SendWebRequest();
if (!string.IsNullOrEmpty(webRequest.error))
{
Debug.LogError(webRequest.error);
}
else
{
//Debug.Log(webRequest.downloadHandler.text);
UserData jsonstr = JsonUtility.FromJson<UserData>(webRequest.downloadHandler.text);
Debug.Log(webRequest.downloadHandler.text);
username = jsonstr.username;
// Debug.Log(username);
}
}
#endregion
}