c# - WebClient DownloadFile with Authorization not working -


i have tried can think of work, including several things i've found online. i'm trying download file (which has direct link) website have log in to.

i tried doing following, "uploadvalues":

            webclient mywebclient = new webclient();             namevaluecollection mynamevaluecollection = new namevaluecollection();             mynamevaluecollection.add("username", this.username);             mynamevaluecollection.add("password", this.password);             byte[] responsearray = mywebclient.uploadvalues(felony, mynamevaluecollection);             mywebclient.downloadfile(felony, localfelony); 

and i've tried putting login info in headers well. i've tried setting credentials, can see commented code:

            webclient client = new webclient();             //client.usedefaultcredentials = false;             //client.credentials = new networkcredential(this.username, this.password);             client.headers.add(httprequestheader.authorization, "basic " + convert.tobase64string(encoding.ascii.getbytes(this.username + ":" + this.password)));             client.headers.add(httprequestheader.useragent, "mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, gecko) chrome/51.0.2704.103 safari/537.36");             //client.headers.add(httprequestheader.cookie, this.webbrowser.document.cookie);             client.downloadfile(felony, localfelony); 

no matter try, thing can download file ends being login page, if didn't accept login info passed.

i've looked @ headers , such, , don't see out of ordinary explain why isn't working. ideas? thanks.

this seems authentication/authorization issue.

there many reasons causing like: 1) may authentication/authorization mechanism uses kind of hash. 2) may using wrong kind of authentication mechanism ("basic" can see). 3) may getting authenticated not authorized.

the best way find root cause is: use fiddler. login using ui page , try download file. while doing capture fiddler session. there try same whatever code have. again capture fiddler session. compare fiddler find difference.

hope helps.


Comments