【发布时间】:2011-08-04 12:04:49
【问题描述】:
所以我一直在尝试获取对字符串使用身份验证的网页并将其保存到文件中。它应该是非常基本的,所以我希望有人能看到我的错误。我对 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
【问题讨论】:
-
网页是做讨厌的对话框弹出登录,还是基于网页上的表单登录?
-
没有弹出窗口,但它有一个输入用户并通过的表单,其中一个标准框。
-
那么几乎可以肯定它没有使用内置的 Web 身份验证。这会阻止这项工作 - 您可以证明通过使用本地 IIS,您可以通过 vis studio 获得,并拥有一个 Web 身份验证页面。
-
我不太明白你在说什么。请详细说明。