欢迎莅临 SUN WU GANG 的园子!!!

世上无难事,只畏有心人。有心之人,即立志之坚午也,志坚则不畏事之不成。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  470 随笔 :: 0 文章 :: 22 评论 :: 30万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

第一步,创建Winfrom窗体界面

第二步,Nuget安装CefSharp--67版本为例

第三步,实现,如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?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);//把收到的字符串序列化
        }
    }
} 

 实现效果:

 

posted on   sunwugang  阅读(278)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
点击右上角即可分享
微信分享提示