UnityWebRequest如何打印所有请求头

Joh*_*y19 4 c# header http request unity-game-engine

有没有办法从 UnityWebRequest API 打印所有请求标头?(我对自动添加的“x-unity-version”和“user-agent”特别感兴趣)

另外,这些标头存储在代码中的哪里?

Pro*_*mer 5

这些标头存储在代码中的哪里?

它们会自动添加到本机端 (C++) 的变量中。从Unity 5.6.03fUnityWebRequest版本开始,您无法使用官方 API访问这些自动添加到 API 的标头。您甚至不能使用反射来执行此操作,因为它只有写入功能而没有读取功能。

有没有办法从 UnityWebRequest API 打印所有请求标头?

是的,但这很棘手,因为您无法使用官方 API 或反射来做到这一点。

您必须HttpListener在另一个 中创建一个本地服务器Thread,连接到它,然后从API 中UnityWebRequest检索所有标头并将它们存储在 中。然后您可以关闭服务器。HttpListenerRequestHttpListenerListHttpListener

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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 版本 - 值:5.6.0f3

键:内容长度 - 值:0