C# UnityWebRequest如何打印所有请求头
有没有办法从UnityWebRequestAPI打印所有请求头? (我对自动添加的“x-unity-version”和“用户代理”特别感兴趣) 另外,这些头文件在代码中存储在哪里 这些头文件存储在代码中的什么位置 它们会自动添加到本机端(C++)的变量中。从Unity 5.6.03f发行版开始,您无法访问这些头文件,这些头文件是通过官方API自动添加到C# UnityWebRequest如何打印所有请求头,c#,http,unity3d,header,request,C#,Http,Unity3d,Header,Request,有没有办法从UnityWebRequestAPI打印所有请求头? (我对自动添加的“x-unity-version”和“用户代理”特别感兴趣) 另外,这些头文件在代码中存储在哪里 这些头文件存储在代码中的什么位置 它们会自动添加到本机端(C++)的变量中。从Unity 5.6.03f发行版开始,您无法访问这些头文件,这些头文件是通过官方API自动添加到UnityWebRequestAPI的。你甚至不能使用反射来做这件事,因为它只有一个写功能而没有读功能 有没有办法从UnityWebRequest
UnityWebRequest
API的。你甚至不能使用反射来做这件事,因为它只有一个写功能而没有读功能
有没有办法从UnityWebRequestAPI打印所有请求头
是的,但这很棘手,因为您无法使用官方API或反射来实现这一点
您必须在另一个线程中使用HttpListener
创建本地服务器,使用UnityWebRequest
连接到它,然后从HttpListener
API的HttpListenerRequest
中检索所有头,并将它们存储在列表中。然后可以关闭HttpListener
服务器
volatile bool serverIsReady = false;
//Holds List of all the headers from received from UnityWebRequest request
List<HeaderInfo> headers = new List<HeaderInfo>();
void Start()
{
StartCoroutine(getUnityWebRequestHeaders());
}
IEnumerator getUnityWebRequestHeaders()
{
//Start Http Server
ThreadPool.QueueUserWorkItem(new WaitCallback(RunInNewThread), new string[] { "http://*:8080/" });
//Wait for server to actually start
while (!serverIsReady)
yield return null;
Debug.LogWarning("Server is ready");
yield return new WaitForSeconds(0.2f);
UnityWebRequest www = UnityWebRequest.Post("http://localhost:8080", "");
yield return www.Send();
//Check if connections was successfull
if (!www.isError && www.downloadHandler.text.Contains("SUCCESS"))
{
Debug.LogWarning("Connection was successfull");
}
//Show all the headers from UnityWebRequest
for (int i = 0; i < headers.Count; i++)
{
Debug.Log("KEY: " + headers[i].header + " - VALUE: " + headers[i].value);
}
}
private void RunInNewThread(object a)
{
//Cast parameter back to string array
string[] serverPrefix = (string[])a;
//Start server with the provided parameter
SimpleListenerExample(serverPrefix);
}
//Creates a http server
public void SimpleListenerExample(string[] prefixes)
{
serverIsReady = false;
if (!HttpListener.IsSupported)
{
Debug.LogWarning("Windows XP SP2 or Server 2003 is required to use the HttpListener class.");
return;
}
//URI prefixes are required,
//for example "http://contoso.com:8080/index/".
if (prefixes == null || prefixes.Length == 0)
throw new ArgumentException("prefixes");
//Create a listener.
HttpListener listener = new HttpListener();
//Add the prefixes.
foreach (string s in prefixes)
{
listener.Prefixes.Add(s);
}
listener.Start();
Debug.LogWarning("Listening...");
serverIsReady = true;
//Note: The GetContext method blocks while waiting for a request.
HttpListenerContext context = listener.GetContext();
HttpListenerRequest request = context.Request;
//Obtain a response object.
HttpListenerResponse response = context.Response;
//Construct a response.
string responseString = "<HTML><BODY>SUCCESS</BODY></HTML>";
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
//Get a response stream and write the response to it.
response.ContentLength64 = buffer.Length;
System.IO.Stream output = response.OutputStream;
output.Write(buffer, 0, buffer.Length);
//Get all the headers sent from UnityWebRequest and add them to the List
addHeaders(request);
//You must close the output stream.
output.Close();
listener.Stop();
}
//Get all the headers sent from UnityWebRequest
void addHeaders(HttpListenerRequest request)
{
System.Collections.Specialized.NameValueCollection receivedHeaders = request.Headers;
for (int i = 0; i < receivedHeaders.Count; i++)
{
string key = receivedHeaders.GetKey(i);
string value = receivedHeaders.Get(i);
headers.Add(new HeaderInfo(key, value));
}
}
//Hold header and value
public class HeaderInfo
{
public string header;
public string value;
public HeaderInfo(string header, string value)
{
this.header = header;
this.value = value;
}
}
volatile bool serverIsReady=false;
//保存从UnityWebRequest请求接收的所有标头的列表
列表标题=新列表();
void Start()
{
start例程(getUnityWebRequestHeaders());
}
IEnumerator getUnityWebRequestHeaders()
{
//启动Http服务器
QueueUserWorkItem(新的WaitCallback(RunInNewThread),新的字符串[]{“http://*:8080/”});
//等待服务器实际启动
而(!服务器已就绪)
收益返回空;
LogWarning(“服务器已准备就绪”);
收益率返回新WaitForSeconds(0.2f);
UnityWebRequest www=UnityWebRequest.Post(“http://localhost:8080", "");
收益率返回www.Send();
//检查连接是否成功
如果(!www.isError&&www.downloadHandler.text.Contains(“成功”))
{
Debug.LogWarning(“连接成功”);
}
//显示UnityWebRequest中的所有标题
对于(int i=0;i
输出带有Unity 5.6.03f:
键:主机-值:localhost:8080
键:用户代理-值:UnityPlayer/5.6.0f3
(UnityWebRequest/1.0,libcurl/7.51.0-DEV)
键:接受-值:*/*
键:接受编码-值:标识
键:内容类型-值:application/x-www-form-urlencoded
键:X-Unity-Version-值:5.6.0f3
关键字:内容长度-值:0
“这些头在代码中添加到哪里?”我认为您应该重新措辞,因为我不理解这一点。另外,您要打印的头,是UnityWebRequest
发送的头还是从服务器接收的头?我提到了“请求头”,这应该是不言自明的。。但万一。。。这些是发送到服务器的。我的第二个问题是指它们存放在哪里?哪个物体?