【问题标题】:Connect to LiveLink and download document连接到 LiveLink 并下载文档
【发布时间】:2014-08-15 19:16:52
【问题描述】:

我需要连接到 LiveLink 并下载一些 ISO PDF 文件。

我的连接方式是:

string url = "http://pod.iso.org/isostd/livelink?func=ll.login&Username=user&password=pasword";
string urlDoc = "http://pod.iso.org/isostd/livelink?func=doc.fetch&nodeid=705699&doctitle=ISO_11095";
string responseString = string.Empty;
WebRequest wReq = WebRequest.Create(url);
HttpWebRequest httpReq = (HttpWebRequest)wReq;
httpReq.Method = "POST";
httpReq.AllowAutoRedirect = false;
HttpWebResponse response = (HttpWebResponse)httpReq.GetResponse();            
string resp = response.StatusCode.ToString();

变量 resp 给了我“OK”,所以我已登录到该站点,但我不知道如何继续。变量 urlDoc 是要下载的文档的 url,但我无法继续。

提前谢谢,原谅我的英语不好。

【问题讨论】:

    标签: c# iso livelink


    【解决方案1】:

    适合我的代码:

    protected override void Login()
    {
        var httpparams = new Hashtable();
        httpparams.Add("func", "ll.login");
        httpparams.Add("CurrentClientTime", DateTime.Now.ToString("D/yyyy/M/dd:H:m:s"));
        httpparams.Add("Username", this.LoginName);
        httpparams.Add("Password", this.Password);
    
        byte[] requestPostParams = WebHelper.GetWebPostData(httpparams);
    
        HttpWebRequest loginRequest = this.GetWebRequest(this.Uri);
        loginRequest.Method = "POST";
        loginRequest.GetRequestStream().Write(requestPostParams, 0, requestPostParams.Length);
        loginRequest.GetRequestStream().Close();
    
        var response = (HttpWebResponse)loginRequest.GetResponse();
        response.Cookies = loginRequest.CookieContainer.GetCookies(loginRequest.RequestUri);
    }
    
    public static byte[] GetWebPostData(Hashtable httpparams)
    {
        StringBuilder requestStream = new System.Text.StringBuilder();
        foreach (string paramname in httpparams.Keys)
        {
            if (requestStream.Length == 0)
            {
                requestStream.Append(string.Format("{0}={1}", paramname, httpparams[paramname]));
            }
            else
            {
                requestStream.Append(string.Format("&{0}={1}", paramname, httpparams[paramname]));
            }
        }
        UTF8Encoding encoding = new UTF8Encoding();
        return encoding.GetBytes(requestStream.ToString());
    }
    private void validateConnection()
    {
        if ((!this._IsConnected) || this._LastExecutionTime < DateTime.Now.AddMinutes(-15))
        {
            this._IsConnected = false;
            try
            {
                // recreate cookies container
                this._CookiesContainer = new CookieContainer();
    
                // login to livelink
                this.Login();
    
                this._IsConnected = true;
                this.ResetCounter();
            }
            catch (Exception e)
            {
                string context = string.Format("Login to: {0}; user: {1}", this.Name, this.LoginName);
                this.NextAttempt(e, context);
    
                // try one more time
                this.validateConnection();
            }
        }
    }
    
    /// <summary>
    /// Download file from livelink based on provided query string
    /// </summary>
    public bool DownloadFileUri(string fileUri, string destinationFile)
    {
        // validate input
        if (string.IsNullOrEmpty(fileUri))
            return false;
    
        if (string.IsNullOrEmpty(destinationFile))
            return false;
    
        // check that adapter is connected to livelink
        this.validateConnection();
    
        // get file content
        FileStream fs = null;
        HttpWebResponse response = null;
        try
        {
            //Retreive file
            var httpRequest = this.GetWebRequest(fileUri);
            httpRequest.Method = "GET";
            httpRequest.Proxy = ConnectorBase.GetProxy();
            response = (HttpWebResponse)httpRequest.GetResponse();
    
            int bytesRead;
            int bytesReceived = 0;
            var buffer = new byte[1000000];
    
            Stream receiveStream = response.GetResponseStream();
            fs = new FileStream(destinationFile, FileMode.Create);
            while ((bytesRead = receiveStream.Read(buffer, 0, buffer.Length)) > 0)
            {
                fs.Write(buffer, 0, bytesRead);
                bytesReceived += bytesRead;
                this.OnDataDownload(bytesReceived);
            }
    
            // close file stream
            fs.Close();
            fs = null;
    
            // check file information
            var fi = new FileInfo(destinationFile);
    
            this.ResetCounter();
    
            // check file size and return true if file size > 0
            return fi.Length > 0;
        }
        catch (WebException e)
        {
            ResetAdapter();
    
            string context = string.Format("Uri: {0}", fileUri);
            this.NextAttempt(e, context);
    
            return DownloadFileUri(fileUri, destinationFile);
        }
        catch (Exception e)
        {
            ResetAdapter();
    
            string context = string.Format("Uri: {0}", fileUri);
            Logger.Error("Error: {0}, context: {1}", e.ToString(), context);
    
            throw;
        }
        finally
        {
            if (fs != null)
                fs.Close();
            if (response != null)
                response.Close();
        }
    }
    

    如果您需要更多帮助,请告诉我,一般情况下,我可以共享代码以下载文件或解析 Livelink 的内容以下载页面上引用的所有文档

    【讨论】:

      【解决方案2】:

      为了从 Livelink 下载文档,最简单的方法是使用 LiveServices,它不过是 Livelink 的 WCF 服务。 您可以创建一个客户端,然后通过传递文档的 ObjectId/DataId 来下载它。 如果您需要有关客户端代码的帮助,请告诉我。

      【讨论】:

      • 您好,抱歉耽误了我的回答,但两周无法连接。我会很感激有关客户端代码的一些帮助,你能寄给我同样的样本吗?提前致谢!
      【解决方案3】:
      Here's a simple method that will create a folder in Livelink. 
      
      //LAPI Example
      
      private int CreateFolder(int parentId, string folderName)
              {
      llSession = new LLSession(host, Convert.ToInt32(port), string.Empty, userId, userPwd);
                  int folderId = 0;
                  LAPI_DOCUMENTS llDoc;
                  LLValue objectInfo = (new LLValue()).setAssoc();
      
                  try
                  {
                      llDoc = new LAPI_DOCUMENTS(llSession);
                      if (llDoc.AccessEnterpriseWS(objectInfo) == 0)
                      {
                          this.llDocuments = llDoc;
                          llDoc.CreateFolder(llVolume, parentId, folderName, objectInfo);
                          folderId = objectInfo.toInteger("ID");
      
                          log.Info(folderName + " folder created successfully");
                          return folderId;
                      }
                      else
                      {
                          log.Info(folderName + " could not be created");
                          return 0;
                      }
                  }
                  catch (Exception ex)
                  {
                      log.Error(ex.Message);
                      return 0;
                  }
              } 
      
      
      //WCF Example getting Node info
      
       private void GetNodeInfo()
              {
                  AuthenticationClient authClient = new AuthenticationClient();
                  DocumentManagementClient docMan = new DocumentManagementClient();
                  DocumentManagement.OTAuthentication otAuth = new DocumentManagement.OTAuthentication();
                  string authToken = authClient.AuthenticateUser(userId, userPwd);
                  otAuth.AuthenticationToken = authToken;
      
                  try
                  {
                    DocumentManagement.Node node = new Node();
      
                    node = docMan.GetNode(otAuth, Convert.ToInt32(dataRow[dataColumn].ToString()));
                  }
                  catch (Exception ex)
                  {
                      log.Error(ex.Message);
                  }
                  finally
                  {
                      authClient.Close();
                      docMan.Close();
                  }
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-11-10
        • 1970-01-01
        • 2015-12-03
        • 2015-05-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多