你的位置:首页 > 操作系统

[操作系统]Winform用Post方式打开IE


1.主要实现Code

 1 void OpenNewIe(string url, string postData)///url是要post的网址,postData是要传入的参数 2     { 3       if (ie != null)///避免重复打开ie对象没有释放,抛出异常 4       { 5         try 6         { 7           ie.Quit(); 8         } 9         catch10         {11           ie = null;12         }13       }14       ie = new InternetExplorer();15       object vPost, vHeaders, vFlags, vTargetFrame;16       vPost = Encoding.GetEncoding("GB2312").GetBytes(postData);///注意编码方式是GB2312,还是ASCII17       vFlags = null;18       vTargetFrame = null;19       vHeaders = "Content-Type: application/x-www-form-urlencoded" + Convert.ToChar(10) + Convert.ToChar(13);20       ie.Visible = true;21       ie.Navigate(url, ref vFlags, ref vTargetFrame, ref vPost, ref vHeaders);23     }

2.参数传递

1 StringBuilder postData = new StringBuilder();2       postData.AppendFormat("uid={0}", HelpEncrypt.Encode(uid.ToString(), key));3       postData.AppendFormat("&serial_number={0}", HelpEncrypt.Encode(serial_number, key));

 3.调用方法

1 if (checkBoxNo.Checked)2       {3         OpenNewIe(Parameter.Payaddress, postData.ToString());        4       }

4.附加:用WebBrowser组件Post打开IE

1 private void TransferWebForm_Load(object sender, EventArgs e)2     {3       string vHeaders = "Content-Type: application/x-www-form-urlencoded" + Convert.ToChar(10) + Convert.ToChar(13);4       byte[] vPost = Encoding.GetEncoding("GB2312").GetBytes(_postData);5       this.webBrowser1.Navigate(navigateUrl, null, vPost, vHeaders);6     }