c# - c#在没有WebBrowser控制的情况下使用用户名和密码打开到网站的HTTP会话
问题描述
我需要HTTP
使用用户名和密码创建与网站的会话。该网站是用PHP开发的,登录是form
带有提交的简单HTML。知道怎么做吗?
解决方案
这真的取决于网站,下面的代码应该是一个合理的起点。使用 HttpWebRequest 以预期的编码发送登录表单数据,通过使用 cookie 容器,应保存登录会话以供后续请求使用。
CookieContainer cookieContainer = new CookieContainer();
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(<login url>);
request.CookieContainer = cookieContainer;
request.Method = WebRequestMethods.Http.Post;
request.ContentType = "application/x-www-form-urlencoded"
推荐阅读
- google-apps-script - 如何通过 Google Apps 脚本在 MS Graph Api 中创建订阅
- jquery - FullCalendar:填充事件并单击按钮时,它会返回到当前月份 jquery
- rest - GraphQl 片段到 Pojo
- python - 如何从日期时间获取日期?
- firebase - Firebase admin sdk 监听实时数据库
- stripe-payments - 我可以更改付款意向金额吗?
- database - SQLite 更新一列中的多行,除了一行之外只有一个值(类似于反转选择):颤动
- c++ - Ipopt 使用 msys64 构建 - 下一步是什么?
- flutter - Flutter/Dart - 如何从不同文件中的另一个 StatefulWidget 更改 StatefulWidget 的状态?
- r - 如何使用带有函数和输出的嵌套 for 循环