启动 IE 执行 POST 数据

1. WPF 下使用 WebBrowser 控件 (System.Windows.Controls)

 

private static void Login(string initUrl, string postData) { using (WebBrowser wb = new WebBrowser()) { Uri url = new Uri(initUrl); ASCIIEncoding encoding = new ASCIIEncoding(); Byte[] b = encoding.GetBytes(postData); wb.Navigate(url, "_blank", b, null); } }

 

2.  使用Microsoft Internet 控件 (Shdocvw.dll)

 

private static void Login(string initUrl, string postData) { SHDocVw.InternetExplorer ie; object objPost, objHeaders, objFlags, objTargetFrame, objUrl; objFlags = null; objTargetFrame = null; objUrl = initUrl; objHeaders = "Content-Type: application/x-www-form-urlencoded" + Convert.ToChar(10) + Convert.ToChar(13); //Convert the string to post to an array of bytes. objPost = ASCIIEncoding.ASCII.GetBytes(postData); //Create an instance of Internet Explorer and make it visible. ie = new SHDocVw.InternetExplorer(); ie.Visible = true; ie.Navigate2(ref objUrl, ref objFlags, ref objTargetFrame, ref objPost, ref objHeaders); ShowWindow(ie.HWND, SW_MAXIMIZE); }

 

 

private const int SW_HIDE = 0; private const int SW_NORMAL = 1; private const int SW_MAXIMIZE = 3; private const int SW_SHOWNOACTIVATE = 4; private const int SW_SHOW = 5; private const int SW_MINIMIZE = 6; private const int SW_RESTORE = 9; private const int SW_SHOWDEFAULT = 10; [DllImport("user32.dll"), CharSet = CharSet.Auto)] private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow); [DllImport("USER32.dll", CharSet = CharSet.Auto)] private static extern int ShowWindow(System.IntPtr hWnd, int nCmdShow); [DllImport("USER32.dll", CharSet = CharSet.Auto)] private static extern bool SetForegroundWindow(System.IntPtr hWnd);

 

如果只是提交表单,还可以使用WebClient,另外上传文件时也经常会用 WebClient 。

 

private static void Login(string initUrl, string postData) { using (WebClient myWebClient = new WebClient()) { ASCIIEncoding encoding = new ASCIIEncoding(); Byte[] byteArray = encoding.GetBytes(postData); byte[] responseArray = myWebClient.UploadData(initUrl, "POST", byteArray); } }

你可能感兴趣的:(程序设计)