HttpListenerContext.Request.InputStream always null

896 views Asked by At

I send a request on port 60080

var XHR = new XMLHttpRequest();
XHR.open('GET', 'http://localhost:60080/api/products');
XHR.setRequestHeader("Content-Type", "application/json; charset=UTF-8");
XHR.body = JSON.stringify({ email: "[email protected]", response: { name: "Tester" } });
XHR.send();

and receive it using HttpListener

this.listener = new HttpListener();
listener.Prefixes.Add("http://+:60080/");
listener.Start();
listener.BeginGetContext(HandleRequest, listener);

so i get HttpListenerContext

HttpListenerContext context = listener.EndGetContext(result);

When i try to get InputStream (on Android or UWP) it is constantly null. ContentLength = 0; When i test on iOS InputStream not null and ContentLength > 0;

What is my problem?

UPDATE:

I solved my problem.

The fact that I sent a Post method, but actually came method OPTIONS.

I sent the headers in a response and then came the Post method

httpListenerContext.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With");
httpListenerContext.Response.AddHeader("Content-type", "application/json");
httpListenerContext.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
httpListenerContext.Response.AddHeader("Access-Control-Max-Age", "1728000");
httpListenerContext.Response.AppendHeader("Access-Control-Allow-Origin", "*");
httpListenerContext.Response.Close();
1

There are 1 answers

0
temiklis On BEST ANSWER

I solved my problem.

The fact that I sent a Post method, but actually came method OPTIONS.

this is due to CORS tehnology.

I sent the headers in a response and then came the Post method

httpListenerContext.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With");
httpListenerContext.Response.AddHeader("Content-type", "application/json");
httpListenerContext.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
httpListenerContext.Response.AddHeader("Access-Control-Max-Age", "1728000");
httpListenerContext.Response.AppendHeader("Access-Control-Allow-Origin", "*");
httpListenerContext.Response.Close();