簡體   English   中英

Unity HttpWebRequest 響應顯示為 HTML 而不是 XML

[英]Unity HttpWebRequest response shows as HTML instead of XML

我正在嘗試使用 HttpWebRequest WebRequest 從 Unity 發出 POST 請求,我只得到 HTML 代碼,而在 Postman 中我得到了正確的結果。

我嘗試過將 HttpWebRequest 與 StreamWriter 一起使用,我嘗試以正常方式和異步方式進行操作。

我使用的 function

    public void GetResults()
    {
        byte[] bytes;
        bytes = Encoding.ASCII.GetBytes("<?xml version=\"1.0\" encoding=\"UTF - 8\"?><blabla>asdasd</blabla> ");
        string str = "[MY_API]";
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(str);
        req.Method = "POST";
        req.ContentType = "text/xml";
        string strRequest = Encoding.ASCII.GetString(bytes);
        req.ContentLength = strRequest.Length;
        StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
        streamOut.Write(strRequest);
        streamOut.Close();
        StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
        string strResponse = streamIn.ReadToEnd();
        print(strResponse);
        streamIn.Close();

    }

預期的打印是帶有一些數據的 XML 代碼,但實際打印是

<html>
<body>
<h1>
200
</h1>
</body>
</html>

您需要指定接受類型:

req.Accept="application/xml" //or */*

我用過 Unity3dAzure 開源很棒的 package,RESTClient 它解決了我所有的問題。 非常容易使用。

https://github.com/Unity3dAzure/RESTClient

Note: It looks like for XML what I'm doing is not enough, this package handles XMl and JSON in a good way.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM