C# 连接网页并进行身份验证
所以我一直在尝试获取一个使用字符串身份验证的网页并将其保存到文件中。它应该是非常基本的,所以我希望有人能够看到我的错误。我对 C# 非常陌生,所以以后请对待我:)
这段代码在某种程度上起作用,但我得到的文件是登录屏幕的 html,而不是为登录用户显示的页面。我是什么做错了吗?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
namespace fetchingweb
{
class WebAutheticator
{
static void Main(string[] args)
{
string htmlHer = GetWeb();
StreamWriter file = new
StreamWriter("C:\\Users\\user\\Documents\\atextfile.txt");
file.Write(htmlHer);
file.Close();
} //main end
private static string GetWeb()
{
WebClient wc = new WebClient();
wc.Credentials = new NetworkCredential("user", "pass");
string url = "http://someurl.com/index.php";
try
{
using (Stream stream = wc.OpenRead(new Uri(url)))
{
using (StreamReader reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
}
catch (WebException e)
{
return "failure!";
}
} //getweb end
} //class end
} //namespace end
So I've been trying to fetch a web page that uses authentication to a string and save it to a file. It should be pretty basic so I hope someone can see my errors. I'm very new to C# so treat me thereafter :)
This code functions to some extend, but the file I get is the html for the login screen and not the page that its shown for users that is logged in. What is it I'm doing wrong?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
namespace fetchingweb
{
class WebAutheticator
{
static void Main(string[] args)
{
string htmlHer = GetWeb();
StreamWriter file = new
StreamWriter("C:\\Users\\user\\Documents\\atextfile.txt");
file.Write(htmlHer);
file.Close();
} //main end
private static string GetWeb()
{
WebClient wc = new WebClient();
wc.Credentials = new NetworkCredential("user", "pass");
string url = "http://someurl.com/index.php";
try
{
using (Stream stream = wc.OpenRead(new Uri(url)))
{
using (StreamReader reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
}
catch (WebException e)
{
return "failure!";
}
} //getweb end
} //class end
} //namespace end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在使用 NetworkCredential 登录 Web 应用程序,但 Web 应用程序正在使用某种表单身份验证。
只要 web 应用程序未配置为使用网络凭据,这将不起作用。
由于这是一个 php 应用程序,我猜它使用普通表单身份验证,并且您需要在继续之前将用户名/密码发布到登录页面。
You are using NetworkCredential to login at the webapp, but the webapp is using some kind of forms authentication.
As long as the webapp is not configured to use network credentials this will not work.
Since this is a php application I guess it uses plain forms auth and that you will need to post username/password to the login page before continuing.
查看您尝试登录并下载的网站的登录页面上的代码。
会有一个
部分,如果它是post你需要发布,如果它是get你使用get。它将发布到新页面,并可能使用重定向,或者甚至引用自身。它很可能会使用某种形式的会话 ID 或 cookie 来证明您已登录。理解这一点的最佳方法是编写一个代理,它接受您的请求,并显示您发送的内容和返回的内容。是了解您使用事物时会发生什么以及您的程序需要做什么来模仿它的最佳方式
Look at the code on the login page of the site you're trying to log into and download.
There will be a
<FORM..>
section, if its post you need to post, if its get you use get. It will either post to a new page, and possibly use redirection, or, even refer to itself. Chances are it will use either some form of session id, or cookies to prove you logged in.Best way to understand this is to write a proxy, which takes your requests, and shows what you send and what you get back.. As this is the best way to understand what happens when you use things and what your program would need to do to mimic it