- Home /
Any sample code for doing a HEAD web request?
I'm looking for any example of either WWW or UnityWebRequest which does a HEAD request. Reason: I want to query the server for the size of a file before attempting to download it.
Answer by Bunny83 · Jun 03, 2017 at 09:06 AM
Well, in general you can do this:
IEnumerator Request()
{
UnityWebRequest req = UnityWebRequest.Head("http://192.168.178.23");
yield return req.Send();
Debug.Log("Size:" + req.GetResponseHeader("Content-Length"));
}
// [...]
StartCoroutine(Request());
However be warned that some webservers nowadays don't allow HEAD or don't return a "Content-Length" header. A HEAD request should return the same headers as a GET request by definition. Though it's not required. For example the Unity Answers webserver returns a "405 Method Not Allowed"
response. The google server (at least the german one) only returns a Content-Length header when you request a specific URI. If you just request "a path" (where the server returns the index.html) no Content-Length is included. The same holds true for the free hoster where i lately put up my web examples.
The WWW class by definition only support GET and POST requests.
Your answer
![](https://koobas.hobune.stream/wayback/20220612123401im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Web build: WWW downloads correctly, but www.assetBundle is Null 0 Answers
UnityWebRequest - 403 Forbidden 0 Answers
WWW request works in Editor but not in IOS? 1 Answer
Web radio playing inside unity? 2 Answers
Invoke www.error in PHP 1 Answer