我搜索了各种论坛和链接,但还没有得到任何帮助。
实际上我正致力于使用C#在ASP.net中开发Web应用程序,该应用程序允许用户登录远程服务器,然后从该服务器下载文件。
我可以登录该服务器并查看受保护页面的内容。
但是在下载文件时,我收到了Unauthorize访问错误。
我可以从服务器下载图像,但这些图像不受保护。
我到目前为止开发的代码是
protected void Unnamed1_Click(object sender, EventArgs e)
{
try
{
string LOGIN_URL = "https://some.server/";
// first, request the login form to get the value
HttpWebRequest webRequest = WebRequest.Create(LOGIN_URL) as HttpWebRequest;
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.KeepAlive = true;
webRequest.UserAgent = userAgent;
String received = Encoding.ASCII.GetString(Request.BinaryRead(Request.ContentLength));
webRequest.ContentLength = received.Length;
webRequest.Proxy.Credentials = new NetworkCredential("usrname", "password", "domain");
StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream(), Encoding.ASCII);
string responseData = responseReader.ReadToEnd();
responseReader.Close();
string postData = "user=usr&password=pass&switch=Login";
Response.Write(webRequest.Address);
// have a cookie container ready to receive the forms auth cookie
CookieContainer cookies = new CookieContainer();
// now post to the login form
webRequest = WebRequest.Create(LOGIN_URL) as HttpWebRequest;
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.CookieContainer = cookies;
// write the form values into the request message
StreamWriter requestWriter = new StreamWriter(webRequest.GetRequestStream());
requestWriter.Write(postData);
requestWriter.Close();
// we don't need the contents of the response, just the cookie it issues
webRequest.GetResponse().Close();
// Download files
WebProxy myProxy = new WebProxy("server.https.com", 443);
//WebRequest request = WebRequest.Create("https://some.server/file.zip");
WebRequest request = WebRequest.Create("https://some.server/file.zip");
request.Proxy.Credentials = new NetworkCredential("usrname", "password", "domain");
string filename = "file.zip";
string filepath = "C:\\Documents and Settings\\user\\Desktop\\" + filename.ToString();
WebClient client = new WebClient();
client.Credentials = new NetworkCredential("usrname", "password", "domain");
client.DownloadFile("https://some.server/file.zip", filepath);
}
catch (Exception exp)
{
Response.Write(exp.ToString());
}
}
服务器错误。
远程服务器返回错误:(401)未经授权。 描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
异常详细信息:System.Net.WebException:远程服务器返回错误:(401)未经授权。
来源错误:
第80行:WebClient客户端=新WebClient();
第81行:client.Credentials = new NetworkCredential(“username”,“password”,“domain”);
第82行:client.DownloadFile(“https://some.server/file.zip”,filepath);
答案 0 :(得分:1)
使用从WebClient派生的此类。每次请求都会传递cookie。
class WebClientWithCookies: WebClient
{
private CookieContainer _container = new CookieContainer();
protected override WebRequest GetWebRequest(Uri address)
{
HttpWebRequest request = base.GetWebRequest(address) as HttpWebRequest;
if (request != null)
{
request.Method = "Post";
request.CookieContainer = _container;
}
return request;
}
}
答案 1 :(得分:0)