0

我搜索了各种论坛和链接,但还没有任何有用的信息。

实际上,我正在努力使用 C# 在 ASP.net 中开发一个 Web 应用程序,该应用程序可以用来登录远程服务器,然后从该服务器下载文件。

目前我能够登录到该服务器并查看受保护页面的内容。

但是在下载文件时,我收到了 Unauthorize access 错误。

我可以从服务器下载图像,但这些图像不受保护。

到目前为止我开发的代码是

    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) Unauthorized。说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

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

源错误:

第 80 行:WebClient 客户端 = new WebClient();

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

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

4

2 回答 2

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;
    }
}
于 2012-06-25T09:35:51.860 回答
0

尝试检查这个库:http ://www.rebex.net/default.aspx

于 2012-06-25T08:54:08.317 回答