从受保护的服务器asp.net下载文件 [英] asp.net Download files from protected server

查看:93
本文介绍了从受保护的服务器asp.net下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我寻觅各种论坛和链接,但没有带什么东西有用呢。

I have searched various forums and links but havn't got anything helpful yet.

其实我正努力使用C#,允许使用登录远程服务器上,然后从服务器下载文件制定在ASP.net的Web应用程序。

Actually i working towards developing a web application in ASP.net using C# that enables use to login on a remote server and then download files from that server.

有curently读音字能够登录到该服务器,并看到受保护的页面的内容。

Curently i m able to login to that server and see the content of the protected page.

但是,当涉及到下载的文件,我得到一个取消授权访问错误。

But when it comes to download the file, i get an Unauthorize access error.

我为能够从服务器上下载图像,但这些都没有得到保护。

I m able to download images from the server but those are not protected.

在code到目前为止,我已经开发为

the code i have developed so far is

    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());
    }
}

在/的WebSite1应用程序中

服务器错误。

远程服务器返回错误:(401)未经授权。
说明:执行当前Web请求的执行过程中发生未处理的异常。请查看有关错误的详细信息的堆栈跟踪以及它起源于code。

Server Error in '/WebSite1' Application.

The remote server returned an error: (401) Unauthorized. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

异常详细信息:System.Net.WebException:远程服务器返回错误:(401)未经授权

Exception Details: System.Net.WebException: The remote server returned an error: (401) Unauthorized.

源错误:

80号线:Web客户端的客户端=新的WebClient();

Line 80: WebClient client = new WebClient();

81号线:client.Credentials =新的NetworkCredential(用户名,密码,域);

Line 81: client.Credentials = new NetworkCredential("username", "password", "domain");

82号线:client.DownloadFile(HTTPS://some.server/file.zip,文件路径);

Line 82: client.DownloadFile("https://some.server/file.zip", filepath);

推荐答案

使用从WebClient的派生这个类。它总是会通过饼干的每个请求。

Use this class derived from WebClient. It will always pass the cookies with every request.

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;
    }
}

这篇关于从受保护的服务器asp.net下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆