Get Unity WWW Response Status Code

I am working with Unity WWW for some Rest API request. But it does not support getting response status (return text and error only). Any solution for this? Thanks!

+6
source share
1 answer

edit: Ever since I asked this question, Unity has released a new infrastructure for HTTP messages called UnityWebRequest. It is much more modern than WWW and provides ultimate access to the response code, as well as greater flexibility in headers, HTTP verbs, etc. You should probably use this instead of www.


apparently you need to parse it yourself from the response headers.

this looks like a trick:

public static int getResponseCode(WWW request) { int ret = 0; if (request.responseHeaders == null) { Debug.LogError("no response headers."); } else { if (!request.responseHeaders.ContainsKey("STATUS")) { Debug.LogError("response headers has no STATUS."); } else { ret = parseResponseCode(request.responseHeaders["STATUS"]); } } return ret; } public static int parseResponseCode(string statusLine) { int ret = 0; string[] components = statusLine.Split(' '); if (components.Length < 3) { Debug.LogError("invalid response status: " + statusLine); } else { if (!int.TryParse(components[1], out ret)) { Debug.LogError("invalid response code: " + components[1]); } } return ret; } 
+12
source

All Articles