0

list.asmx(getlistitemsメソッド)を使用してWindowsアプリケーションを作成し、別のサーバーにあるドキュメントライブラリからドキュメントをダウンロードしました。ドキュメント名やURLなどにアクセスできます。次のコードを使用してファイルをダウンロードすると、ダウンロードしようとしている各ファイルのコンテンツとしてログインページのhtmlが返されます。

何かご意見は?

            Dim spAuthentication As New Authentication()
            spAuthentication.Url = authenticationWSAddress
            spAuthentication.CookieContainer = New CookieContainer()
            Dim spLists As New Lists()
            spLists.Url = listWSAddress
            'Try to login to SharePoint site with Form based authentication
            Dim loginResult As LoginResult = spAuthentication.Login(userName, password)
            Dim cookie As New Cookie()
            'If login is successfull
            If loginResult.ErrorCode = LoginErrorCode.NoError Then
                'Get the cookie collection from the authenticatin web service
                Dim cookies As CookieCollection = spAuthentication.CookieContainer.GetCookies(New Uri(spAuthentication.Url))
                'Get the specific cookie which contains the security token

                cookie = cookies(loginResult.CookieName)
                'Initialize the cookie container of the list web service
                spLists.CookieContainer = New CookieContainer()
                'set the cookie of list web service to the authenticatio cookie
                spLists.CookieContainer.Add(cookie)
                'Dim responseNode As XmlNode = spLists.GetListCollection()
                'response = responseNode.InnerXml

                Dim query As String = "<mylistitems><Query><Where><Eq><FieldRef Name='FileDirRef' /><Value Type='Url'>DocLib/Property Documents/BELASERA AT FULTON (lax10027)/Master Meter Invoices</Value></Eq></Where></Query><QueryOptions><ViewAttributes Scope='RecursiveAll' IncludeRootFolder='False' /><IncludeAttachmentUrls>TRUE</IncludeAttachmentUrls><ViewFields><FieldRef Name='EncodedAbsUrl'/></ViewFields></QueryOptions></mylistitems>"
                Dim doc As New XmlDocument()
                doc.LoadXml(query)
                Dim dt As DataTable = Nothing
                Dim queryNode As XmlNode = doc.SelectSingleNode("//Query")
                Dim viewNode As XmlNode = doc.SelectSingleNode("//ViewFields")
                Dim optionNode As XmlNode = doc.SelectSingleNode("//QueryOptions")

                Dim retNode As XmlNode = spLists.GetListItems("DocLib", String.Empty, queryNode, viewNode, String.Empty, optionNode, Nothing)

                Dim ds As New DataSet()
                Using sr As New StringReader(retNode.OuterXml)
                    ds.ReadXml(sr)
                End Using

                If ds.Tables("Row") IsNot Nothing AndAlso ds.Tables("Row").Rows.Count > 0 Then
                    dt = ds.Tables("Row").Copy()
                    For Each myrow As DataRow In dt.Rows
                        '  myrow.Item(0) contains url of the document
                        If myrow.Item(0) IsNot Nothing AndAlso myrow.Item(0) <> "" Then
                            DownLoadAttachmentold(myrow.Item("ows_EncodedAbsUrl"), RemoveLookupID(myrow.Item("ows_FileLeafRef")))
                        End If
                    Next
                End If




  Public Shared Sub DownLoadAttachment(ByVal strURL As String, ByVal strFileName As String)
        Dim myWebClient As New WebClient()
        Dim DestinationFolder As String = "C:\\DownLoads\\"
        Form2.RTBStatus.AppendText("Downloading File " + strFileName + " from " + strURL + " .......")
        ' The DownloadFile() method downloads the Web resource and saves it into the current file-system folder.
        myWebClient.DownloadFile(strURL, DestinationFolder + strFileName)
        'Form2.RTBStatus.AppendText("Successfully Downloaded file ""{0}"" from ""{1}""", "C:\\DownLoads\\" + strFileName, strURL)
        Form2.RTBStatus.AppendText((ControlChars.Cr + "Downloaded file saved in the following file system folder:" + ControlChars.Cr + ControlChars.Tab + DestinationFolder))

    End Sub
4

1 に答える 1

0

DownloadAttachment は、(フォームベースの) 認証済み HTTP 要求を作成する必要もあります。

以下の C# の例:

request = (HttpWebRequest)WebRequest.Create(strURL);
request.Credentials = System.Net.CredentialCache.DefaultCredentials;//adapt for your FBA
request.AllowWriteStreamBuffering = false;
response = (HttpWebResponse)request.GetResponse();
Stream s = response.GetResponseStream();
FileStream fs = new FileStream(@"C:\DownLoads\"+strFileName, FileMode.Create);
于 2012-10-23T10:49:13.973 に答える