【问题标题】:UnityWebRequest how to print all Request headersUnityWebRequest 如何打印所有请求标头
【发布时间】:2017-06-30 22:49:03
【问题描述】:

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

另外,这些标头存储在代码中的什么位置?

【问题讨论】:

  • “这些标头是在代码中的什么位置添加的?” 我认为您应该改写一下,因为我不明白。另外,您要打印的标头是UnityWebRequest 发送的标头还是从服务器接收的标头?
  • 我提到了“请求标头”,这应该是不言自明的。但万一……那些是发送到服务器的。我的第二个问题是指它们存储在哪里?哪个对象?

标签: c# http unity3d header request


【解决方案1】:

这些标头存储在代码中的什么位置?

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

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

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

您必须在另一个Thread 中使用HttpListener 创建一个本地服务器,使用UnityWebRequest 连接到它,然后从HttpListener API 的HttpListenerRequest 检索所有标头并将它们存储在@987654327 @.然后您可以关闭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;
    }
}

输出Unity 5.6.03f

键:主机 - 值:本地主机:8080

键:用户代理 - 值:UnityPlayer/5.6.0f3 (UnityWebRequest/1.0, libcurl/7.51.0-DEV)

键:接受 - 值:*/*

KEY:Accept-Encoding - VALUE:身份

KEY:Content-Type - VALUE:application/x-www-form-urlencoded

键:X-Unity-版本 - 值:5.6.0f3

键:内容长度 - 值:0

【讨论】:

    猜你喜欢
    • 2019-01-30
    • 1970-01-01
    • 2013-10-25
    • 1970-01-01
    • 2013-03-19
    • 2017-01-24
    • 2013-05-30
    • 2011-04-22
    相关资源
    最近更新 更多