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();
 
I installed .net 4.8 framework but it didn't integrated automatically with my visual studio. In otherwords, it is not included in the drop-down list like yours
visual_studio_screenshot.png
 
You need to install .NET Framework SDK, not the .NET Framework runtime, for it to show up in the VS list.

Also that light gray gradient on the tabs suggests that you may have an older version of Visual Studio. I don't think I've seen that in quite a while since the newer Visual Studio uses flat colouring. I recommend upgrading to the latest version.
 
Skydiver and Sheepings, he's using VS 2008 which can only target .Net 2.0, 3.0, and 3.5
What he needs to do first is download the VS2019 Community Edition (it's free) then he can open his project in it and change it to target .Net 4.8
 
Using VS2019 by itself won't fix things. You still need to build your code targeting .NET Framework 4.8. What VS2019 does is allow you to pick that target.

As an aside, you could also build your code with just the .NET Framework 4.8 SDK on the command line, but that will require you to use VS Code, or some other text editor. Since you are still on the learning curve, keep things simple and just install VS2019. You learn the language first, then later learn about how things are actually done by the IDE for you.

(Yes, we C# folks are less strict than the C and C++ folks that insist that you first learn how the compiler and linker works before for learn how to write code.)
 
Using VS2019 by itself won't fix things. You still need to build your code targeting .NET Framework 4.8. What VS2019 does is allow you to pick that target.

As an aside, you could also build your code with just the .NET Framework 4.8 SDK on the command line, but that will require you to use VS Code, or some other text editor. Since you are still on the learning curve, keep things simple and just install VS2019. You learn the language first, then later learn about how things are actually done by the IDE for you.

(Yes, we C# folks are less strict than the C and C++ folks that insist that you first learn how the compiler and linker works before for learn how to write code.)

thanks for the clarification. The issue I was referring to though was in regards to the 401 authorization error I was getting in relation to my console application
 
The TLS fix allows you to talk to the website. Now that you can talk to it, you need to pass in credentials that is authorized to use the web service you are trying to talk to. That is not a technology problem, but rather as social/security problem. Have you talked to the website owners to find out what credential you need to pass in?
 
The TLS fix allows you to talk to the website. Now that you can talk to it, you need to pass in credentials that is authorized to use the web service you are trying to talk to. That is not a technology problem, but rather as social/security problem. Have you talked to the website owners to find out what credential you need to pass in?
I know the username and password of the website and the associated url information, should I be asking for other stuff as well
 
So where are you passing in the credentials in your request?
 
So where are you passing in the credentials in your request?


In the following section of the code below

the networkcredential takes the username and password
// credentialCache.Add(new System.Uri(url), "Basic", new NetworkCredential(~~~~, ~~~~~~~~~~~~));

C#:
private static CredentialCache GetCredential()
{
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | (SecurityProtocolType)3072;
    string url = @"https://~~~~~~~~~~~~~~~~~~~~~~.com/~~~~~";
    CredentialCache credentialCache = new CredentialCache();
    credentialCache.Add(new System.Uri(url), "Basic", new NetworkCredential(~~~~, ~~~~~~~~~~~~));
    return credentialCache;
}

public static void testcall (string url)
{

    WebRequest request = WebRequest.Create(url);
    request.PreAuthenticate = true;
    request.Credentials = GetCredential();

    ((System.Net.HttpWebRequest)request).Referer = url;
    ((System.Net.HttpWebRequest)request).Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
    ((System.Net.HttpWebRequest)request).UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:83.0) Gecko/20100101 Firefox/83.0";

    // Set the Method property of the request to POST.
    request.Method = "POST";
    // Create POST data and convert it to a byte array.
    string postData = "param1=XXXXXX";
    ASCIIEncoding encoding = new ASCIIEncoding();

    byte[] byteArray = encoding.GetBytes(postData);
    // Set the ContentType property of the WebRequest.
    request.ContentType = "application/x-www-form-urlencoded";
    // Set the ContentLength property of the WebRequest.
    request.ContentLength = byteArray.Length;
    // Get the request stream.
    Stream dataStream = request.GetRequestStream();
    // Write the data to the request stream.
    dataStream.Write(byteArray, 0, byteArray.Length);
    // Close the Stream object.
    dataStream.Close();



    // Get the response.
    WebResponse response = request.GetResponse();
    // Display the status.
    Console.WriteLine(((HttpWebResponse)response).StatusDescription);
    // Get the stream containing content returned by the server.
    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);
    // Clean up the streams.
    reader.Close();
    dataStream.Close();
    response.Close();
}
 
It looks like the website needs more than just the user name and password. Does it also need headers or cookies that contains some other secrets? What did the website owner tell you about how to authenticate against their website using a direct web request instead of going through the GUI?
 
It looks like the website needs more than just the user name and password. Does it also need headers or cookies that contains some other secrets? What did the website owner tell you about how to authenticate against their website using a direct web request instead of going through the GUI?

it is just username and password or is there anyway to verify this by studying the site.
 
The TLS fix allows you to talk to the website.
Skydiver, it's important to note that the below is not required to sent in your requests if you are using the latest .net versions.
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | (SecurityProtocolType)3072; is not required in 4.8.
Since the error the OP is getting is of that of authentication of credentials, may also be linked to a TLS issue. Irradiating irrelevant code is a start, and running the latest version of .net framework is advised above :
What he needs to do first is download the VS2019 Community Edition (it's free) then he can open his project in it and change it to target .Net 4.8
If the OP doesn't try, they will never know if the line of code I quoted is the issue or not. But as I said, Microsoft took that out in v4.6 I think it was, and it is no longer needed as later versions of .net determine the protocol for you.
 
Skydiver, it's important to note that the below is not required to sent in your requests if you are using the latest .net versions.

Since the error the OP is getting is of that of authentication of credentials, may also be linked to a TLS issue. Irradiating irrelevant code is a start, and running the latest version of .net framework is advised above :

If the OP doesn't try, they will never know if the line of code I quoted is the issue or not. But as I said, Microsoft took that out in v4.6 I think it was, and it is no longer needed as later versions of .net determine the protocol for you.
Hi I just installed visual studio 2019 and likewise the framework 4.5 wasn't able to integrate with visual studio 2019 automatically after several attempts. But most importantly, I am still getting the remote server unauthorized 401 error in visual studio 2019 as well
 
You need a recap on what I told you.
I have a console app and I can use 4.8.

There is an option to install other versions.

Move up to 4.8, and remove the line I told you to remove. Then tell us if you get a new error or not?
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | (SecurityProtocolType)3072; is not required in 4.8.
And your problem is indicative of a problem with either TLS or your credentials.

You will need someone else to help from here. I have server deployments to attend to.

Look at the screenshot, install if missing any version
 
Back
Top Bottom