I've been looking/asking around and can't seem to figure this one out. I have a C# application and need to be able to gather some data in the app, pop open a web browser and POST some data to it.
I can POST to the site from within the app fine and I can obviously pop open IE to a certain link but I can't do both. I can't POST to that link directly. Any ideas on how to accomplish this开发者_StackOverflow社区?
private void btnSubmit_Click(object sender, EventArgs e)
{
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "Fullname=Test";
byte[] data = encoding.GetBytes(postData);
// Prepare web request...
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://www.url.com/Default.aspx");
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream = myRequest.GetRequestStream();
// Send the data.
newStream.Write(data, 0, data.Length);
System.Diagnostics.Process.Start(myRequest.Address.ToString()); //open browser
newStream.Close();
}
Any insight would be greatly appreciated.
Thanks
If this is a WinForms application you could use the WebBrowser control to host an instance of Internet Explorer inside your application instead of spawning a new process. The advantage of this is that you have full control over it and among other you could POST to a given url:
private void btnSubmit_Click(object sender, EventArgs e)
{
var postData = Encoding.Default.GetBytes("Fullname=Test");
webBrowser1.Navigate(
"http://www.url.com/Default.aspx",
null,
postData,
"Content-Type: application/x-www-form-urlencoded" + Environment.NewLine
);
}
You can set the PostBackUrl property in a Button (or LinkButton or ImageButton)
However, that will do the standard ASP.NET post (with ViewState, etc.)
Depending on what you want to do, you can also create a separate that does not have runat="server" and then you can set the form's action property to your page.
Edit: Never mind, if you have a Windows application (not a web application) this won't work. At least, not directly.
精彩评论