I am attempting to get information from a web page that is generated after a button is selected. This information is generated from a Post request (I think) and I am trying to send this request using c#. All of my attempts for different post requests are resulting in a 404 error. I have also tried using extensions along with google chrome and internet explorer to find what information is being sent on the button press, however I have found nothing conclusive. I have seen posts regarding how to send post requests, but nothing has helped so far.
I have a web page that looks like this:
<FORM METHOD="POST" ACTION="/postYYYY">
<INPUT TYPE="submit" VALUE="YY YYYY">
</FORM>
</TD>
</TR>
<TR align="center">
<TD><BR>
<FORM METHOD="POST" ACTION="/postXXXX">
<INPUT TYPE="submit" VALUE="XX XXXX">
and C# code that looks like this:
WebRequest request = WebRequest.Create("http://192.168.254.12/postXXXX");
byte[] buffer = System.Text.Encoding.GetEncoding(1252).GetBytes("VALUE=XX XXXX");
request.Method = WebRequestMethods.Http.Post;
request.ContentType = "application/x-www-form-urlencoded";
Stream requestStream = request.GetRequestStream();
requestStream.Write(buffer, 0, buffer.Length);
requestStream.Close();
Console.Write(buffer.Length);
Console.Write(request.ContentLength);
WebResponse myResponse = request.GetResponse();
string result = "";
StreamReader reader = new StreamReader(myResponse.GetResponseStream());
result = reader.ReadToEnd();
Console.WriteLine(result);
I am getting 404 errors - I assume this is due to my POST text but nothing I have tried or any combination of the info in the HTML has worked...
The exception: `unhandled exception of type 'System.Net.WebException' occurred in System.dll
Additional information: The remote server returned an error: (404) Not Found.`
I have figured out my issue.
The Post request did not need any "content" to submit, so writing info to the buffer was not working correctly. I do not know the preferred method of sending the POST request in this case, but for me... I fixed the issue by changing
byte[] buffer = System.Text.Encoding.GetEncoding(1252).GetBytes("whatever");
to
byte[] buffer = System.Text.Encoding.GetEncoding(1252).GetBytes("");
Also, really most of the code I had was not needed. Final code:
WebRequest request = WebRequest.Create("http://192.168.254.12/postInstrumentStatus");
request.Method = WebRequestMethods.Http.Post;
request.ContentType = "application/x-www-form-urlencoded";
WebResponse myResponse = request.GetResponse();
string result = "";
StreamReader reader = new StreamReader(myResponse.GetResponseStream());
result = reader.ReadToEnd();
Console.WriteLine(result);
If any of this is wrong, please correct me. I am sure I have some misconceptions.