Download file from intranet which requires to be logged in

netuser

Active Member
Joined
Jun 19, 2015
Messages
420
Hi,

I need a little help to find a way to download a file from intranet page that require to be logged in . I tried a lot of code found here and on google and nothing works

I have the link to the file (it is not direct link with file extension) but when I am already logged in and I input the url it will popup to save the file (here is how link look like, I modified the name of real link

Code:
https://my-documents.company.com/folder/fpath/default/default-domain/Favorites@xl?contentViewName=filename&currentPage=0&pageSize=0

If I am not already logged in it will redirect me to the page to login :

Code:
https://my-documents.company.com/folder/login.jsp  (no popup just a form style login page)


How can I automatized the download of the file to a specific folder? When not logged in I want popup to ask in excel for username and pass and then download file, if already logged in I want it to download to a specific folder automatically.

Thanks for your help
 
Not quite correct. If that is a genuine JSESSIONID related to the GET request, and not just an example in the HTML, then the following code will extract it (you need the JSESSIONID= and the .nuxxx at the end).
Code:
    Dim p1 As Long, p2 As Long
    p1 = InStr(1, .responseText, "jsessionid=", vbTextCompare)
    p2 = InStr(p1, .responseText, " -->")
    JSESSIONID = Mid(.responseText, p1, p2 - p1)

Although it is different at each session (which may indicate it is not an example), it still is not working and I am getting HTML login page as xls. Also I tried to check with F12 and the cookie was different on same page then resposentext.

So we really need to get that Cookie in order to correctly download it.

Thanks again for all your help
 
Upvote 0

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Yes, I think the JSESSIONID cookie is needed to download the file.

But I just noticed this contentViewState parameter and value in the download request URL:
HTML:
Demande    GET /nuxxx/nxpath/default/XXX/workspaces/xxx/xx@xl?contentViewName=ORL_CLAI_CVI-TasksAdminVerificationTeamA&currentPage=0&pageSize=0&contentViewState=H4sIAAAAAAAAAJ2SMU%252FDMBCF%252F4vnlMKaLaRIVIpoRKMsCEWHc2ksHDucnZYS5b9zTjqgMsFmn%252F29d%252B%252FsUUhrPBpfKjw9QYciFrvnrEqzZFul5XZVgHt3Sd0pUyKpRknwypoCoUtEJHo4YE72qGqk%252F9F79cXU3W0k5EDEjeRcFDHvPwakcw7Esh7JifiFtbfiNRIOgWS7sXLoGBDxKPy5%252F4t3JY%252FBnmyP5BWy9jhNrGvJb01jg9c471Krh878VN6nj6tUg%252BpiGlxbNRoOYiETJ9HUyhxE3IB2OEVXGrWMJSF4rH8TngacOBqhG7TP4GyHOZi5Gmpxn83BMuU8gxtw7ZsFqkMtPMBumaLEDfuwjVdeB4H6MqxKL2A4IjBO87ViuRNaiITi%252F5CDb5lZh7VbX6NVKN%252F0s4Zr7Sndlw%252BfPce5pJi%252BAWd54LdUAgAA HTTP/1.1
contentViewState probably changes when you navigate from the login page to the download page. It is usually defined in the web page's HTML in an INPUT tag with type="hidden", like this:
HTML:
< input type="hidden" name="contentViewState" id="contentViewState" value="H4sIAAAAAAAAAJ2SMU%252FDMBCF%25....." >

You may need to extract "contentViewState" from the HTML like this, assuming the id is "contentViewState"
Code:
    Dim contentViewState As String
    contentViewState = HTMLdoc.getElementById("contentViewState").Value
and use the value (URL-encoded) in the download request URL. There may be other hidden input elements which you may need to specify.
 
Upvote 0
you mean when we click on download link ? how can I get Html code of it ? or you mean main page where link is ?
 
Upvote 0
On main page where download link is, there is some hidden input but they are for other links, like refresh, but for download (Excel Export) there is no Hidden contentViewState
 
Upvote 0
You need to look at all pages, from the login page to the page with the download link and the download itself. The contentViewState, if present, is for the whole page, not a specific link.

The hidden input elements/tags are usually within a form element/tag. For example, View Source to look at this thread's HTML and there is:

HTML:
< form action="http://www.mrexcel.com/forum/search.php?do=process" method="post" id="navbar_search" class="navbar_search">               
                < input type="hidden" name="securitytoken" value="1465239056-010dc241b0a93fe36c6a1ef555969d65ca0f7f21" />
                < input type="hidden" name="do" value="process" />
 
Upvote 0
you want me to post all these hidden tags here for login and page with download link ?

How do you get HTML source of a download link ? it is not a page
 
Upvote 0
Only post relevant hidden tags. Those are tags whose parameter name or value string is used in any of the page requests/responses, either as part of the URL (in the query string after the '?' in the URL) or in POST form parameters, or in cookies. In HTML tags, you may see id="parameterName" or name="parameterName" and value="valueString".

Yes, the download link isn't a page, but where does the contentViewState parameter value in the download URL come from? You say there isn't a hidden input element called contentViewState. Maybe the download URL and/or contentViewState value in the URL query string is being generated by a Javascript function embedded in the page or called in a separate .js file (contains JavaScript code). Your GET requests show a number of .js files being returned to the browser.

Working half blind like this and without seeing the exact details of the requests and responses for myself it is very difficult to help you. And I would use Fiddler because you can compare IE's requests and responses with VBA's XMLhttp requests and responses and change the code if needed. IE's developer tools only show the browser's requests and responses, so sending requests with VBA XMLhttp you have no way of telling whether the requests are correct or not.
 
Last edited:
Upvote 0
Unfortunately Fiddler require Admin to install it. I wish it was just an add on. I am unable to find information to help you more, I understand it is not easy as you cant see all info., I am going to send you in PM the whole source code of the page, like this it will be easier for you.
 
Upvote 0
The HTML helps slightly.

There is a Javascript file which the browser downloads - src="/Nuxxx/javax.faces.resource/js/fixviewstate.js.faces"

This file, fixviewstate.js.faces, should be in your IE cache (temporary internet files), or you can view it directly from http://www.yoursite.com/Nuxxx/javax.faces.resource/js/fixviewstate.js.faces. Could you send the file contents (JavaScript source code) to me because I'm guessing that it may have something do with contentViewState.

You don't have to install Fiddler; you can run the executable, Fiddler.exe, after extracting the files from the download. See debugging - Is there a non-installable version of Fiddler, or equivalent? - Stack Overflow.
 
Upvote 0
Thanks for that info, I am not at work today, I will get back to you tomorrow with that. And also will try fiddler without installing.
 
Upvote 0

Forum statistics

Threads
1,217,383
Messages
6,136,272
Members
450,001
Latest member
KWeekley08

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top