I need to connect to LiveLink and download some ISO PDF files.
The way I connect is:
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();
The variable resp gives me "OK", so I'm logged intoto the site but I don't know how to continue. The variable urlDoc is the url that of the document to download but I'm not able to continue.
Thnks in advance and excuse my poor english.
Code that works for me:
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();
}
}
let me know if you need more help, in general, I can share code to download files or parse the content of the Livelink to download all documents referenced on the page