首页 > 解决方案 > c#在没有WebBrowser控制的情况下使用用户名和密码打开到网站的HTTP会话

问题描述

我需要HTTP使用用户名和密码创建与网站的会话。该网站是用PHP开发的,登录是form带有提交的简单HTML。知道怎么做吗?

标签: c#httpsessionloginforms

解决方案


这真的取决于网站,下面的代码应该是一个合理的起点。使用 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"

推荐阅读