Jquery Post To A .net Httplistener And Back Again
Solution 1:
Don't forget about the same origin policy restriction. Unless your javascript is hosted on http://localhost:8080
you won't to be able to send AJAX requests to this URL. A different port number is not allowed either. You will need to host your javascript file on an HTML page served from http://localhost:8080
if you want this to work. Or have your server send JSONP but this works only with GET requests.
Remark: make sure you properly dispose disposable resource on your server by wrapping them in using statements or your server might start leaking network connection handles.
Solution 2:
Don't forget to release the resources by closing the response.
Calling Close on the response will force the response to be sent through the underlying socket and will then Dispose all of its disposable objects.
In your example, the Close method is only called on the Output stream. This will send the response through the socket, but will not dispose any resources related to the response, which includes the output stream you referenced.
// Complete async GetContext and reference required objectsHttpListenerContextContext= Listener.EndGetContext(Result);
HttpListenerRequestRequest= Context.Request;
HttpListenerResponseResponse= Context.Response;
// Process the incoming request here// Complete the request and release it's resources by call the Close method
Response.Close();
Solution 3:
I do not see setting of content-type. Set the content-type to text/html
.
response.ContentType = "text/html";
Solution 4:
You can simplify the writing code a lot. Just use this:
// Construct a response.
string responseString = "<HTML><BODY> Hello world!</BODY></HTML>";
context.Response.Write(responseString);
No need for the OutputStream
or most of that other code. If you do have a reason to use it, note that you actually should not close the OutputStream
. When you use Resopnse.OutputStream
you're retrieving a reference to it but you're not taking ownership. It's still owned by the Response
object and will be closed properly when the Response
is disposed at the end of the request.
Post a Comment for "Jquery Post To A .net Httplistener And Back Again"