Click here to Skip to main content
14,875,778 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi everyone!

I need to open system default browser and send to some custom URI POST data. So I have two part of code: first - opens def browser and another must send POST data to it, but does not do it.

What can you say about it?
C#
private void button1_Click(object sender, EventArgs e)
        {
            
            string browser = string.Empty;
            RegistryKey key = null;
            try
            {
                key = Registry.ClassesRoot.OpenSubKey(@"HTTP\shell\open\command", false);

                //trim off quotes
                browser = key.GetValue(null).ToString().ToLower().Replace("\", "");
                if (!browser.EndsWith("exe"))
                {
                    //get rid of everything after the ".exe"
                    browser = browser.Substring(0, browser.LastIndexOf(".exe")+4);
                }
            }
            finally
            {
                if (key != null) key.Close();
            }
            //open default system browser
            System.Diagnostics.Process.Start(browser, strURL.Text);


//***************************************************

            // Convert string data into byte array 
            string strData = "Name=Sergiy&Age=21";
            byte[] dataByte = Encoding.UTF8.GetBytes(strData);

            HttpWebRequest POSTRequest = (HttpWebRequest)WebRequest.Create(strURL.Text);
            POSTRequest.Method = "POST";
            // Set the content type - Mine was xml.
            POSTRequest.ContentType = "application/x-www-form-urlencoded";
            POSTRequest.KeepAlive = false;
            POSTRequest.Timeout = 5000;
            POSTRequest.ContentLength = dataByte.Length;
            // Get the request stream
            Stream POSTstream = POSTRequest.GetRequestStream();
            // Write the data bytes in the request stream
            POSTstream.Write(dataByte, 0, dataByte.Length);

            //Get response from server
            HttpWebResponse POSTResponse = (HttpWebResponse)POSTRequest.GetResponse();

        }

Thanks!
Posted
Updated 15-Sep-10 9:12am
v2
Comments
Sandeep Mewara 15-Sep-10 15:13pm
   
Use PRE tags to format your code part. It makes the question readable.

Please tell me where you would like to execute this form?
If you are willing this from the client side, you need to create a form using Javascript and post it.

Otherwise, if you want it in your server, Just open the browser and Navigate to the location with post data. If you need this, you can call only the url using Process.Start, and it will automatically navigate itself to a specified location.


On the other hand if you want it programmitcally you can use WebRequest.Create

http://msdn.microsoft.com/en-us/library/debx8sh9.aspx[^]
Just use WebBrowser to show the response only. Dont use Process.Start in this case.
   
Comments
yozhik89 15-Sep-10 15:55pm
   
I need on any computer which have .NET run my application. It takes two params: page address and POST data. Then I press button 'Open'. And it must open DEFAULT browser with URL and pass there POST parameter.


I find sourse, but it doesnt work( it compiles but no reaction:

// Create a request using a URL that can receive a post.
WebRequest request = WebRequest.Create(strURL.Text);
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
string postData = "Name=Sergiy&Age=14";
byte[] byteArray = Encoding.UTF8.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();
I need on any computer which have .NET run my application. It takes two params: page address and POST data. Then I press button 'Open'. And it must open DEFAULT browser with URL and pass there POST parameter.


I find sourse, but it doesnt work( it compiles but no reaction:

// Create a request using a URL that can receive a post.
WebRequest request = WebRequest.Create(strURL.Text);
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
string postData = "Name=Sergiy&Age=14";
byte[] byteArray = Encoding.UTF8.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();
   
For our WebBrowser automatic guidance, or even to submit features, is not difficult.
Suppose there is a simple login page, enter the user name password, click "Login" button to login. Known user name input box id (or Name, the same below) is the username, the password input box id is the password, "login" button id is submitbutton, then we only need webBrowser the DocumentCompleted event use the following code that is may:
HtmlElement btnSubmit = webBrowser.Document.All["submitbutton"];
HtmlElement tbUserid = webBrowser.Document.All["username"];
HtmlElement tbPasswd = webBrowser.Document.All["password"];

if (tbUserid == null || tbPasswd == null || btnSubmit == null)
    return;

tbUserid.SetAttribute("value", "smalldust");
tbPasswd.SetAttribute("value", "12345678");

btnSubmit.InvokeMember("click"); 

Here we use the SetAttribute to set the text box "value" attribute, with InvokeMember to call the button "click" method. Html for the different elements of their own properties and methods are also different, so. Net 2.0 provides a unified HtmlElement to summarize the various elements of the same Html provides two ways to call the element-specific functionality. Html elements on the various properties and methods list, you can access MSDN's DHTML Reference.
※ submitted on the form, indeed there is another way to access form elements instead of button, and submit with the form element methods:
HtmlElement formLogin = webBrowser.Document.Forms["loginForm"];  
//……
formLogin.InvokeMember("submit"); 

breast[^]
   
v2

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