HTTP multipart/form-data over Socket does not show the variables
-
25-06-2021 - |
Question
I'm now already trying for hours, but somehow i don't figure out the problem. Here is a sample of my Request:
POST /test/upload/upload.php HTTP/1.0
Host: localhost
User-Agent: TestBrowser
Content-Type: multipart/form-data, boundary=635131229269 //edited
Content-Length: 94
--635131229269
Content-Disposition: form-data; name="testme"
contender
--635131229269--
It is sended over standard TCP/IP Socket to a PHP Server, but the $_POST['testme'] Value is always empty.
Does someone can see the bug in this Request? --> solved
There is a \r\n at the end which doesn't show in the Code here.
Thank you, that solved my first problem.
Maybe you can help my with my second aswell. As i had seen on your profile you are well with C# and there is my second problem. I'm trying to upload a file to my server and the filedata somehow does not requested properly, i think it's because of the encoding, but i'm not sure.
_content = _content
+ "--" + boundary + Environment.NewLine
+ "Content-Disposition: form-data; name=\"" + this._FileVarName + "\"; filename=\"" + Path.GetFileName(this._FilePath) + "\""
+ Environment.NewLine + "Content-Transfer-Encoding: application/octet-stream"
+ Environment.NewLine + Environment.NewLine;
mainContent = this.Combine(Encoding.UTF8.GetBytes(_content), StreamFile(this._FilePath));
private Byte[] StreamFile(string Path)
{
FileStream fs = new FileStream(Path, FileMode.Open, FileAccess.Read);
Byte[] ImageData = new byte[fs.Length];
fs.Read(ImageData, 0, System.Convert.ToInt32(fs.Length));
fs.Close();
return ImageData;
}
Solution
You have to specify the boundary (on line 4) without the leading dashes. Also I only count 92 bytes of payload.
OTHER TIPS
Just to make this answer a bit more complete, there has to be a semicolon after "multipart/form-data" (see w3c forms spec)
Content-Type: multipart/form-data; boundary=635131229269