I have a Raspberry Pi server where if I request GET `https://192.168.1.2/stream`, the server will ask for authentication, then provide a snapshot image of the webcam. I use `HttpWebRequest` for authentication and to allow all SSL certificates (also, there's `HttpWebResponse` which pairs with it), but `UnityWebRequest` is good at requesting an image. How would I combine the both into one request? Should I use `HttpWebRequest` or `UnityWebRequest`? (I don't use `WWW` and `WWWForm` for inability to ignore SSL errors, or allow all SSL certificates).
In one request, I need to:
- Ignore SSL errors, or accept all SSL certificates
- Authenticate my identity with username and password
- Receive an image texture
I'm basically looking for the '*__One Class to Rule Them All__*'
Thank you!
using UnityEngine; // Main stuff
using UnityEngine.UI; // For 'urlInput'
using UnityEngine.Networking; // For 'UnityWebRequest'
using System.Collections; // Main stuff
using System.Net; // Main Network library, most important
using System.Net.Security; // For 'SslPolicyErrors'
using System.Security.Cryptography.X509Certificates; // For building the trusted certificates list
public class Stream_HTTPS : MonoBehaviour {
public InputField urlInput; // Format: 0.0.0.0
private string url;
public float GetRate = 0.15f;
private float nextGet = 0;
IEnumerator GetTexture() {
url = urlInput.text;
Renderer renderer = GetComponent();
ServicePointManager.ServerCertificateValidationCallback = TrustCertificate;
// ===============
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://" + url + "/stream");
request.ContentType = "image/jpg";
request.PreAuthenticate = true;
request.Credentials = new NetworkCredential ("Robot","password123");
request.Method = "GET";
// ^^^
// Combine
// vvv
UnityWebRequest www = UnityWebRequest.GetTexture ("http://" + url + "/stream");
yield return www.Send();
renderer.material.mainTexture = ((DownloadHandlerTexture)www.downloadHandler).texture;
// ===============
}
void Update() {
if (Time.time > nextGet) {
StartCoroutine (GetTexture ());
nextGet = Time.time + GetRate;
}
}
private static bool TrustCertificate(object sender, X509Certificate x509Certificate, X509Chain x509Chain, SslPolicyErrors sslPolicyErrors) {
// Accept all Certificates
return true;
}
}
↧