Thread View: borland.public.cppbuilder.internet.socket
6 messages
6 total messages
Started by Blake Young
Thu, 21 Jul 2005 20:51
http 302 error (redirect)
Author: Blake Young
Date: Thu, 21 Jul 2005 20:51
Date: Thu, 21 Jul 2005 20:51
69 lines
3189 bytes
3189 bytes
This is the location of the file I'm trying to download via my program:
https://webspace.utexas.edu/youngba2/www/winboxer/version.txt
Despite the fact that it has a secure URL (hence "https"), a username and
password is NOT needed to download the file. I tried using both the
TClientSocket and TIdHTTP components, but both of them are giving me the
same hassles. Namely, the response to the GET command is a 302 error,
saying that the file has been moved. I believe the problem is that I cannot
get either component to connect to the HTTPS location...they only try the
HTTP location. This may be clearer after reading the rest of my post. Here
is my code:
void __fastcall TForm1::Button2Click(TObject *Sender)
{
if (!ClientSocket1->Active)
{
ClientSocket1->Host = "webspace.utexas.edu";
ClientSocket1->Port = 80;
ClientSocket1->ClientType = ctNonBlocking;
ClientSocket1->Open();
}
}
void __fastcall TForm1::ClientSocket1Connect(TObject *Sender,
TCustomWinSocket *Socket)
{
ClientSocket1->Socket->SendText("GET /youngba2/www/winboxer/version.txt
HTTP/1.0\r\n\r\n");
}
void __fastcall TForm1::ClientSocket1Read(TObject *Sender,
TCustomWinSocket *Socket)
{
AnsiString response = Socket->ReceiveText();
Memo1->Lines->Add("File contents: " + response);
}
The response I get from ReceiveText() should just be the single line of text
that makes up the "version.txt" file, but instead I get the following:
HTTP/1.1 302 Found
Date: Thu, 21 Jul 2005 20:42:33 GMT
Server: Apache
Location: https://webspace.utexas.edu/youngba2/www/winboxer/version.txt
Connection: close
Content-Type: text/html; charset=iso-8859-1
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<HTML><HEAD>
<TITLE>302 Found</TITLE>
</HEAD><BODY>
<H1>Found</H1>
The document has moved <A
HREF="https://webspace.utexas.edu/youngba2/www/winboxer/version.txt">here</A>.<P>
</BODY></HTML>
The only thing I can think of is that maybe it's originally trying to go to
the non-secured URL first (i.e. http://webspace.utexas.edu/...) instead of
the secure https location. If this is indeed the problem, how can I use the
component (either TClientSocket or TIdHTTP) to access the https URL?
Thanks
Blake
Re: http 302 error (redirect)
Author: Blake Young
Date: Thu, 21 Jul 2005 21:02
Date: Thu, 21 Jul 2005 21:02
9 lines
1425 bytes
1425 bytes
There seems to be some promise in the properties of the IdHTTP component, but perhaps if somebody could point me in the right direction that would be great. Specifically, there is a property called HandleRedirects that lets the component handle the redirection automatically if set to true. However, since I'm being redirected to an HTTPS location, I need to also do something with the Intercept and InterceptEnabled properties (since apparently they are required for HTTPS connections). Am I on the right track?
Re: http 302 error (redirect)
Author: Remy Lebeau (TeamB)
Date: Thu, 21 Jul 2005 21:05
Date: Thu, 21 Jul 2005 21:05
60 lines
2930 bytes
2930 bytes
"Blake Young" <blakeyoung@mail.utexas.edu> wrote in message news:42e00af1$1@newsgroups.borland.com... > Despite the fact that it has a secure URL (hence "https"), a > username and password is NOT needed to download the file. Perhaps not, but an SSL negotiation is still required. > the response to the GET command is a 302 error, saying that > the file has been moved. As it should be. See below. > I believe the problem is that I cannot get either component to > connect to the HTTPS location... Yes, you can. TIdHTTP has support for HTTPS URLs. > they only try the HTTP location. That may be the case for your TClientSocket code, but that is not the case with TIdHTTP. > Here is my code: Your TClientSocket code is not HTTPS-aware at all. > ClientSocket1->Host = "webspace.utexas.edu"; > ClientSocket1->Port = 80; You are not accessing "https://webspace.utexas.edu" at all. You are accessing "http://webspace.utexas.edu" instead. HTTPS runs on port 443, not port 80. That makes a big difference. > The response I get from ReceiveText() should just be the single line > of text that makes up the "version.txt" file No, it should not, because you are not accessing the HTTPS URL at all. You can even see in the response from "http://webspace.utexas.edu" (port 80) that you are being redirected to "https://webspace.utexas.edu" (port 443) instead. > The only thing I can think of is that maybe it's originally trying to go to > the non-secured URL first (i.e. http://webspace.utexas.edu/...) instead > of the secure https location. That is exactly what your TClientSocket code is doing. > If this is indeed the problem, how can I use the component (either > TClientSocket or TIdHTTP) to access the https URL? For TClientSocket, you need to change the Port that is used. For TIdHTTP, it handles HTTPS automatically, so you do not need to do anything for it. Gambit
Re: http 302 error (redirect)
Author: Blake Young
Date: Thu, 21 Jul 2005 21:33
Date: Thu, 21 Jul 2005 21:33
42 lines
2581 bytes
2581 bytes
Thanks for the HTTPS vs HTTP explanation. I tried doing what you said, and
in one sense it worked, but in another it didn't. Using the TClientSocket,
I changed the port number to 443, but now the response is a flat-out 404
error. I have confirmed that the file is still in the correct location, so
I'm not sure why it can't be found. For the sake of completeness, I'll post
my complete and updated code. Is this a problem with my coding, or is the
server I'm trying to connect to refusing to let me connect? You can try
downloading the file for yourself and you should find that it exists. I
also tried using the IdHTTP component and I get the same 404 error, so I'm
guessing it's a problem with the server. I'm having the same problem trying
to download this file from a Tripod site that I uploaded it to
(http://winboxer.tripod.com/version.txt).
void __fastcall TForm1::Button2Click(TObject *Sender)
{
if (!ClientSocket1->Active)
{
ClientSocket1->Host = "webspace.utexas.edu";
ClientSocket1->Port = 443;
ClientSocket1->ClientType = ctNonBlocking;
ClientSocket1->Open();
}
}
void __fastcall TForm1::ClientSocket1Connect(TObject *Sender,
TCustomWinSocket *Socket)
{
Memo1->Lines->Add("Connected.");
ClientSocket1->Socket->SendText("GET /youngba2/www/winboxer/version.txt
HTTP/1.0\r\n\r\n");
}
void __fastcall TForm1::ClientSocket1Read(TObject *Sender,
TCustomWinSocket *Socket)
{
Memo1->Lines->Add("Reading...");
AnsiString response = Socket->ReceiveText();
Memo1->Lines->Add("Message: " + response);
}
Re: http 302 error (redirect)
Author: Remy Lebeau (TeamB)
Date: Thu, 21 Jul 2005 21:56
Date: Thu, 21 Jul 2005 21:56
20 lines
1684 bytes
1684 bytes
"Blake Young" <blakeyoung@mail.utexas.edu> wrote in message news:42e00d5b$1@newsgroups.borland.com... > since I'm being redirected to an HTTPS location, I need to also > do something with the Intercept and InterceptEnabled properties > (since apparently they are required for HTTPS connections). > Am I on the right track? There is no InterceptEnabled property anymore. If your version of Indy has that, then you are using a VERY old version, like Indy 8. If so, then please upgrade to Indy 9 or 10 instead. Anything older is no longer supported. With that said, the only thing you have to do in order to enable HTTPS support is to assign the IOHandler to a TIdSSLIOHandlerSocket component, and that is all. TIdHTTP handles the rest. Gambit
Re: http 302 error (redirect)
Author: Remy Lebeau (TeamB)
Date: Thu, 21 Jul 2005 22:02
Date: Thu, 21 Jul 2005 22:02
25 lines
1974 bytes
1974 bytes
"Blake Young" <blakeyoung@mail.utexas.edu> wrote in message news:42e014c4@newsgroups.borland.com... > Thanks for the HTTPS vs HTTP explanation. I tried doing what you > said, and in one sense it worked, but in another it didn't. Using the > TClientSocket, I changed the port number to 443, but now the > response is a flat-out 404 error. Your TClientSocket code is still not doing anything with SSL at all. Like you said earlier, HTTPS is a secure connection. You need to establish a secure connection with the server before you can do anything over HTTPS. I high suggest that you stop using TClientSocket. Both HTTP and SSL are very complex to properly code manually. There are plenty of pre-made implementations that you should be using instead. > I'm having the same problem trying to download this file from a Tripod > site that I uploaded it to (http://winboxer.tripod.com/version.txt). Works fine when I try it. But then, I'm using a much newer version of Indy than you apparently are. Gambit
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads