Pray god, with the following code into UnityWebRequest request
IEnumerator Get (url string, the string auth, string json)
{
Dictionary
Byte [] postBytes;
The header. The Add (" the content-type ", "application/json");//add the header
If (auth!=null)
{
The header. The Add (" Authorization ", auth);
}
If (json!=null)
{
//POST
//to convert data into a json data stream
PostBytes=System. Text. Encoding. UTF8. GetBytes (json);
}
The else
{
//GET
//this case does not need the json data so null
PostBytes=null;
}
WWW WWW=new WWW (url, postBytes, m_dir);
Yield return WWW.
If (www.error!=null)
{
The Debug Log (" the error is: "+ www.error);
}
The else
{
The Debug Log (" request result: "+ www.text);
}
}
CodePudding user response:
Well, check the official documentation for the ahCodePudding user response:
using System;using System.Collections;
using System.IO;
using UnityEngine.Networking;
Public class HttpDownLoad
{
Public float progress {get; Private set; }
Public bool isDone {get; Private set; }
Private bool isStop;
Public IEnumerator Start (url string, the string filePath, Action callBack)
{
Var headRequest=UnityWebRequest. Head (url);
Yield return headRequest. SendWebRequest ();
Var totalLength=long. Parse (headRequest GetResponseHeader (" the Content - Length "));
Var dirPath=Path. GetDirectoryName (filePath);
if (! Directory. The Exists (dirPath))
{
Directory. CreateDirectory (dirPath);
}
Using (var fs=new FileStream (filePath, FileMode OpenOrCreate, FileAccess. Write))
{
Var fileLength=fs. Length;
If (fileLength & lt; TotalLength)
{
Fs. Seek (fileLength, SeekOrigin. Begin);
Var request=UnityWebRequest. Get (url);
Request. SetRequestHeader (" Range "and" bytes="+ + totalLength fileLength +" - ");
Request. SendWebRequest ();
Var index=0;
while (! Request. IsDone)
{
If (isStop) break;
Yield return null;
Var buff=request. DownloadHandler. Data;
If (buff!=null)
{
Var length=buff. Length - index;
Fs. Write (buff, index, length);
Index +=length;
The fileLength +=length;
If (fileLength==totalLength)
{
Progress=1 f;
}
The else
{
Progress=fileLength/(float) totalLength;
}
}
}
}
The else
{
Progress=1 f;
}
Fs. The Close ();
Fs. The Dispose ();
}
If (progress & gt;=1 f)
{
IsDone=true;
If (callBack!=null)
{
The callBack ();
}
}
}
Public void the Stop ()
{
IsStop=true;
}
}