首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >UnityWebRequest如何打印所有请求头

UnityWebRequest如何打印所有请求头
EN

Stack Overflow用户
提问于 2017-06-30 22:49:03
回答 1查看 6.1K关注 0票数 4

有没有一种方法可以打印来自UnityWebRequest API的所有请求头?(我特别感兴趣的是自动添加的“x-unity”和“用户-agent”)。

此外,这些标头存储在代码中的何处?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-01 18:20:48

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

它们会自动添加到本机端的变量(C++)中。在Unity5.6.03f发行版中,您无法访问这些与官方API一起自动添加到UnityWebRequest API中的标头。您甚至不能使用反射来完成这个任务,因为它只有一个写函数,而没有读取函数。

有没有一种方法可以打印来自UnityWebRequest API的所有请求头?

,但这很棘手,因为您不能使用官方API或反射来完成这一任务。

您必须在另一个Thread中使用Thread创建一个本地服务器,用UnityWebRequest连接到它,然后从HttpListener API的HttpListenerRequest检索所有头部,并将它们存储在List.You中,然后关闭HttpListener服务器。

代码语言:javascript
运行
复制
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;
    }
}

带有United5.6.03f的输出

键: Host - VALUE: localhost:8080 关键词:User VALUE: UnityPlayer/5.6.0f3 (UnityWebRequest/1.0,libcurl/7.51.0-DEV) 键:接受-值:*/* 键:接受-编码-值:标识 键:内容-类型-值: application/x-www-form-urlencoded 键:X- VALUE: 5.6.0f3 键:内容长度-值:0

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44855721

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档