UnityWebRequst. CORS problem.
Hi! I'm writing a project on Unity3D under WEBGL. The server for the project is written in C # using the HttpListener. Problem: the request reaches the server, but the request body is empty, i.e. .hasEntityBody == false. In the editor and on the Standalone version everything works. On Webgl, the coroutine is infinitely waiting for the SendWebRequest () method. If after the request to close the server (not immediately), the browser displays a message: The request from an outside source is blocked: A single-source policy prohibits reading the remote resource to http: // localhost: 5555 /. (Cause: CORS request failed). The joke is that earlier requests worked in WebGL, but after I finished all the commands, or something else, I do not even know what else I could change there, suddenly everything stopped working. Other methods such as Post and Get also do not work. Example query:
IEnumerator TrySendMessage(string message)
{
RequestData requestData = new RequestData()
{
isMailMessage = true,
mailData = new MailData()
{
targetAddress = selectedCompanyData.email,
senderAddress = nativeCompany.email,
senderName = nativeCompany.companyName,
senderPhone = nativeCompany.phoneNumber,
message = message
}
};
byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(JsonUtility.ToJson(requestData));
using (UnityWebRequest www = UnityWebRequest.Put("http://localhost:5555", byteArray))
{
yield return http://www.SendWebRequest();
if (http://www.isNetworkError || http://www.isHttpError)
print(http://www.error);
else
{
print(http://www.downloadHandler.text);
UI.inst.SetMailWindowState(false);
}
}
}
Here's the server:
static async Task Listen()
{
HttpListener httpListener = new HttpListener();
httpListener.Prefixes.Add("http://localhost:5555/");
httpListener.Start();
Console.WriteLine("Started...");
while (true)
{
HttpListenerContext context = await httpListener.GetContextAsync();
HttpListenerRequest request = context.Request;
HttpListenerResponse response = context.Response;
//response.AddHeader("Access-Control-Allow-Credentials", "true");
response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time");
response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, OPTIONAL");
response.AddHeader("Access-Control-Allow-Origin", "*");
Console.WriteLine("New request...");
if (request.HasEntityBody)
{
Stream stream = response.OutputStream;
BinaryWriter binaryWriter = new BinaryWriter(stream);
//BinaryReader binaryReader = new BinaryReader(request.InputStream);
byte[] byteArray = new byte[request.ContentLength64];
request.InputStream.Read(byteArray, 0, (int)request.ContentLength64);
RequestData requestData = JsonConvert.DeserializeObject<RequestData>(System.Text.Encoding.UTF8.GetString(byteArray));
if (requestData.isDataRequest)
{
//...
binaryWriter.Write("OK");
}
else if (requestData.isAuthentification)
{
//...
binaryWriter.Write("OK");
...
}
The server outputs "New request" but does not enter the condition, since the body is empty. There where three points - some operations. Earlier when I wrote the server and there were problems with CORS, the browser in the console displayed in brackets what exactly the problem was, and now it's just a failure of the CORS request ... although like all the necessary headers in the response are spelled out. From the browser, only the pre-flight empty Options request is sent. Thank you.
Your answer
Follow this Question
Related Questions
How to make a script wait for a UnityWebRequest to finish for WebGL without a coroutine? 1 Answer
Wait for value to not be null until thread finishes 0 Answers
2021.2.7: DownloadHandlerTexture and UnityWebRequestTexture not recognized? 1 Answer
MultipartFormDataSection not working - Error: 403 0 Answers
Error in BUILD with UnityWebRequest but not in Editor 0 Answers