Winfrom 界面嵌入网页解决方案
作者:互联网
第一步,创建Winfrom窗体界面
第二步,Nuget安装CefSharp--67版本为例
第三步,实现,如下所示:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" /> </startup> <appSettings> <add key="Url" value="https://www.baidu.com/"/> <add key="screenNum" value="2"/> </appSettings> </configuration> using CefSharp; using CefSharp.WinForms; using SignIn.Helper; using System; using System.Configuration; using System.Linq; using System.Windows.Forms; namespace SignIn { public partial class SignInForm : Form { public ChromiumWebBrowser CurrentBrowser; public SignInForm() { InitializeComponent(); } private void SignInForm_Load(object sender, EventArgs e) { InitBrowser(); InitChrome(); } public void InitBrowser() { string url = ConfigurationManager.AppSettings["Url"]; Cef.Initialize(new CefSettings()); CurrentBrowser = new ChromiumWebBrowser(url); this.Controls.Add(CurrentBrowser); CurrentBrowser.Dock = DockStyle.Fill; } public void InitChrome() { try { //string str = DateTime.Now.ToString("yyyyMMdd"); int s = Screen.AllScreens.Count(); int screenNum = int.Parse(ConfigurationManager.AppSettings["screenNum"]); if (s > 1 && screenNum > 1) { //this.FormBorderStyle = FormBorderStyle.None; // this.DesktopBounds = Screen.AllScreens[screenNum - 1].Bounds; //this.Top = ((Screen.AllScreens[screenNum-1].Bounds.Height - this.Height) / 2); this.Left = (this.Width); this.WindowState = FormWindowState.Maximized; } else { //this.FormBorderStyle = FormBorderStyle.None; this.Left = 0; this.WindowState = FormWindowState.Maximized; //this.DesktopBounds = Screen.AllScreens[0].Bounds; } } catch (Exception ex) { LogHelper.Instance.SaveText("InitChrome ERROR" + ex.Message + ex.StackTrace); } } } } using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Net.Http; using System.Threading.Tasks; namespace SignIn.Helper { public class HttpRequest { /// <summary> /// 使用post方法异步请求 /// </summary> /// <param name="url">目标链接</param> /// <param name="json">发送的参数字符串,只能用json</param> /// <returns>返回的字符串</returns> public static async Task<string> PostAsyncJson(string url, string json) { HttpClient client = new HttpClient(); HttpContent content = new StringContent(json); content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); HttpResponseMessage response = await client.PostAsync(url, content); response.EnsureSuccessStatusCode(); string responseBody = await response.Content.ReadAsStringAsync(); return responseBody; } public static HttpResponseMessage PostJson(string url, string json) { HttpClient client = new HttpClient(); HttpContent content = new StringContent(json); content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); HttpResponseMessage response = client.PostAsync(url, content).Result; response.EnsureSuccessStatusCode(); //string responseBody = response.Content.ReadAsStringAsync().Result; return response; } public static HttpResponseMessage Get(string url) { HttpClient client = new HttpClient(); HttpResponseMessage response = client.GetAsync(url).Result; response.EnsureSuccessStatusCode(); return response; } /// <summary> /// 使用post方法异步请求 /// </summary> /// <param name="url">目标链接</param> /// <param name="data">发送的参数字符串</param> /// <returns>返回的字符串</returns> public static async Task<string> PostAsync(string url, string data, Dictionary<string, string> header = null) { HttpClient client = new HttpClient(new HttpClientHandler() { UseCookies = false }); HttpContent content = new StringContent(data); if (header != null) { client.DefaultRequestHeaders.Clear(); foreach (var item in header) { client.DefaultRequestHeaders.Add(item.Key, item.Value); } } HttpResponseMessage response = await client.PostAsync(url, content); response.EnsureSuccessStatusCode(); string responseBody = ""; responseBody = await response.Content.ReadAsStringAsync(); return responseBody; } /// <summary> /// 使用get方法异步请求 /// </summary> /// <param name="url">目标链接</param> /// <returns>返回的字符串</returns> public static async Task<string> GetAsync(string url, Dictionary<string, string> header = null) { HttpClient client = new HttpClient(new HttpClientHandler() { UseCookies = false }); if (header != null) { client.DefaultRequestHeaders.Clear(); foreach (var item in header) { client.DefaultRequestHeaders.Add(item.Key, item.Value); } } HttpResponseMessage response = await client.GetAsync(url); response.EnsureSuccessStatusCode();//用来抛异常的 string responseBody = ""; //if (Gzip) //{ // GZipInputStream inputStream = new GZipInputStream(await response.Content.ReadAsStreamAsync()); // responseBody = new StreamReader(inputStream).ReadToEnd(); //} //else //{ responseBody = await response.Content.ReadAsStringAsync(); //} return responseBody; } /// <summary> /// 使用post返回异步请求直接返回对象 /// </summary> /// <typeparam name="T">返回对象类型</typeparam> /// <typeparam name="T2">请求对象类型</typeparam> /// <param name="url">请求链接</param> /// <param name="obj">请求对象数据</param> /// <returns>请求返回的目标对象</returns> public static async Task<T> PostObjectAsync<T, T2>(string url, T2 obj) { String json = JsonConvert.SerializeObject(obj); string responseBody = await PostAsyncJson(url, json); //请求当前账户的信息 return JsonConvert.DeserializeObject<T>(responseBody);//把收到的字符串序列化 //return responseBody; } /// <summary> /// 使用Get返回异步请求直接返回对象 /// </summary> /// <typeparam name="T">请求对象类型</typeparam> /// <param name="url">请求链接</param> /// <returns>返回请求的对象</returns> public static async Task<T> GetObjectAsync<T>(string url) { string responseBody = await GetAsync(url); //请求当前账户的信息 return JsonConvert.DeserializeObject<T>(responseBody);//把收到的字符串序列化 } } }
实现效果:
标签:嵌入,网页,string,url,Winfrom,responseBody,client,new,response 来源: https://www.cnblogs.com/YYkun/p/15271642.html