I am writing a webserver based off the code in this link. I'm trying to get POST data from a form but I am having trouble getting that data. The webserver is meant to be self hosted. Its basically a control panel where I can add and edit these devices called stack lights. Here is my WebServer.Run method:
public void Run()
{
ThreadPool.QueueUserWorkItem((o) =>
{
Console.WriteLine("StackLight Web Server is running...");
try
{
while (_listener.IsListening)
{
ThreadPool.QueueUserWorkItem((c) =>
{
var ctx = c as HttpListenerContext;
try
{
// set the content type
ctx.Response.Headers[HttpResponseHeader.ContentType] = SetContentType(ctx.Request.RawUrl);
WebServerRequestData data = _responderMethod(ctx.Request);
string post = "";
if(ctx.Request.HttpMethod == "POST")
{
using(System.IO.StreamReader reader = new StreamReader(ctx.Request.InputStream, ctx.Request.ContentEncoding))
{
post = reader.ReadToEnd();
}
}
if(data.ContentType.Contains("text") || data.ContentType.Equals("application/json"))
{
// serve text/html,css,js & application/json files as UTF8
// images don't need to be served as UTF8, they don't have encodings
char[] chars = new char[data.Content.Length / sizeof(char)];
System.Buffer.BlockCopy(data.Content, 0, chars, 0, data.Content.Length);
string res = new string(chars);
data.Content = Encoding.UTF8.GetBytes(res);
}
// this writes the html out from the byte array
ctx.Response.ContentLength64 = data.Content.Length;
ctx.Response.OutputStream.Write(data.Content, 0, data.Content.Length);
}
catch (Exception ex)
{
ConfigLogger.Instance.LogCritical(LogCategory, ex);
}
finally
{
ctx.Response.OutputStream.Close();
ctx.Response.Close();
}
}, _listener.GetContext());
}
}
catch (Exception ex)
{
ConfigLogger.Instance.LogCritical(LogCategory, ex);
}
});
}
I am using a class called WebServerRequestData to get my pages, css, javascript and images so I can server them. That class looks like this:
public class WebServerRequestData
{
// Raw URL from the request object
public string RawUrl { get; set; }
// Content Type of the file
public string ContentType { get; set; }
// A byte array containing the content you need to serve
public byte[] Content { get; set; }
public WebServerRequestData(string data, string contentType, string rawUrl)
{
this.ContentType = contentType;
this.RawUrl = rawUrl;
byte[] bytes = new byte[data.Length * sizeof(char)];
System.Buffer.BlockCopy(data.ToCharArray(), 0, bytes, 0, bytes.Length);
this.Content = bytes;
}
public WebServerRequestData(byte[] data, string contentType, string rawUrl)
{
this.ContentType = contentType;
this.RawUrl = rawUrl;
this.Content = data;
}
}
And this is my form:
public static string EditStackLightPage(HttpListenerRequest request)
{
// PageHeadContent writes the <html><head>...</head> stuf
string stackLightPage = PageHeadContent();
// start of the main container
stackLightPage += ContainerDivStart;
string[] req = request.RawUrl.Split('/');
StackLightDevice stackLight = Program.StackLights.First(x => x.Name == req[2]);
stackLightPage += string.Format("<form action='/edit/{0}/update' method='post' enctype='multipart/form-data'>", stackLight.Name);
stackLightPage += string.Format("Stack Light<input type='text' id='inputName' value='{0}'>", stackLight.Name);
stackLightPage += string.Format("IP Address<input type='text' id='inputIp' value='{0}'>", stackLight.Ip);
stackLightPage += string.Format("Port Number<input type='text' id='inputPort' value='{0}'>", stackLight.Port);
stackLightPage += "<button type='submit'>Update</button>";
stackLightPage += "</form>";
// end of the main container
stackLightPage += ContainerDivEnd;
stackLightPage += PageFooterContent();
return stackLightPage;
}
It's just 3 fields: name, ip & port for a custom class that writes to some safety lights. Its being called from a SendResponse method in another class.
private static WebServerRequestData SendResponse(HttpListenerRequest request)
This is the segment where my form is being called, an example of the edit url is localhost:8080/edit/stackLight-Name and the update would be localhost:8080/edit/stackLight-Name/update. This is the code to check if the rawurl contains those routes:
if(request.RawUrl.Contains("edit"))
{
if (request.RawUrl.Contains("update"))
{
// get form data from the edit page and return to the edit
_resultString = WebServerHtmlContent.EditStackLightPage(request);
_data = new WebServerRequestData(_resultString, "text/html", request.RawUrl);
return _data;
}
_resultString = WebServerHtmlContent.EditStackLightPage(request);
_data = new WebServerRequestData(_resultString, "text/html", request.RawUrl);
return _data;
}
This is where I'm processing my requests. It's got a few if statements based on the HttpListenerRequest objects RawUrl property. I am trying to get my form data. In the section:
if(ctx.Request.HttpMethod == "POST")
{
using(System.IO.StreamReader reader = new StreamReader(ctx.Request.InputStream, ctx.Request.ContentEncoding))
{
post = reader.ReadToEnd();
}
}
I am able to get an InputStream but I am not getting form data. This is the data I am getting: "------WebKitFormBoundaryAGo7VbCZ2YC79zci--\r\n"
Shouldn't the input stream have my form data in it?
I'm not seeing any of the data from the form fields in the InputStream. I've tried using application/x-www-form-urlencoded but that returns a null InputStream from ctx.Request. (ctx is my HttpListenerContext object).
I read about using multipartform and application/x-www-form-urlencoded and have tried them both.
So far, multipart form gives me data (even though its not the form data) and the other doesn't.
I think I'm close to getting my form data to show up I'm just stuck at this point. Im not sure what to do.
Also, right now I am currently reading this similar post on stackoverflow
EDIT: After reading from that link, I have changed my web server run method to the following:
try
{
// set the content type
WebServerRequestData data = _responderMethod(ctx.Request);
string post = "";
if(ctx.Request.HttpMethod == "POST")
{
data.ContentType = ctx.Request.ContentType;
post = GetRequestPostData(ctx.Request);
}
ctx.Response.ContentLength64 = data.OutputBuffer.Length;
ctx.Response.OutputStream.Write(data.OutputBuffer, 0, data.OutputBuffer.Length);
}
And this is the method GetRequestPostData():
private static string GetRequestPostData(HttpListenerRequest request)
{
if (!request.HasEntityBody)
return null;
using(System.IO.Stream body = request.InputStream)
{
using(System.IO.StreamReader reader = new StreamReader(body, request.ContentEncoding))
{
return reader.ReadToEnd();
}
}
}
And I am still just getting "------WebKitFormBoundaryjiSulPEnvWX7MIeq--\r\n"
I've figured it out, I forgot to add a name to my form fields.