Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello!

I need to call a site with a post request in the user's default browser.

What I have tried:

I was able to do this using the web browser component:



Dim postData As String = "par1=val1&par2=" & val2
        Dim Encoding As System.Text.Encoding = System.Text.Encoding.UTF8
        Dim bytes As Byte() = Encoding.GetBytes(postData)
        Dim url As String = "https://mysite/"
        webBrowser1.Navigate(url, String.Empty, bytes, "Content-Type: application/x-www-form-urlencoded")

But this does not suit me, because. for this I have to create my form with a web browser, but I need to do all this in the user's default browser.


Does anyone know how this can be done? 
Posted
Updated 7-Feb-22 0:35am

Option 1:
Write code to determine the user's default browser. Then write code to automate their default browser. This will need to be different for each browser, since there isn't a standard way to automate them. There may be some browsers which don't support this kind of automation. And you will likely still end up with users whose default browser is some obscure product you've never heard of, so your code won't work for them.

Option 2:
Create a page on your own website which accepts the value to post as a querystring parameter, and returns a pre-populated <form> with some Javascript to automatically submit the form when the page loads. Then construct the URL to your site, and use Process.Start to launch the user's default browser and navigate to that URL.
VB.NET
Process.Start("https://yoursite.local/redirect-to-target/?par1=val1&par2=" & val2)

Option 3:
Create a temporary HTML file on disk containing a pre-populated <form> and some Javascript to submit the form when the page loads. Use Process.Start to open that HTML file in the user's default browser (assuming they haven't configured HTML files to open in a different application or browser).
VB.NET
Dim html As String = $"
<form id=""f"" method=""POST"" action=""https://mysite/"">
    <input type=""hidden"" name=""par1"" value=""val1"" />
    <input type=""hidden"" name=""par2"" value=""{val2}"" />
</form>
<script>
document.getElementById('f').submit();
</script>
"

Dim fileName As String = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N") + ".html")
File.WriteAllText(fileName, html)

Process.Start(fileName)
You'll want to clean up these temporary HTML files once you've finished with them.
 
Share this answer
 
v3
Comments
Member 14609058 7-Feb-22 7:16am    
Hello!
Thank you for your answer!
Of the proposed options, option 3 suits me. But I tried to create the html file manually. And open it, as a result, a file with a path from a local folder was opened. In addition, the browser issued a warning asking if this file should be opened.
Richard Deeming 7-Feb-22 7:20am    
Sorry, the URL should be in the action attribute, not the target attribute. I've updated my answer.

Unfortunately, recent versions of Windows have an annoying habit of regularly asking you to re-confirm which application you want to use to open a file type you've already configured. I haven't found any way to disable that message yet. But once you've told it to continue using your default browser, it should remember that choice for a while.
Member 14609058 7-Feb-22 7:27am    
Thank you! All work!
Now, on that website there is a button that performs a POST request and updates the content of the site, without changing the URL. I would like to directly show that updated site, rather than having the user clicking around on the website manually.

Is there any way to do this programmatically in C#?

One idea is doing the POST in C# and somehow pass the response to the browser. I know how to do HTTP requests in C# and I know what parameters I need to send here, but I don't know of a way to make the browser display the response.

Any other ideas are welcome, too.
 
Share this answer
 
Comments
OriginalGriff 7-Feb-22 2:38am    
Please do not post things like this as a solution - it removes it from the "Unanswered" queue and that makes it less likely the OP will get an answer.
If you must post comments like this (and your other contribution) then use the "Have a question or comment" facility below the question instead.

Posting "non-answers" can be seen as "rep point farming" and that is considered a form of site abuse for which you can be kicked off the site permanently. I don't think that was the idea here, but the more trigger happy amongst us will assume it was if you continue.

Would you like me to delete this for you?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900