I want to build my project to WebGL and I need to use UnityWebRequest instead of HTTPWebRequest because System.Net namespaces is not supported on WebGL.
I am having issues converting the working HTTPRequest to a UnityWebRequest.
/// Opens A Simple "webserver" On localhost:8080 For The Auth Redirect To Land On.
private void StartLocalWebserver()
{
HttpListener httpListener = new HttpListener();
httpListener.Prefixes.Add(twitchRedirectUrl);
httpListener.Start();
httpListener.BeginGetContext(new AsyncCallback(IncomingHttpRequest), httpListener);
}
/// Handles The Incoming HTTP Request
/// <param name="result"></param>
private void IncomingHttpRequest(IAsyncResult result)
{
string code;
string state;
HttpListener httpListener;
HttpListenerContext httpContext;
HttpListenerRequest httpRequest;
HttpListenerResponse httpResponse;
string responseString;
// Get Back The Reference To Our Http Listener
httpListener = (HttpListener) result.AsyncState;
// Fetch The Context Object
httpContext = httpListener.EndGetContext(result);
// The Context Object Has The Request Object For Us, That Holds Details About The Incoming Request
httpRequest = httpContext.Request;
code = httpRequest.QueryString.Get("code");
state = httpRequest.QueryString.Get("state");
// Check That We Got A Code Value And The State Value Matches Our Remembered One
if ((code.Length > 0) && (state == _twitchAuthStateVerify))
{
// If All Checks Out, Use The Code To Exchange It For The Actual Auth Token At The API
GetTokenFromCode(code);
isAuthReady = true;
UserID(_authToken);
}
// Build A Response To Send An "ok" Back To The Browser For The User To See
httpResponse = httpContext.Response;
responseString = "<html><body><b>DONE!</b><br>(You can close this tab/window now)</body></html>";
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
// Send The Output To The Client Browser
httpResponse.ContentLength64 = buffer.Length;
System.IO.Stream output = httpResponse.OutputStream;
output.Write(buffer, 0, buffer.Length);
output.Close();
// The HTTP Listener Has Served It's Purpose, Shut It Down
httpListener.Stop();
}
In these two methods I need to use UnityWebRequest instead of Http