Question by
HazelShi103 · Dec 03, 2018 at 06:40 AM ·
unityeditoraugmented-realitywebrequestvideo streamingauthentication
HttpWebRequest doesn't work with Hololens
Hey guys, I'm trying to stream CCTV live video to Hololens, it works fine in Unity Editor but failed in Hololens with an exception of "connect failure TLS support not available".
I know UnityWebRequest works better for Hololens but I have to use the HttpWebRequestin this case because of the credentials.
Is it because Hololens doesn't support HttpWebRequest? I'm using unity 2018.1.9f2.
My code:
public void GetVideo()
{
texture = new Texture2D(2, 2);
HttpWebRequest www = (HttpWebRequest)WebRequest.Create(sourceURL);
www.Credentials = new NetworkCredential("Username", "password");
ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback((sender, certificate, chain, policyErrors) => { return true; });
debugText.text += "\ngetting certification";
try
{
HttpWebResponse resp = (HttpWebResponse)www.GetResponse();
stream = resp.GetResponseStream();
}
catch(Exception ex)
{
debugText.text += "\n"+ex.Message.ToString();
}
StartCoroutine(GetFrame());
}
IEnumerator GetFrame()
{
Byte[] JpegData = new Byte[65536];
while (stream != null)
{
if(streamOnce == false)
{
streamOnce = true;
}
int bytesToRead = FindLength(stream);
print(bytesToRead);
if (bytesToRead == -1)
{
print("End of stream");
yield break;
}
int leftToRead = bytesToRead;
while (leftToRead > 0)
{
leftToRead -= stream.Read(JpegData, bytesToRead - leftToRead, leftToRead);
yield return null;
}
MemoryStream ms = new MemoryStream(JpegData, 0, bytesToRead, false, true);
texture.LoadImage(ms.GetBuffer());
frame.material.mainTexture = texture;
stream.ReadByte(); // CR after bytes
stream.ReadByte(); // LF after bytes
}
}
int FindLength(Stream stream)
{
int b;
string line = "";
int result = -1;
bool atEOL = false;
while ((b = stream.ReadByte()) != -1)
{
if (b == 10) continue; // ignore LF char
if (b == 13)
{ // CR
if (atEOL)
{ // two blank lines means end of header
stream.ReadByte(); // eat last LF
return result;
}
if (line.StartsWith("Content-Length:"))
{
result = Convert.ToInt32(line.Substring("Content-Length:".Length).Trim());
}
else
{
line = "";
}
atEOL = true;
}
else
{
atEOL = false;
line += (char)b;
}
}
return -1;
}
Comment