Question sending an HTTP request/POST to a website

nofu1

Active member
Joined
Oct 30, 2020
Messages
37
Programming Experience
Beginner
I am still a beginner in c#. I have a console application. In javascript, we can do the following document.getElementById("myText").value = "Mary; which is used to set an HTML element. Is this possible in a console application that sending an HTTP requests to a website or should the application be using a post method. Please see code so far below. There is a textbox with the tag name testmedownelement at the website(www.~~~~~~~~~~~~~~~~~~~~~~.com).

This is the javascript code: document.getElementsByName('testmedownelement').value = "XXXXXX", I am looking for a C# equivalent since I can't use java script in my console application

C#:
            WebRequest request = WebRequest.Create("www.~~~~~~~~~~~~~~~~~~~~~~.com");
        

           request.Method = "GET";
            request.ContentType = "application/html";
            
          

            // Get the response.

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            //Display Status

            Console.WriteLine(response.StatusDescription);

            // Get the stream containing content returned by the server.

            Stream dataStream = response.GetResponseStream();

            // Open the stream using a StreamReader for easy access.

            StreamReader reader = new StreamReader(dataStream);

            // Read the content.

            string responseFromServer = reader.ReadToEnd();

            // Display the content.

            Console.WriteLine(responseFromServer);

            // Cleanup the streams and the response.

            reader.Close();

            dataStream.Close();

            response.Close();

            Console.ReadKey();
 
As @Sheepings said above, TLS 1.2 is enabled by default on 4.7 and up. You are targeting 4.5. As I said above, TLS 1.2 still won't fix your 401 error because that is a different problem. What is wrong with talking to the website owner and finding out what you need to do to authenticate? Or are you illegally page scraping their site.
 
As @Sheepings said above, TLS 1.2 is enabled by default on 4.7 and up. You are targeting 4.5. As I said above, TLS 1.2 still won't fix your 401 error because that is a different problem. What is wrong with talking to the website owner and finding out what you need to do to authenticate? Or are you illegally page scraping their site.

They said the username and password were the only credentials needed. The username and password were tested manual and worked fine. Please let not make any false assumptions. I only mentioned the framework as clarity after installation.
 
Last edited:
In the original post, you mentioned that you had this working in JavaScript, but the only code that you showed was you setting value on the DOM. You never actually showed how you post the form back to the server so that the server gets the value that you set. Did you just do a plain old HTTP POST, or did some headers or cookies need to be set?

So the question is: at the point when you are changing the DOM, did the user have to manually log on to the web site? And if so, did the server send back some kind of session cookie or token to represent that session? And if so, how our you simulating that same kind of logon and getting back a cookie using your HTTP web request code?

Use Fiddler (or whatever web proxy tool you prefer -- maybe the F12 developer tools will be enough). Watch the traffic that is sent when you are using your browser. Compare it to the traffic that is sent when you are using your console application.
 
As I said above, TLS 1.2 still won't fix your 401 error because that is a different problem.
That's not necessarily correct. It is possible to get a 401 error if the security of the request is not correct. On DIC I answered a question not so long ago regarding TLS, and after the OP of that thread removed line for TLS/updated .Net, their issue was relinquished, and hence; why I suggested moving to 4.8 .Net. There are a range of issues that can cause a 401 when using TLS : tls 401 error - Google Search

Since user is using TLS, what does the server use? Server side cert or client side cert? If this is a two-way TLS, you have a lot bigger problems. I also don't like the direction this topic is going. If there was a legitimate API to connect to, there would have been no reason to do anything with the DOM using JS. Which means, that this website or server you are trying to connect to is not meant to be connected to in the manner you are attempting.
 
That's not necessarily correct. It is possible to get a 401 error if the security of the request is not correct. On DIC I answered a question not so long ago regarding TLS, and after the OP of that thread removed line for TLS/updated .Net, their issue was relinquished, and hence; why I suggested moving to 4.8 .Net. There are a range of issues that can cause a 401 when using TLS : tls 401 error - Google Search

Since user is using TLS, what does the server use? Server side cert or client side cert? If this is a two-way TLS, you have a lot bigger problems. I also don't like the direction this topic is going. If there was a legitimate API to connect to, there would have been no reason to do anything with the DOM using JS. Which means, that this website or server you are trying to connect to is not meant to be connected to in the manner you are attempting.
I hope you know there are mapping websites that allows you to determine the associated lat/long based on an input such as an address (DOM JS), furthermore these websites also gives you the option to make API calls as well utilizing their configuration to determine the associated lat/long. Also, sometimes people tend to mistakenly look in the wrong direction required to solve an API problem. and there are a range of issues that can cause 401 as well.
 
Sorry you lost me when you started talking about latitude etc... What does any of that have to do with what I said?
Also, sometimes people tend to mistakenly look in the wrong direction required to solve an API problem.
Really? Like what?

If I want to get data from a server that doesn't accept data requests, I make an API to deal with that problem. Anyway, I don't see the relevance of what point you are trying to establish here?
and there are a range of issues that can cause 401 as well.
Well yea, I kinda pointed that out in the link I posted... anyway, nothing in that reply brings you any closer to resolving your problem. Maybe if you gave us a link to what you're trying to connect too, we might be able to come up with some other suggestions. Have you since installed the latest VS community edition along with Dotnet4.8 and removed that line I mentioned previously?
 
Your problem is, you are trying to connect to a server in a way that is likely not allowed by its owners. Otherwise, You'd be doing jack shit with the DOM and you would be looking to the owners API for answers.

Can you show me there API? Or their documentation for the API you are using?

If they have an API, why were you mauling the DOM?

And if the owner of the server wanted you to have access to their resource, they would provide you with credentials and an example to use.

You have not shown us this API, or provided any links to either of its documentation or a URI. This makes it a little harder for us to provide any quality assistance to you.
 
I have scrapped trying to create a console app, I rather have them just send me a .csv file with the data. but thanks everyone for the effort.
 
Why are you reluctant to share with us the information I requested?

You are limiting the amount of support we can give by retaining answers to questions we have asked you.
 
Back
Top Bottom