Check if a file exists on a remote server using Delphi - http

I need to check if a file exists on a server using delphi..
The idea is to send a request to the server (ex : http://www.example.com/file.txt) and check the status code of the response..
how is it done in delphi?

You can use the TIdHTTP class (included in Delphi). Simply create an instance at run time and use its Head method to retrieve information about the server resource.
MyIdHTTP.Head(TheURL);
ResponseCode := MyIdHTTP.Response.ResponseCode; // 200 = OK etc
ContentLength := MyIdHTTP.Response.ContentLength;
Note that it will not download the whole resource, and the value in ContentLength is not guaranteed ok (for example for dynamically created resources)

hyper(abstract) text transfer protocol.
you can use ftp protocol for this purpose

Related

copy file from local to remote server

I'm dynamically creating html files on my local system (using HTMLTEXTWRITER, then save them using streamwriter to local file system). I want to copy this file to my remote server withour user interaction, so that my users can read file. I use C#
for instance I want to copy from d:\myfile.html to mysite.com\myfile.html, how can I do it?
I have used this and it worked. may be useful
for holding path of local
rPath = "\\" & Request.UserHostAddress & "\c$\temp\"
for output file
rOutput = Session.SessionID & "_" & Format(Date.Now(), "ddMMyyhhmmss") & ".pdf"
now: report will be created at localhost\c\temp
You can't use the System.IO classes for this (unless you have access to the remote server as a network drive), but you can programmatically POST the file from the client to the remote server over HTTP using System.Net.
Here's a snippet using the WebRequest class:
WebRequest request = WebRequest.Create( url );
request.Timeout = 1000; // some appropriate value
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = 0; // set a length here
using( StreamWriter requestStream = new StreamWriter( request.GetRequestStream(), System.Text.Encoding.UTF8) ) {
// write to the stream here using requestStream.Write();
requestStream.Close();
}
More info for HTTP: http://msdn.microsoft.com/en-us/library/debx8sh9.aspx
Alternatively, you could use a protocol designed for transferring files like FTP (or something more secure) which isn't that hard to do in code.
FTP options: http://msdn.microsoft.com/en-us/library/ms229718
Is you remote server based on Windows and in the same workgroup or domain with you working machine ? If so, you can turn on the Windows File Sharing on the server. Then you can copy your file with cmd like this:
copy c:\test.txt \\mysite.com
The path "\\mysite.com" is also valid used by File.Copy in C#.
Otherwise, you need to set up a FTP environment on you server and use the FTP related API in C#.
You could set-up an FTP server and copy the files programmatically via FTP.
An example would be found here or here.
There are three ways by which you can copy the file to remote server.
Using normal file copy mode. Here you need to have access to the the webserver shared path. If the webserver is in same network as your application, then you can share the webroot and provide write access to the user who is running the application. He can then use File.Copy("source.txt", "\\Servername\SharedFolderName\target.txt").
The second approach is to use FTP to copy the file to the remote server. This MSDN example would help you on how to do this. This will work with most of the shared hosting providers.
You can use HTTP POST as noted by Tim. But this would let any user to perform the post. You may have to take care of user provisioning, authentication and authorization. IMO, keep this as last option as provisioning user and providing rights to certain path, may become cumbersome.

How to get executing server name or address with Selenium Server 2.20

When working with Selenium Server, it would be very useful to log the name of the machine that actually does the execution of the selenium script. Is it possible to get that information?
I am working with C# bindings, but answer in any language would do fine.
We should know where the Selenium Server is running.
This is Java Code :
we have straight method for this in HttpCommandExecutor class, getAddressOfRemoteServer()
code for Firefox :
RemoteWebDriver rcw = new RemoteWebDriver(new URL("http://serveraddress:portnumber/wd/hub"), DesiredCapabilities.firefox())
so if you have an instance of RemoteWebDriver
rcw.getCommandExecutor().getAddressOfRemoteServer()
code for IE :
Same as Above OR for Local
((HttpCommandExecutor)(new InternetExplorerDriver().getCommandExecutor())).getAddressOfRemoteServer();
Here's how to do this in Grid.
Please refer to this blog post of mine to learn how to find out the node ip and port to which the test was routed to.
Blog post : https://rationaleemotions.wordpress.com/2016/01/15/where-did-my-test-run/
In a nutshell, here's what you need to do (The blog I shared has elaborate explanation and required code )
Get the session id from webdriver via Webdriver.getSessionId()
You then append the session id obtained from the previous step to the URL http://localhost:4444/grid/api/testsession?session= (replace localhost with the actual Grid IP/host and replace 4444 with the port on which grid is listening to) and trigger a POST call.
From the JSON response you parse the value of attribute proxyId as a URL and extract out the IP and port from it.

iis7 website accessed externally downloads files to server instead of local machine

I've a site set up in IIS. It's allows users to download files from a remote cloud to their own local desktop. HOWEVER, the context seems to be mixed up, because when I access the website externally via the IP, and execute the download, it saves the file to the server hosting the site, and not locally. What's going on??
My relevant lines code:
using (var sw2 = new FileStream(filePath,FileMode.Create))
{
try
{
var request = new RestRequest("drives/{chunk}");
RestResponse resp2 = client.Execute(request);
sw2.Write(resp2.RawBytes, 0, resp2.RawBytes.Length);
}
}
Your code is writing a file to the local filesystem of the server. If you want to send the file to the client, you need to do something like
Response.BinaryWrite(resp2.RawBytes);
The Response object is what you use to send data back to the client who made the request to your page.
I imagine that code snippet you posted is running in some sort of code-behind somewhere. That is running on the server - it's not going to be running on the client. You will need to write those bytes in the Response object and specify what content-type, etc. and allow the user to Save the file himself.

HTTP POST request in Inno Setup Script

I would like to submit some information collected from user during Inno setup installation to our server via POST.
Obvious solution would be to include an .exe file that the setup would extract into temporary location and launch with parameters. However, I'm wondering - is there is any easier/better way?
Based on jsobo advice of using WinHTTP library, I came with this very simple code that does the trick. Say, you want to send serial number for verification just before the actual installation starts. In the Code section, put:
procedure CurStepChanged(CurStep: TSetupStep);
var
WinHttpReq: Variant;
begin
if CurStep = ssInstall then
begin
if AutoCheckRadioButton.Checked = True then
begin
WinHttpReq := CreateOleObject('WinHttp.WinHttpRequest.5.1');
WinHttpReq.Open('POST', '<your_web_server>', false);
WinHttpReq.SetRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
WinHttpReq.Send('<your_data>');
{ WinHttpReq.ResponseText will hold the server response }
end;
end;
end;
The Open method takes as arguments the HTTP method, the URL and whether to do async request and it seems like we need to add SetRequestHeader in order to set the Content-Type header to application/x-www-form-urlencoded.
WinHttpReq.Status will hold the response code, so to check if the server returned successfully:
if WinHttpReq.Status <> 200 then
begin
MsgBox('ERROR', mbError, MB_OK);
end
else
begin
MsgBox('SUCCESS', mbInformation, MB_OK);
end;
https://learn.microsoft.com/en-us/windows/win32/winhttp/winhttprequest lists all methods and properties of the WinHttpRequest object.
Also, to avoid run-time errors (can happen if the host is unreachable) it is a good idea to surround the code with try/except code.
You could always have your installer use curl
to make the http post...
You could write a pascal script right in innosetup to do the call utilizing the winhttp library
Or you could just write a vbscript and execute that with the cscript engine to do the same http call via the winhttp library.
That should point you to at least 3 different options to do what you need.
I think putting the exe in there would be the least error prone but utilizing the winhttp library with the pascal script (used by innosetup) would be the most simple.
I haven't tried it but the ISXKB has an entry for an uninstall survey that uses an HTTP POST:
http://www.vincenzo.net/isxkb/index.php?title=Uninstall_Survey

Reading a remote URL in Domino LotusScript

I have a remote RSS feed which has to be transformed into Notes documents using LotusScript.
I've looked through the documentation, but I can't find how to open a remote URL in order to retrieve its contents. In other words, some sort of wget- or curl-like functionality. Can anyone shed some light on how to do this? Using Java is not an option.
Thanks.
Check out the NotesDOMParser class - available in LotusScript - which lets you (indirectly) pull XML from a remote URL and process in a an XML DOM object.
You can pull the XML into a string using the MSXMLHTTP COM object, then use NotesStream to send the XML to the NotesDOMParser.
I have not tested, but the code would look something like this:
...
Set objXML = CreateObject("Microsoft.XMLHTTP")
objXML.open "GET", sURL, False, "", ""
objXML.send("")
sXMLAsText = Trim$(objXML.responseText)
Set inputStream = session.CreateStream
inputStream.Open (sXMLAsText)
Set domParser=session.CreateDOMParser(inputStream, outputStream)
domParser.Process
...
Documentation: http://publib.boulder.ibm.com/infocenter/domhelp/v8r0/index.jsp?topic=/com.ibm.designer.domino.main.doc/H_NOTESDOMPARSER_CLASS.html
You can't open a remote URL (whether it's HTTP or some other protocol) using native Lotusscript: the object library simply doesn't support it. If you're running on a Windows server, you should be able to use the MS XMLHttp DLLs to get a handle on your remote file via a URL, as specified by the previous answer. (Alternatively, this link specifies how to parse and open a UNC path with Lotusscript—again, Windows only).
All that said, if I understand you correctly, you're not using HTTP to access the remote file at all. If the RSS file is just on a simple path, why can't you open the file for parsing in the normal way with Lotusscript?

Resources