如何在 C# 中登录网页并检索其内容?

发布于 2024-07-14 08:10:42 字数 26 浏览 3 评论 0原文

如何在 C# 中登录网页并检索其内容?

How do you login to a webpage and retrieve its content in C#?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

我要还你自由 2024-07-21 08:10:42

这取决于登录所需的内容。您可以使用 Web 客户端将登录凭据发送到服务器的登录页面(通过所需的任何方法,GET 或 POST),但这不会保留 cookie。 有一种方法让网络客户端处理cookie,所以您可以将登录信息发布到服务器,然后使用同一网络客户端请求您想要的页面,然后对该页面执行您想要的任何操作。

That depends on what's required to log in. You could use a webclient to send the login credentials to the server's login page (via whatever method is required, GET or POST), but that wouldn't persist a cookie. There is a way to get a webclient to handle cookies, so you could just POST the login info to the server, then request the page you want with the same webclient, then do whatever you want with the page.

踏月而来 2024-07-21 08:10:42

查看System.Net.WebClient,或者了解更高级的要求System.Net.HttpWebRequest/System.Net.HttpWebResponse

至于实际应用这些:您必须研究要抓取的每个页面的 html 源代码,以便准确了解它所期望的 Http 请求。

Look at System.Net.WebClient, or for more advanced requirements System.Net.HttpWebRequest/System.Net.HttpWebResponse.

As for actually applying these: you'll have to study the html source of each page you want to scrape in order to learn exactly what Http requests it's expecting.

太傻旳人生 2024-07-21 08:10:42

“登录”是什么意思?

如果子文件夹在操作系统级别受到保护,并且当您访问该子文件夹时浏览器会弹出登录对话框,则您将需要在 HttpWebRequest 上设置 Credentials 属性。

如果网站有自己的基于 cookie 的会员/登录系统,您将必须使用 HttpWebRequest 首先响应登录表单。

How do you mean "login"?

If the subfolder is protected on the OS level, and the browser pops of a login dialog when you go there, you will need to set the Credentials property on the HttpWebRequest.

If the website has it's own cookie-based membership/login system, you will have to use HttpWebRequest to first response to the login form.

定格我的天空 2024-07-21 08:10:42
string postData = "userid=ducon";
            postData += "&username=camarche" ;
            byte[] data = Encoding.ASCII.GetBytes(postData);
            WebRequest req = WebRequest.Create(
                URL);
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            req.ContentLength = data.Length;
            Stream newStream = req.GetRequestStream();
            newStream.Write(data, 0, data.Length);
            newStream.Close();
            StreamReader reader = new StreamReader(req.GetResponse().GetResponseStream(), System.Text.Encoding.GetEncoding("iso-8859-1"));
            string coco = reader.ReadToEnd();
string postData = "userid=ducon";
            postData += "&username=camarche" ;
            byte[] data = Encoding.ASCII.GetBytes(postData);
            WebRequest req = WebRequest.Create(
                URL);
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            req.ContentLength = data.Length;
            Stream newStream = req.GetRequestStream();
            newStream.Write(data, 0, data.Length);
            newStream.Close();
            StreamReader reader = new StreamReader(req.GetResponse().GetResponseStream(), System.Text.Encoding.GetEncoding("iso-8859-1"));
            string coco = reader.ReadToEnd();
嘿看小鸭子会跑 2024-07-21 08:10:42

使用 WebClient 类。

Dim Html As String

Using Client As New System.Net.WebClient()
    Html = Client.DownloadString("http://www.google.com")
End Using

Use the WebClient class.

Dim Html As String

Using Client As New System.Net.WebClient()
    Html = Client.DownloadString("http://www.google.com")
End Using
要走就滚别墨迹 2024-07-21 08:10:42

您可以使用 WebClient 对象中的构建,而不是自己创建请求。

WebClient wc = new WebClient();
wc.Credentials = new NetworkCredential("username", "password");
string url = "http://foo.com";          
try
{
    using (Stream stream = wc.OpenRead(new Uri(url)))
    {
        using (StreamReader reader = new StreamReader(stream))
        {
            return reader.ReadToEnd();
             }
    }
}
catch (WebException e)
{
    //Error handeling
}

You can use the build in WebClient Object instead of crating the request yourself.

WebClient wc = new WebClient();
wc.Credentials = new NetworkCredential("username", "password");
string url = "http://foo.com";          
try
{
    using (Stream stream = wc.OpenRead(new Uri(url)))
    {
        using (StreamReader reader = new StreamReader(stream))
        {
            return reader.ReadToEnd();
             }
    }
}
catch (WebException e)
{
    //Error handeling
}
笑饮青盏花 2024-07-21 08:10:42

尝试这个:

public string GetContent(string url)  
{ 
  using (System.Net.WebClient client =new System.Net.WebClient()) 
  { 
  return client.DownloadString(url); 
  } 
} 

Try this:

public string GetContent(string url)  
{ 
  using (System.Net.WebClient client =new System.Net.WebClient()) 
  { 
  return client.DownloadString(url); 
  } 
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文