I have a program which uses a TIdHTTPServer. Now I want to write some automated tests using a TIdHTTP which talks to the TIdHTTPServer. The test code is in the program itself.
When the TIdHTTP tries to connect a 'Socket Error # 10061 Connection refused.' exception is raised. I'm guessing that's beacuse the TIdHTTPServer is using the port already.
Is it possible for a TIdHTTPServer and a TIdHTTP which are in the same executable to talk to each other at all? If so, how?
Yes, they can run in the same executable and connect to each other. Simply specify (one of) TIdHTTPServer's listening IP(s) in the URL that you pass to TIdHTTP, eg:
with IdHTTPServer1.Binding.Add do
begin
IP := '127.0.0.1';
Port := 80;
end;
IdHTTPServer1.Active := True;
...
IdHTTP1.Get('http://127.0.0.1/');
Related
I use TIdHTTP for easy get requests. Mostly it works well but on some sites I cannot establish connection even if I try various settings.
I tried to follow answer from here
To truly connect to "any" server, you would have to detect a "wrong
version" error and retry with a different specific Method/SSLVersions
configuration. Unfortunately, the "wrong version" reply does not
include the server's actual version, so you have to use
trial-and-error. If SSLv23 fails, try TLSv1_2. If that fails, try
TLSv1_1. If that fails, try TLSv1. If that fails, try SSLv3.
But it didn't help.
Exception -
'Error connecting with SSL.error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake failure'
Or undestandable SSL routines:SSL3_GET_RECORD:wrong version number.
But I cannot solve exactly SSL3_READ_BYTES:sslv3 alert
Sample code which reproduce problem and examples of websites:
begin
httpSender := TIdHTTP.Create(nil);
SSL := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
SSL.SSLOptions.Method := sslvTLSv1; //or sslvSSLv3, sslvTLSv1,sslvTLSv1_1
//or
//SSL.SSLOptions.Method := sslvSSLv23;
//SSL.SSLOptions.SSLVersions := [sslvSSLv2, sslvSSLv3, sslvTLSv1, sslvTLSv1_1, sslvTLSv1_2];
httpSender.IOHandler := SSL;
//exceptions here
httpSender.Get('https://www.linux.org/');
//httpSender.Get('https://st.deviantart.net/');
//httpSender.Get('https://c.tcdn.co/fa4/aa2/fa4aa23e-f55b-11e6-ba87-040157cdaf01/channel256.png');
end;
So how should I configure TIdHTTP for connecting to these websites?
I use Delphi XE8 and openssl 1.0.1e
None of the sites in your question needs a lower TLS protocol version. They are all perfectly capable of communicating with TLS 1.2 and sometimes even TLS 1.3. But all of these sites work only if the client uses the SNI TLS extension to advertise the target hostname within the TLS handshake.
It is thus more likely that the problem is actually a missing SNI extension. This seems to be a known problem. See TIdHTTP and TLS SNI doesnt work for more information and ways how to deal with the problem.
i have analyzed a site to see how data sent .. but i got some problem ..
the site use connect method to connect to site like this connet to "http://example.com:443" and there is no S in the http ..
there is no such method in Indy or clever compenents .. only post ,Get , put ..
this is a pic to understand me ..
so how i can use that connect method in indy or clever compenents .. !!
CONNECT is used to let a client connect to a target server through an HTTP proxy. This is most commonly used (but not limited) to proxying SSL connections, like those used for HTTPS.
Indy's TIdHTTP component uses CONNECT internally when you have configured a proxy Host:Port in the TIdHTTP.ProxyParams property and then:
request an HTTPS url.
request an HTTP url and have the hoNonSSLProxyUseConnectVerb flag enabled in the TIdHTTP.HTTPOptions property.
Indy also has a TIdConnectThroughHttpProxy component that can be used with Indy's other non-HTTP TCP clients to proxy connections through an HTTP proxy using CONNECT.
If I remember correctly, the following Synapse-based code would trigger a CONNECT to port 443 using the OpenSSL libraries...
procedure Test;
var HTTP:THTTPSend;
begin
HTTP:=THTTPSend.Create;
try
HTTP.Sock.CreateWithSSL(TSSLOpenSSL);
HTTP.Sock.SSLDoConnect; // CONNECT happens here
HTTP.HTTPMethod('GET','https://www.google.com/');
(...)
finally
HTTP.Free;
end;
end;
How, i've made a program that uploads files on ftp server, the thing is that whenever i don't have access to the internet, it appears me an error, which stops running the program and says that i am not connected to the internet.
How do i make the program appear that message in a showmessage box, so that it doesn't stop running the program?
For example:
If internetconnection then
begin
end else showmessage ('You are not connected to the internet')
Please try code from this link
Ping
Also you can try to use free Internet Component Suite components that allows to implement test connection to your ftp server.
EDIT:
Since it was found that the author uses IdFTP (Indy) component for upload files on ftp server and my first answer was not good i will take courage to write as i think correct code for checking connection to the ftp server:
with IdFTP1 do begin
Host := ..;
Port := ..;
Username := ..;
Password := ..;
if Connected then Disconnect;
try
Connect;
ShowMessage('FTP IS Connected')
except
ShowMessage('FTP IS NOT Connected');
end;
end;
I am developed one application for sending files client to server using TCP/IP Components using SSL.
Error is Connection Timed Out is occurred when Sending Large Files at Server Side
Server Side (SSL)
idTCPServer1.IOHandler := IdServerIOHandlerSSLOpenSSL1;
OnExecute
Acontext.Connection.IOHandler.LargeStream := True;
Acontext.Connection.IOHandler.ReadStream(filestream,Filesize); // Error is occurred here.
End;
Client Side (SSL)
idTCPClient1.IOHandler := IdSSLIOHandlerSocketOpenSSL1;
idTCPClient1.IOHandler.LargeStream := True;
idTCPClient1.IOHandler.WriteStream(fms,-1,True);
Connection is established between Client & Server. When send the files after some time Connection Timed out is error is shown at server & at client application going to Not Responding .
Here another scenario is when i connect the Client & Server as Normal using IdIOHandlerStack1 (at Client Side) and server side not assign any Handler. Files transfered successfully with out any problem.
I am using Delphi2010 & Indy10.5.5, DLL Version is 0.9.8.18
anyone guide to me
There is no WriteStream() method, there is a TStream overload of the Write() method instead.
You are telling Write() to send the stream size as a 64-bit integer, but you are not telling ReadStream() to read the stream size, so presumably you have read the full 64-bit file size beforehand using ReadInt64(), correct?
Also, 10.5.5 is an outdated version of Indy. The current version is 10.5.9. Try upgrading to make sure you have all of the latest bug fixes and such, and then report back if you are still having problems.
I'm using the TIdSSLIOHandlerSocketOpenSSL Indy component in Delphi XE2 to send data to an SSL server (Apple Push Notification Service) over TCP. I've got it working to a degree but not sure if I'm going about it the best way. I'm doing the following :
Set the SSL properties inc. path to certificates
Call the .Open method to open the connection
Check the AType parameter in the OnStatusInfoEx event until I get a 'Handshake Done'
Send the data using .WriteDirect
Close the connection with .Close
Is there a better way to know when the connection is ready to send data? Does anybody have sample code using the TIdSSLIOHandlerSocketOpenSSL component directly over TCP? The samples I've found are mainly for HTTP calls where the TIdSSLIOHandlerSocketOpenSSL component is just attached to secure the connection.
Since you are using the client component, you only need to setup the certificates on the client if the server is going to authenticate the client's certificate.
Otherwise, set the TIdSSLIOHandlerSocketOpenSSL's SSLOptions.Mode to sslmClient, and you should be able to connect.
It's a good idea to enable the VerifyMode and use the OnVerifyPeer event on the socket component to verify the fingerprint on the server certificate in order to avoid man in the middle attacks.
Depending on your version of Indy, you may need to set the SSLOptions Method to sslvTLSv1. Some web servers no longer support SSLv2, which Indy 9 defaults to.
Here's some sample code that demonstrates retrieving a web page over SSL using the TCP component:
procedure TForm1.Button1Click(Sender: TObject);
var
s: String;
begin
IdTCPClient1.Host := 'example.com';
IdTCPClient1.Port := 443;
IdTCPClient1.Connect;
IdTCPClient1.WriteLn('GET / HTTP/1.1');
IdTCPClient1.WriteLn('Host: example.com');
IdTCPClient1.WriteLn('');
// Retrieve all the data until the server closes the connection
s := IdTCPClient1.AllData;
Memo1.Lines.Add(s);
end;
Don't forget to include the OpenSSL libraries libeay32.sll and ssleay32.dll in the same folder as your EXE on Windows. Use the standard (latest) binaries for Indy 10.
This is what works for me. I am using Delphi 2010, but it this probably works just as well on Delphi XE2 (not tested). I use the current tip revision of Indy, which is revision 4774, rather that the out-of-the-box version.
I have 3 components on a datamodule or webmodule:
TIdHTTP
TIdSSLIOHandlerSocketOpenSSL
TidCookieManager
Hook all the components up to each other at design-time, with the following change at run-time: If the protocol is plain http:, then disconnect the TIdSSLIOHandlerSocketOpenSSL component. If the protocol is https:, then set the Handler property of the IdHTTP to the IdSSLIOHandlerSocketOpenSSL.
In the SSL Options, set method to sslvSSLv23 and SSLVersions to [sslvSSLv2,sslvSSLv3,sslvTLSv1,sslvTLSv1_1,sslvTLSv1_2]. I found that other permutations these properties just did not work. I can't explain it. I just know that this works with a wide range of webservers.
From there it is dead easy. To GET, simple call the Get() method on the TIdHTTP. For POST, call Post(). Parameters and Cookies are accessible by obviously named properties.
I had a lot of trouble get the out-of-the-box version to POST correctly, but with the tip revision, and setting the options as I mentioned, its been a breeze.