在默认浏览器中使用POST参数打开URL [英] open URL with POST params in default browser

查看:231
本文介绍了在默认浏览器中使用POST参数打开URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

我需要打开系统默认浏览器,并发送一些自定义URI POST数据.因此,我有两部分代码:第一部分-打开def浏览器,另一部分必须向其发送POST数据,但不这样做.

您能说些什么?

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?

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();

        }


推荐答案

请告诉我您想在哪里执行此表格?
如果您愿意从客户端进行此操作,则需要使用Javascript创建表单并将其发布.

否则,如果要在服务器中使用它,只需打开浏览器,然后导航到包含张贴数据的位置.如果需要,可以使用Process.Start仅调用url,它将自动将自身导航到指定位置.


另一方面,如果您希望以编程方式进行操作,则可以使用WebRequest.Create

http://msdn.microsoft.com/en-us/library/debx8sh9.aspx [ ^ ]
只需使用WebBrowser仅显示响应即可.在这种情况下,请不要使用Process.Start.
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.


我需要在装有.NET并运行我的应用程序的任何计算机上.它需要两个参数:页面地址和POST数据.然后我按下按钮打开".并且必须使用URL打开DEFAULT浏览器并在其中传递POST参数.


我发现很酸,但它不起作用(它编译但没有反应:

//使用可以接收帖子的URL创建请求.
WebRequest请求= WebRequest.Create(strURL.Text);
//将请求的Method属性设置为POST.
request.Method ="POST";
//创建POST数据并将其转换为字节数组.
字符串postData ="Name = Sergiy&Age = 14";
byte [] byteArray = Encoding.UTF8.GetBytes(postData);
//设置WebRequest的ContentType属性.
request.ContentType ="application/x-www-form-urlencoded";
//设置WebRequest的ContentLength属性.
request.ContentLength = byteArray.Length;
//获取请求流.
流dataStream = request.GetRequestStream();
//将数据写入请求流.
dataStream.Write(byteArray,0,byteArray.Length);
//关闭Stream对象.
dataStream.Close();
//获取响应.
Web响应响应= request.GetResponse();
//显示状态.
Console.WriteLine(((((HttpWebResponse)response).StatusDescription));
//获取包含服务器返回内容的流.
dataStream = response.GetResponseStream();
//使用StreamReader打开流以方便访问.
StreamReader reader =新的StreamReader(dataStream);
//阅读内容.
字符串responseFromServer = reader.ReadToEnd();
//显示内容.
Console.WriteLine(responseFromServer);
//清理流.
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();


对于我们的WebBrowser,自动指导甚至提交功能都不难.
假设有一个简单的登录页面,输入用户名密码,单击登录"按钮进行登录.已知的用户名输入框ID(或名称,下同)是用户名,密码输入框ID是密码,登录"按钮ID是SubmitButton,那么我们只需要webBrowser DocumentCompleted事件使用以下代码:
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"); 


在这里,我们使用SetAttribute设置文本框的"value"属性,并使用InvokeMember调用按钮的"click"方法. HTML对于不同元素自身的属性和方法也有所不同,因此. Net 2.0提供了一个统一的HtmlElement来汇总同一Html的各种元素,并提供了两种方法来调用特定于元素的功能.在HTML元素的各种属性和方法列表上,可以访问MSDN的DHTML参考.
※在表单上提交,确实有另一种方法可以访问表单元素而不是按钮,并使用表单元素提交方法:


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[^]


这篇关于在默认浏览器中使用POST参数打开URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆