138 lines
4.1 KiB
C#
138 lines
4.1 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Net;
|
|
using System.Net.NetworkInformation;
|
|
using System.Net.Sockets;
|
|
using System.Text;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
public enum ADDRESSFAM
|
|
{
|
|
IPv4, IPv6
|
|
}
|
|
public static class InterfaceData
|
|
{
|
|
/// <summary>
|
|
/// 发送Get请求
|
|
/// </summary>
|
|
/// <param name="url">地址</param>
|
|
/// <param name="dic">请求参数定义</param>
|
|
/// <returns></returns>
|
|
public static string HttpWebRequestGet(string url, Dictionary<string, string> dic)
|
|
{
|
|
string result = "";
|
|
StringBuilder builder = new StringBuilder();
|
|
builder.Append(url);
|
|
if (dic.Count > 0)
|
|
{
|
|
builder.Append("?");
|
|
int i = 0;
|
|
foreach (var item in dic)
|
|
{
|
|
if (i > 0)
|
|
builder.Append("&");
|
|
builder.AppendFormat("{0}={1}", item.Key, item.Value);
|
|
i++;
|
|
}
|
|
}
|
|
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(builder.ToString());
|
|
//添加参数
|
|
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
|
|
Stream stream = resp.GetResponseStream();
|
|
try
|
|
{
|
|
//获取内容
|
|
using (StreamReader reader = new StreamReader(stream))
|
|
{
|
|
result = reader.ReadToEnd();
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
stream.Close();
|
|
}
|
|
Debug.Log("result: " + result);
|
|
return result;
|
|
}
|
|
/// <summary>
|
|
/// Post请求
|
|
/// </summary>
|
|
/// <param name="url"></param>
|
|
/// <param name="fromDic"></param>
|
|
/// <param name="textCallBack"></param>
|
|
/// <returns></returns>
|
|
public static IEnumerator UnityWebRequestPost(string url, Dictionary<string, string> fromDic, Action<string> textCallBack)
|
|
{
|
|
WWWForm form = new WWWForm();
|
|
foreach (var item in fromDic)
|
|
{
|
|
form.AddField(item.Key, item.Value);
|
|
}
|
|
UnityWebRequest request = UnityWebRequest.Post(url, form);
|
|
request.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
|
yield return request.SendWebRequest();
|
|
if (request.isHttpError || request.isNetworkError)
|
|
{
|
|
Debug.LogError(request.error);
|
|
}
|
|
else
|
|
{
|
|
string result = request.downloadHandler.text;
|
|
textCallBack?.Invoke(result);
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 获取本机IP
|
|
/// </summary>
|
|
/// <param name="Addfam">要获取的IP类型</param>
|
|
/// <returns></returns>
|
|
public static string GetIP(ADDRESSFAM Addfam)
|
|
{
|
|
if (Addfam == ADDRESSFAM.IPv6 && !Socket.OSSupportsIPv6)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
string output = "";
|
|
|
|
foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces())
|
|
{
|
|
#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
|
|
NetworkInterfaceType _type1 = NetworkInterfaceType.Wireless80211;
|
|
NetworkInterfaceType _type2 = NetworkInterfaceType.Ethernet;
|
|
|
|
if ((item.NetworkInterfaceType == _type1 || item.NetworkInterfaceType == _type2) && item.OperationalStatus == OperationalStatus.Up)
|
|
#endif
|
|
{
|
|
foreach (UnicastIPAddressInformation ip in item.GetIPProperties().UnicastAddresses)
|
|
{
|
|
//IPv4
|
|
if (Addfam == ADDRESSFAM.IPv4)
|
|
{
|
|
if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
|
|
{
|
|
output = ip.Address.ToString();
|
|
//Debug.Log("IP:" + output);
|
|
}
|
|
}
|
|
|
|
//IPv6
|
|
else if (Addfam == ADDRESSFAM.IPv6)
|
|
{
|
|
if (ip.Address.AddressFamily == AddressFamily.InterNetworkV6)
|
|
{
|
|
output = ip.Address.ToString();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return output;
|
|
}
|
|
|
|
}
|