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
 
here is js.faces source code, let me know if it helps otherwise I will try Fiddler way

Code:
var OmniFaces=OmniFaces||{};
OmniFaces.FixViewState=function(){function r(e){var t=e.getElementsByTagName("update");for(var r=0;r<t.length;r++){if(n.exec(t[r].getAttribute("id"))){return t[r].firstChild.nodeValue}}return null}function i(e){var n=e.elements;for(var r=0;r<n.length;r++){if(n[r].name==t){return n[r]}}return null}function s(e,n){var r;try{r=document.createElement("<input name='"+t+"'>")}catch(i){r=document.createElement("input");r.setAttribute("name",t)}r.setAttribute("type","hidden");r.setAttribute("value",n);r.setAttribute("autocomplete","off");e.appendChild(r)}var e={};var t="javax.faces.ViewState";var n=new RegExp("^([\\w]+:)?"+t.replace(/\./g,"\\.")+"(:[0-9]+)?$");e.apply=function(e){if(typeof e==="undefined"){return}var t=r(e);if(!t){return}for(var n=0;n<document.forms.length;n++){var o=document.forms[n];var u=i(o);if(o.method=="post"&&!u){s(o,t)}else if(o.method=="get"&&u){u.parentNode.removeChild(u)}}};return e}();if(typeof jsf!=="undefined"){jsf.ajax.addOnEvent(function(e){if(e.status=="success"){OmniFaces.FixViewState.apply(e.responseXML)}})}if(typeof jQuery!=="undefined"){jQuery(document).ajaxComplete(function(e,t,n){if(typeof t!=="undefined"){OmniFaces.FixViewState.apply(t.responseXML)}})}
 
Upvote 0

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
here you go for fiddler too, RAW data as you asked

Login page :

Code:
GET https://orl-documents.company.com/nuxxx/login.jsp?tabIds=WORKFLOW_DASHBOARD%3Atransaction_request%3AORL_CLAI_CVI-TasksTeamA&requestedUrl=nxworkflow%2Fdefault%2FIRO%2Fworkspaces%2FORE-CIRO%2FIRO%2F60262%2FAccounts%2F586%2FCessions%2F2164435-1%2FClaims%40view_workflow%3FtabIds%3DWORKFLOW_DASHBOARD%253Atransaction_request%253AORL_CLAI_CVI-TasksTeamA HTTP/1.1
Accept: application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
Accept-Encoding: gzip, deflate
Cookie: JSESSIONID=D84C98CE41E3713D749047B381A3DB95.nuxxx; org.jboss.xxxx.core.Locale=en_US
Host: orl-documents.company.com
Connection: Keep-Alive
Accept-Language: fr-CA
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)

Main page with download link:


Code:
GET https://orl-documents.company.com/nuxxx/nxworkflow/default/IRO/workspaces/ERO-CIRO/IRO/60262/Accounts/Work@view_workflow?tabIds=WORKFLOW_DASHBOARD%3Atransaction_request%3AORL_CLAI_CVI-TasksTeamA&language=en_US HTTP/1.1
Accept: application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
Referer: https://orl-documents.company.com/nuxxx/login.jsp?tabIds=WORKFLOW_DASHBOARD%3Atransaction_request%3AORL_CLAI_CVI-TasksTeamA&requestedUrl=nxworkflow%2Fdefault%2FIRO%2Fworkspaces%2FERO-CIRO%2FIRO%2F60262%2FAccounts%2F586%2FCessions%2F2164435-1%2FWork%40view_workflow%3FtabIds%3DWORKFLOW_DASHBOARD%253Atransaction_request%253AORL_CLAI_CVI-TasksTeamA
Accept-Language: fr-CA
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)
Accept-Encoding: gzip, deflate
Host: orl-documents.company.com
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: JSESSIONID=D84C98CE41E3713D749047B381A3DB95.nuxxx; org.jboss.xxxx.core.Locale=en_US

Clicked on Download link :

Code:
GET https://orl-documents.company.com/nuxxx/nxpath/default/IRO/workspaces/ERO-CIRO/IRO/60262/Accounts/Work@xl?contentViewName=ORL_CLAI_CVI-TasksTeamA&currentPage=0&pageSize=0&contentViewState=H4sIAAAAAAAAAJ2SMU%252FDMBCF%252F4vnlMKaLaRIVIpoRKMsCEWHc2ksHDucnZYS5b9zTjqgMsFmn%252F29d%252B%252FsUUhrPBpfKjw9QYciFrvnrEqzZFul5XZVgHt3Sd0pUyKpRknwypoCoUtEJHo4YE72qGqk%252F9F79cXU3W0k5EDEjeRcFDHvPwakcw7Esh7JifiFtbfiNRIOgWS7sXLoGBDxKPy5%252F4t3JY%252FBnmyP5BWy9jhNrGvJb01jg9c471Krh878VN6nj6tUg%252BpiGlxbNRoOYiETJ9HUyhxE3IB2OEVXGrWMJSF4rH8TngacOBqhG7TP4GyHOZi5Gmpxn83BMuU8gxtw7ZsFqkMtPMBumaLEDfuwjVdeB4H6MqxKL2A4IjBO87ViuRNaiITi%252F5CDb5lZh7VbX6NVKN%252F0s4Zr7Sndlw%252BfPce5pJi%252BAWd54LdUAgAA HTTP/1.1
Accept: application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
Referer: https://orl-documents.company.com/nuxxx/nxworkflow/default/IRO/workspaces/ERO-CIRO/IRO/60262/Accounts/586/Cessions/2164435-1/Work@view_workflow?tabIds=WORKFLOW_DASHBOARD%3Atransaction_request%3AORL_CLAI_CVI-TasksTeamA&language=en_US
Accept-Language: fr-CA
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)
Accept-Encoding: gzip, deflate
Host: orl-documents.company.com
Connection: Keep-Alive
Cookie: JSESSIONID=D84C98CE41E3713D749047B381A3DB95.nuxxx; org.jboss.xxxx.core.Locale=en_US
 
Upvote 0
Thanks. My guess that the FixViewState js.faces code was something to do with contentViewState, was wrong - it doesn't seem to affect it. In any case, the contentViewState parameter value in the Fiddler requests is exactly the same as the IE requests from many days earlier - all 496 characters. This means that contentViewState is probably not significant and is just a static value in the download link, which we can obtain using the code in http://www.mrexcel.com/forum/excel-...e-intranet-requires-logged-2.html#post4536654.

I still think JSESSIONID is important, but not sure what else to suggest. The only thing I can think of is to look carefully in Fiddler at all the sessions from the initial login page, logging in, navigating to the download page and clicking the download link. You can ignore sessions with content types image (e.g. jpeg, png) and css, and probably js/JavaScript. But examine in detail any sessions with content types text/html, text/plain and Excel or ms-excel. On the Inspectors tab, look carefully at the raw data (headers, cookies, form data) of all these requests and also at the raw data of the responses. See if you can spot any strings in these requests and responses which also occur in the HTML source of all pages visited in downloading the file.
 
Upvote 0
Tried to check but I don't see anything more then what I posted above. I don't understand why we cant capture Cookie and then set it back to download. That is our main issue. I read on google somewhere that Cookies are striped out in httpq request for security reason. seem like it is the case. How will we download silently then ?

Thanks
 
Upvote 0
Yes, I mentioned that XMLhttp strips out cookies in the response in post #24 http://www.mrexcel.com/forum/excel-...e-intranet-requires-logged-3.html#post4537595. However, AFAIK WinHttpRequest - used in that code - doesn't.

Another thing to try, based on noticing that the GET request to the download URL returns a http 302 status (location moved), is the following code. This is basically the same as the above linked code, but disables the automatic redirection of http 302 status, gets the URL from the Location header in the 302 response and sends another GET request to that URL to download the file.

Code:
'References required - tick these in Tools -> References in VBA editor
'Microsoft Internet Controls
'Microsoft HTML Object Library
'Microsoft WinHTTP Services version 5.1

Public Sub IE_WinHttp_Download_File1()
    
    Dim URL As String
    Dim IE As InternetExplorer
    Dim HTMLdoc As HTMLDocument
    Dim imgs As IHTMLElementCollection, imgExcel As HTMLImg, i As Long
    Dim downloadURL As String
    
    Dim httpReq As WinHttpRequest
    Set httpReq = New WinHttpRequest
    
    'Dim httpReq As Object
    'Set httpReq = CreateObject("WinHttp.WinHttpRequest.5.1")
    
    '#If VBA7 Then
    '    Dim httpReq As WinHttpRequest60
    '    Set httpReq = New WinHttpRequest60
    '#Else
    '    Dim httpReq As WinHttpRequest
    '    Set httpReq = New WinHttpRequest
    '#End If
    
    Dim fileNum As Integer, Buffer() As Byte
    Dim saveInFolder As String, localFile As String
    Dim res As Variant
    Dim JSESSIONID As String
    
    'Folder in which the downloaded file will be saved
    
    saveInFolder = ThisWorkbook.Path
    If Right(saveInFolder, 1) <> "\" Then saveInFolder = saveInFolder & "\"
    
    localFile = saveInFolder & "Excel workbook.xls"
    
    URL = "https://www.YourIntranetSite.com"   'CHANGE THIS TO THE CORRECT URL
    
    With httpReq
        
        'Send GET to the home page and extract the JSESSIONID from the "Set-Cookie" response header
        'JSESSIONID=5913D49F2E35EA9D0xxxxx.nuxxx; org.xxx.seam.core.Locale=en_US
        
        .Open "GET", URL, False
        .send
        
        Debug.Print .Status, .statusText
        Debug.Print .getAllResponseHeaders
        
        JSESSIONID = Split(.getResponseHeader("Set-Cookie"), ";")(0)
    End With
    
    Set IE = New InternetExplorer
    With IE
        .navigate URL
        .Visible = True
        While .readyState <> READYSTATE_COMPLETE
            DoEvents
        Wend
        
        AppActivate Application.Caption
        res = MsgBox("Navigate to the web page containing the Excel Export link, logging in if necessary." & vbNewLine & _
            "Then click OK to continue and download the file, or click Cancel to quit.", vbOKCancel)
        If res = vbCancel Then Exit Sub
        
        Set HTMLdoc = .document
    End With
    
    'Find the "Excel Export" img tag
    
    Set imgs = HTMLdoc.getElementsByTagName("IMG")
    i = 0
    Set imgExcel = Nothing
    While i < imgs.Length And imgExcel Is Nothing
        If imgs(i).Title = "Excel Export" Then Set imgExcel = imgs(i)
        i = i + 1
    Wend
    
    If Not imgExcel Is Nothing Then
    
        downloadURL = imgExcel.parentElement.href
    
        Set httpReq = New WinHttpRequest
        
        With httpReq
        
            'Send GET to request same page as current IE page (might not need this)
            
            '.Open "GET", IE.LocationURL, False
            '.setRequestHeader "Accept", "application/x-ms-application, image/jpeg, application/xaml+xml, image/xxxf, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"
            '.setRequestHeader "Accept-Language", "fr-CA"
            '.setRequestHeader "Accept-Encoding", "gzip, deflate"
            '.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)"
            '.setRequestHeader "Referer", IE.LocationURL
            .send
            
            'Send GET to request the Excel file
            
            .Open "GET", downloadURL, False
            
            'Disable automatic http redirect, so we can extract the redirect URL from the response
        
            .Option(WinHttpRequestOption_EnableRedirects) = False
            
            '.setRequestHeader "Accept", "application/x-ms-application, image/jpeg, application/xaml+xml, image/xxxf, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"
            '.setRequestHeader "Accept-Language", "fr-CA"
            '.setRequestHeader "Accept-Encoding", "gzip, deflate"
            '.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)"
            '.setRequestHeader "", ""   'other header?
            .send
            
            If .Status = 302 Then
                        
                'Get the redirect URL from the Location header
                
                downloadURL = .getResponseHeader("Location")
                
                'Send GET to (hopefully) download the Excel file
            
                .Open "GET", downloadURL
                .send
            
                'If successful, save response in the local file
            
                If .Status = 200 Then
                    fileNum = FreeFile
                    Open localFile For Binary Access Write As #fileNum
                    Buffer = .responseBody
                    Put #fileNum, 1, Buffer
                    Close #fileNum
                    MsgBox "Downloaded " & localFile
                Else
                    MsgBox "URL = " & downloadURL & vbNewLine & "http request returned status " & .Status & vbNewLine & .statusText
                End If
            
            Else
            
                'Not the expected 302 response
                
                MsgBox "URL = " & downloadURL & vbNewLine & "Expected response = 302" & vbNewLine & "Actual response = " & .Status & vbNewLine & .statusText
                
            End If
                
        End With
        
    Else
    
        MsgBox "Unable to find IMG tag with title=""Excel Export"""
        
    End If
    
End Sub
 
Last edited:
Upvote 0
Thanks for last code and while I was reading your message I got an idea and this new thing solved one of our main issue getting Cookie.

I replaced my URL of page with URL of login page and Boom now I am getting no more error on

JSESSIONID = Split(httpReq.getResponseHeader("Set-Cookie"), ";")(0)

And I have the cookie :)

On next block of code when we do

With IE
.navigate URL

I replaced it with my main page URL, that I called URL2

With IE
.navigate URL2


On httpReq I have to uncomment .Open otherwise I was getting an error that you cannot Send without Open.

All went good I was excited but end result is same I am getting HTLM login page files :(

Then I added these lines before .Send to all 3 places :

.setRequestHeader "Cookie", JSESSIONID

But still HTML file.

At least now we have the cookies, why we are still getting HTML file ?

Thanks again for all your help

(Also have to replace InternetExplorer with InternetExplorerMedium)
 
Upvote 0
Good news and progress at last!

I suspect you'll have to do everything with XMLHTTP/WinHttpRequest and not involve IE at all, because any session id or URL parameters/query string used by IE will be separate to those used by XMLHTTP. That means using XMLHTTP/WinHttpRequest to send a GET request to the login page, send a GET request to log in, send a GET request to the page containing the download link, then send a GET request to the download link to download the file.
 
Last edited:
Upvote 0
Another thing I just noticed is that downloadURL link you are creating now is to page login.jsp, I doubt it will download with that.

Now :

Code:
https://orl-documents.company.com/nuxxx/login.jsp?contentViewName=ORL_CLAI_CVI-TasksTeamA&contentViewState=H4sIAAAAAAAAAJ2SMU%252FDMBCF%252F4vnlMKaLaRIVIpoRKMsCEWHc2ksHDucnZYS5b9zTjqgMsFmn%252F29d%252B%252FsUUhrPBpfKjw9QYciFrvnrEqzZFul5XZVgHt3Sd0pUyKpRknwypoCoUtEJHo4YE72qGqk%252F9F79cXU3W0k5EDEjeRcFDHvPwakcw7Esh7JifiFtbfiNRIOgWS7sXLoGBDxKPy5%252F4t3JY%252FBnmyP5BWy9jhNrGvJb01jg9c471Krh878VN6nj6tUg%252BpiGlxbNRoOYiETJ9HUyhxE3IB2OEVXGrWMJSF4rH8TngacOBqhG7TP4GyHOZi5Gmpxn83BMuU8gxtw7ZsFqkMtPMBumaLEDfuwjVdeB4H6MqxKL2A4IjBO87ViuRNaiITi%252F5CDb5lZh7VbX6NVKN%252F0s4Zr7Sndlw%252BfPce5pJi%252BAWd54LdUAgAA&pageSize=0&requestedUrl=nxpath%2Fdefault%2FORI%2Fworkspaces%2FORE-ORIC%2FORI%40xl%3FcontentViewName%3DORL_CLAI_CVI-TasksAdminVerificationTeamA%26currentPage%3D0%26pageSize%3D0%26contentViewState%3DH4sIAAAAAAAAAJ2SMU%25252FDMBCF%25252F4vnlMKaLaRIVIpoRKMsCEWHc2ksHDucnZYS5b9zTjqgMsFmn%25252F29d%25252B%25252FsUUhrPBpfKjw9QYciFrvnrEqzZFul5XZVgHt3Sd0pUyKpRknwypoCoUtEJHo4YE72qGqk%25252F9F79cXU3W0k5EDE
jeRcFDHvPwakcw7Esh7JifiFtbfiNRIOgWS7sXLoGBDxKPy5%25252F4t3JY%25252FBnmyP5BWy9jhNrGvJb01jg9c471Krh878VN6nj6tUg%25252BpiGlxbNRoOYiETJ9HUyhxE3IB2OEVXGrWMJSF4rH8TngacOBqhG7TP4GyHOZi5Gmpxn83BMuU8gxtw7ZsFqkMtPMBumaLEDfuwjVdeB4H6MqxKL2A4IjBO87ViuRNaiITi%25252F5CDb5lZh7VbX6NVKN%25252F0s4Zr7Sndlw%25252BfPce5pJi%25252BAWd54LdUAgAA&currentPage=0

Before:

Code:
https://orl-documents.company.com/nuxxx/nxpath/default/IRO/workspaces/ERO-CIRO/IRO@xl?contentViewName=ORL_CLAI_CVI-TasksTeamA&currentPage=0&pageSize=0&contentViewState=H4sIAAAAAAAAAJ2SMU%252FDMBCF%252F4vnlMKaLaRIVIpoRKMsCEWHc2ksHDucnZYS5b9zTjqgMsFmn%252F29d%252B%252FsUUhrPBpfKjw9QYciFrvnrEqzZFul5XZVgHt3Sd0pUyKpRknwypoCoUtEJHo4YE72qGqk%252F9F79cXU3W0k5EDEjeRcFDHvPwakcw7Esh7JifiFtbfiNRIOgWS7sXLoGBDxKPy5%252F4t3JY%252FBnmyP5BWy9jhNrGvJb01jg9c471Krh878VN6nj6tUg%252BpiGlxbNRoOYiETJ9HUyhxE3IB2OEVXGrWMJSF4rH8TngacOBqhG7TP4GyHOZi5Gmpxn83BMuU8gxtw7ZsFqkMtPMBumaLEDfuwjVdeB4H6MqxKL2A4IjBO87ViuRNaiITi%252F5CDb5lZh7VbX6NVKN%252F0s4Zr7Sndlw%252BfPce5pJi%252BAWd54LdUAgAA


How could I do everything with XMLHTTP/WinHttpRequest ? In any case I don't want any user interaction (only when they are not loged in it should popup in excel to ask user and pass)
 
Last edited:
Upvote 0
How could I do everything with XMLHTTP/WinHttpRequest ?
You need to emulate all the html requests seen in Fiddler, using the same code as shown previously:

Code:
    Set HTMLdoc = New HTMLDocument
    With httpReq
        .Open "GET", URL, False    'change URL string as needed
        .setRequestHeader "User-Agent", "yyyyyy"
        '.setRequestHeader "xxxx", "yyyy" 'maybe other headers as well, e.g. Set-Cookie JSESSIONID after the initial GET
        .send
        Debug.Print .Status, .statusText     '
        Debug.Print .getAllResponseHeaders    'you may need to extract params/values for use in later requests
        Debug.Print .responseText    'is the expected response?

        'Put response in HTMLDocument for parsing - you may need to extract params/values for use in later requests
        HTMLdoc.body.innerHTML = .responseText
    End With
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,313
Members
452,634
Latest member
cpostell

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