-
[RESOLVED] [Problem] Download Image From Url Using Winsock
Hey There Programmers,
I can't download an image from an specific url, like: http://example.com/example.jpg
The header I send is:
GET /example.jpg HTTP/1.1
The response in DataArrival event is (forexample:)
Code:
ÿØÿàŸÏйàCzáGéR‰ˆã¿øR‚¬@#kvÇA@[|ño}6)ËkmæÜEµº|ƒœÔ®8çŸïSCž~¾‚€"Ç‘{üƒŠ_²ÚñþŽžß(ÿ{S8Ǩÿ
Of course I put the data in a string variable and some weird characters appear :D :bigyello:
So the thing is how can I save the DataArrival As an Image?
Thanks in Advance.
-
Re: [Problem] Download Image From Url Using Winsock
Maybe that strange looking characters are the jpg image bytes
Try:
Open App.Path & "\example.jpg" for Binary As #1
Put #1, 1, StringVariable
Close #1
After saving it see if it is a valid picture by double clicking on it
-
Re: [Problem] Download Image From Url Using Winsock
Quote:
Originally Posted by
jmsrickland
Maybe that strange looking characters are the jpg image bytes
Try:
Open App.Path & "\example.jpg" for Binary As #1
Put #1, 1, StringVariable
Close #1
After saving it see if it is a valid picture by double clicking on it
Thanks for the response,
I saved it and it became about 3 KB,
But unfortunately that wasn't a valid picture as I opened it and I faced an error saying:
"No Preview Available".
-
Re: [Problem] Download Image From Url Using Winsock
Why are you trying to use Winsock instead of the Inet control?
-
Re: [Problem] Download Image From Url Using Winsock
Quote:
Originally Posted by
Nightwalker83
Why are you trying to use Winsock instead of the Inet control?
I just want to learn this legendary winsock, not any important reasons.
Also the fastest way to download files, send POST/GET requests, make client applications, etc is only possible with winsock component.
-
Re: [Problem] Download Image From Url Using Winsock
Winsock Making HTTP Post/Get Requests demo by VisualAd might include a download method.
-
Re: [Problem] Download Image From Url Using Winsock
Quote:
Originally Posted by
aliforever
Thanks for the response,
I saved it and it became about 3 KB,
But unfortunately that wasn't a valid picture as I opened it and I faced an error saying:
"No Preview Available".
Without seeing your code hard to say what you have but perhaps it is still the image data but you just don't have all of it.
If you post the exact URL I will download it and see if the data I get is like the data you get
-
Re: [Problem] Download Image From Url Using Winsock
Quote:
Originally Posted by
Nightwalker83
I'm afraid I couldn't find anything about downloading.
Quote:
Originally Posted by
jmsrickland
Without seeing your code hard to say what you have but perhaps it is still the image data but you just don't have all of it.
If you post the exact URL I will download it and see if the data I get is like the data you get
With pleasure,
the url is:
Code:
http://games5.sepanta.com/forum/uploads/profile/photo-thumb-2.jpg
Code:
Private Sub Winsosck1_Connect()
strHeader = "GET /forum/uploads/profile/photo-thumb-2.jpg HTTP/1.1" & vbCrLf
Winsock1.SendData strHeader
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim data As String
Winsock1.GetData data
strData = strData & data
FileBytes = FileBytes + bytesTotal
Label2.Caption = FileBytes
End Sub
Private Sub Winsock1_Close()
strTempFile = App.Path & "\tmp.jpg"
intFile = FreeFile
Open App.Path & "\example.jpg" For Binary As #1
Put #1, 1, strData
Close #1
ReceiveData = ""
Text1.Text = strData
End Sub
strData has been Dimed in general :D
I've also tested strConv to convert string to byte and save it, but again I couldn't get the downloaded image to show.
Thanks for spending time to help me. :p
-
Re: [Problem] Download Image From Url Using Winsock
Sorry, I cannot connect to either of the two below
Winsock1.Connect "www.games5.sepanta.com", 80
Winsock1.Connect "www.sepanta.com", 80
Please show me how you connected
BTW:
This is an invalid URL for Winsock
http://games5.sepanta.com/forum/uplo...to-thumb-2.jpg
Code:
ÿØÿàŸÏйàCzáGéR‰ˆã¿øR‚¬@#kvÇA@[|ño}6)ËkmæÜEµº|ƒœÔ®8çŸïSCž~¾‚€"Ç‘{üƒŠ_²ÚñþŽžß(ÿ{S8Ǩÿ
ÿØÿà
are the 1st four bytes of a jpg image but the rest of that mumbo-jumbo is still jpg data but it appears to be out of sequence. You say you got 3K but the entire image is 13KB
-
Re: [Problem] Download Image From Url Using Winsock
Quote:
Originally Posted by
jmsrickland
Sorry, I cannot connect to either of the two below
Winsock1.Connect "www.games5.sepanta.com", 80
Winsock1.Connect "www.sepanta.com", 80
Please show me how you connected
BTW:
This is an invalid URL for Winsock
http://games5.sepanta.com/forum/uplo...to-thumb-2.jpg
Code:
ÿØÿàŸÏйàCzáGéR‰ˆã¿øR‚¬@#kvÇA@[|ño}6)ËkmæÜEµº|ƒœÔ®8çŸïSCž~¾‚€"Ç‘{üƒŠ_²ÚñþŽžß(ÿ{S8Ǩÿ
ÿØÿà
are the 1st four bytes of a jpg image but the rest of that mumbo-jumbo is still jpg data but it appears to be out of sequence. You say you got 3K but the entire image is 13KB
I'm afraid about that, the profile owner changed his avatar, so the size was changed.
So, I decided to work on this new link:
HTML Code:
http://www.vbforums.com/icom_includes/footers/img/developer_logo.jpg
I could download 15.1 KB with winsock, but again no display available.
My Code:
Code:
Dim strData as String
Private Sub cmdConnect_Click()
winsock1.connect "www.vbforums.com", 80
End Sub
Private Sub Winsock1_Connect()
strHeader = "GET /icom_includes/footers/img/developer_logo.jpg HTTP/1.1" & vbCrLf
strHeader = strHeader & "Host: www.vbforums.com" & vbCrLf & vbCrLf
Winsock1.SendData strHeader
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim data As String
Winsock1.GetData data
strData = strData & data
FileBytes = FileBytes + bytesTotal
Label2.Caption = FileBytes
End Sub
Private Sub Winsock1_Close()
strTempFile = App.Path & "\tmp.jpg"
intFile = FreeFile
Open strTempFile For Binary As #1
Put #1, 1, strData
Close #1
End Sub
Thanks again and sorry about the last url, my mistake.
-
Re: [Problem] Download Image From Url Using Winsock
Maybe you should change from String data to Byte data. In your DataArrival get the data into a byte array then in your close event write the byte array out to the hard drive.
In the DataArrival event define a byte array
Dim byteArray() as Byte
then use the Get
Winsock1.GetData byteArray, vbByte + vbArray
-
Re: [Problem] Download Image From Url Using Winsock
Quote:
Originally Posted by
jmsrickland
Maybe that strange looking characters are the jpg image bytes
Try:
Open App.Path & "\example.jpg" for Binary As #1
Put #1, 1, StringVariable
Close #1
After saving it see if it is a valid picture by double clicking on it
That's a terrible way to do that. You guys should really stop this practice of dealing with binary data in strings. That may have worked in the 90s when it was the norm to have 1 byte per character, but we live in a Unicode world now where encoding would get you nothing but problems. Nonetheless, I don't believe that's the problem in this case. VB6 is still primitive enough for you to get away with things like that in some circumstances.
Quote:
Originally Posted by
aliforever
Thanks for the response,
I saved it and it became about 3 KB,
But unfortunately that wasn't a valid picture as I opened it and I faced an error saying:
"No Preview Available".
Given that you're communicating using the HTTP protocol, I'm guessing you have to parse out some kind of HTTP header. I believe this is where the trouble might be coming from. I'm not sure on the specifics of HTTP when transferring images but given the nature of HTTP, what I said is a likely the case.
-
Re: [Problem] Download Image From Url Using Winsock
Quote:
Originally Posted by
jmsrickland
Remove the HTTP component. That only means something to browsers. The DNS should be able to resolve this:-
Code:
www.games5.sepanta.com/forum/uploads/profile/photo-thumb-2.jpg
If not then remove the www component:-
Code:
games5.sepanta.com/forum/uploads/profile/photo-thumb-2.jpg
-
Re: [Problem] Download Image From Url Using Winsock
Quote:
Originally Posted by
Niya
That's a terrible way to do that. You guys should really stop this practice of dealing with binary data in strings. That may have worked in the 90s when it was the norm to have 1 byte per character, but we live in a Unicode world now where encoding would get you nothing but problems. Nonetheless, I don't believe that's the problem in this case. VB6 is still primitive enough for you to get away with things like that in some circumstances.
Given that you're communicating using the HTTP protocol, I'm guessing you have to parse out some kind of HTTP header. I believe this is where the trouble might be coming from. I'm not sure on the specifics of HTTP when transferring images but given the nature of HTTP, what I said is a likely the case.
Yeah I guess those headers that server responds ruins the structure of the image file.
Quote:
Originally Posted by
jmsrickland
Maybe you should change from String data to Byte data. In your DataArrival get the data into a byte array then in your close event write the byte array out to the hard drive.
In the DataArrival event define a byte array
Dim byteArray() as Byte
then use the Get
Winsock1.GetData byteArray, vbByte + vbArray
I have tested this too, but no result.
Probably Nia is right, would you help me on this, I mean parsing out the extra headers in data arrival?
I guess I have to use a Do - Loop function,
but it is a little complicated, need your help.
And thanks again for the responses.
-
Re: [Problem] Download Image From Url Using Winsock
Quote:
Originally Posted by
Niya
That's a terrible way to do that. You guys should really stop this practice of dealing with binary data in strings. That may have worked in the 90s when it was the norm to have 1 byte per character, but we live in a Unicode world now where encoding would get you nothing but problems. Nonetheless, I don't believe that's the problem in this case. VB6 is still primitive enough for you to get away with things like that in some circumstances.
Either way you get the same results, believe it or not. When it comes to image or video data it's all the same and besides when you get image/video data from a website it is sent to you in string data
I will agree with you if you are dealing with something other than this
-
Re: [Problem] Download Image From Url Using Winsock
Just save your input to a file then look at that file and you will see if there are any text data headers, etc. That will tell you what you have to remove to get to the actual image data which should be several lines down in the file preceded by a textual length of the data. After that length value is the data you want
Like this:
Header Info
Header Info
Header Info
etc
12345 '<-----length
ÿØÿàŸÏйàCzáGéR‰ˆã¿øR‚¬@#kvÇA@[|ño}6)ËkmæÜEµº|ƒ '<---- image data
-
Re: [Problem] Download Image From Url Using Winsock
I decided to tackle this myself and its not receiving any image. I'm getting a bad request response:-
Code:
HTTP/1.1 400 Bad Request
Server: nginx
Date: Tue, 29 Jul 2014 01:40:30 GMT
Content-Type: text/html
Content-Length: 166
Connection: close
<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx</center>
</body>
</html>
This is my test code:-
vb Code:
Private strReceivedData As String
Private Sub Form_Load()
Winsock1.Connect "www.games5.sepanta.com", 80
End Sub
Private Sub Winsock1_Close()
Debug.Print strReceivedData
End Sub
Private Sub Winsock1_Connect()
Dim strHeader As String
strHeader = "GET /forum/uploads/profile/photo-thumb-2.jpg HTTP/1.1" & vbCrLf & vbCrLf
Winsock1.SendData strHeader
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim strn As String
Winsock1.GetData strn, vbString, bytesTotal
strReceivedData = strReceivedData + strn
End Sub
Private Sub Winsock1_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
MsgBox "Winsock error:" + Description
End Sub
-
Re: [Problem] Download Image From Url Using Winsock
Quote:
Originally Posted by
Niya
I decided to tackle this myself and its not receiving any image. I'm getting a bad request response:-
Code:
HTTP/1.1 400 Bad Request
Server: nginx
Date: Tue, 29 Jul 2014 01:40:30 GMT
Content-Type: text/html
Content-Length: 166
Connection: close
<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx</center>
</body>
</html>
This is my test code:-
vb Code:
Private strReceivedData As String
Private Sub Form_Load()
Winsock1.Connect "www.games5.sepanta.com", 80
End Sub
Private Sub Winsock1_Close()
Debug.Print strReceivedData
End Sub
Private Sub Winsock1_Connect()
Dim strHeader As String
strHeader = "GET /forum/uploads/profile/photo-thumb-2.jpg HTTP/1.1" & vbCrLf & vbCrLf
Winsock1.SendData strHeader
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim strn As String
Winsock1.GetData strn, vbString, bytesTotal
strReceivedData = strReceivedData + strn
End Sub
Private Sub Winsock1_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
MsgBox "Winsock error:" + Description
End Sub
ow, sorry I forgot to edit my post, that website wasn't suitable.
So I changed my testing Image :D .
As I mentioned in the 10th post:
http://www.vbforums.com/showthread.p...=1#post4729297
So when I opened the jpg image with notepad, those extra headers were there,
I removed them and saved the image but didn't display either that way. (I am gonna test with vb6 too)
But I saw that after modifying the jpg the size of the file became exactly the same as when you download it with your browser.
But the only problem is that it is not displaying.
-
Re: [Problem] Download Image From Url Using Winsock
Quote:
Originally Posted by
aliforever
You're using the VBForums image now right ? I'm, still getting a bad request with that too:-
Code:
HTTP/1.1 400 Bad Request
Date: Tue, 29 Jul 2014 02:13:38 GMT
Server: Apache
Vary: Accept-Encoding
Content-Length: 279
Connection: close
Content-Type: text/html; charset=iso-8859-1
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
<hr>
<address>Apache Server at kal Port 80</address>
</body></html>
vb Code:
'
Private Sub Form_Load()
Winsock1.Connect "www.vbforums.com", 80
End Sub
Private Sub Winsock1_Connect()
Dim strHeader As String
strHeader = "GET /icom_includes/footers/img/developer_logo.jpg HTTP/1.1" & vbCrLf & vbCrLf
Winsock1.SendData strHeader
End Sub
-
1 Attachment(s)
Re: [Problem] Download Image From Url Using Winsock
I discovered something weird,
Attachment 116925
As you can see there are some differences between both files.
I think the problem has to do with DataArrival.
Any Ideas?
-
Re: [Problem] Download Image From Url Using Winsock
Quote:
Originally Posted by
Niya
You're using the VBForums image now right ? I'm, still getting a bad request with that too:-
Code:
HTTP/1.1 400 Bad Request
Date: Tue, 29 Jul 2014 02:13:38 GMT
Server: Apache
Vary: Accept-Encoding
Content-Length: 279
Connection: close
Content-Type: text/html; charset=iso-8859-1
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
<hr>
<address>Apache Server at kal Port 80</address>
</body></html>
vb Code:
'
Private Sub Form_Load()
Winsock1.Connect "www.vbforums.com", 80
End Sub
Private Sub Winsock1_Connect()
Dim strHeader As String
strHeader = "GET /icom_includes/footers/img/developer_logo.jpg HTTP/1.1" & vbCrLf & vbCrLf
Winsock1.SendData strHeader
End Sub
Would you mind testing it with this requests:
Code:
strHeader = "GET /icom_includes/footers/img/developer_logo.jpg HTTP/1.1" & vbCrLf
strHeader = strHeader & "Host: www.vbforums.com" & vbCrLf & vbCrLf
Winsock1.SendData strHeader
-
Re: [Problem] Download Image From Url Using Winsock
Quote:
Originally Posted by
aliforever
Also the fastest way to download files, send POST/GET requests, make client applications, etc is only possible with winsock component.
Complete nonsense. I shudder to think where you are getting your information.
You are far better off using the built in AsyncRead method or one of the HTTP client components that ship as part of Windows. You can forget about the WinInet control since it is obsolete except as a tool of desperation for FTP, which it really isn't good at anyway.
If you need to use POST and other methods beyond GET consider the XmlHttpRequest or WinHttpRequest objects.
Watching people use Strings for binary data is an excercise in frustration. At best you end up doing expensive double-conversion to get the desired results and at worst you can corrupt the data: converting from ANSI to Unicode and back is not a lossless proposition though it is worse for DBCS locales. There is never an excuse for doing this.
-
Re: [Problem] Download Image From Url Using Winsock
If you're getting Bad Request and you can't figure it out then maybe you can use a packet sniffer and see what the browser sends as headers to get the jpg data
-
2 Attachment(s)
Re: [Problem] Download Image From Url Using Winsock
Not much to it:
SinkCallbacks.cls
Code:
Option Explicit
Public Event onreadystatechange()
'Mark as default member via Tools|Procedure Attributes... dialog.
Public Sub RSChange()
RaiseEvent onreadystatechange
End Sub
Form1.frm
Code:
Option Explicit
Private HReq As MSXML2.XMLHTTP
Private WithEvents SinkCallbacks As SinkCallbacks
Private Sub Form_Load()
Set HReq = New MSXML2.XMLHTTP
Set SinkCallbacks = New SinkCallbacks
With HReq
.open "GET", "http://www.vbforums.com/icom_includes/footers/img/developer_logo.jpg", True
.onreadystatechange = SinkCallbacks
.send
End With
End Sub
Private Sub SinkCallbacks_onreadystatechange()
Dim Vector As WIA.Vector
If HReq.readyState = 4 Then
Set Vector = New WIA.Vector
With Vector
.BinaryData = HReq.responseBody
Set Picture = .Picture
End With
End If
End Sub
We have covered this here many times.
-
Re: [Problem] Download Image From Url Using Winsock
Adding some error handling:
Code:
Private Sub SinkCallbacks_onreadystatechange()
Dim Vector As WIA.Vector
With HReq
If .readyState = 4 Then
If .Status = 200 Then
Set Vector = New WIA.Vector
With Vector
.BinaryData = HReq.responseBody
Set Picture = .Picture
End With
Else
Print "Error " & CStr(.Status) & " " & .statusText
End If
End If
End With
End Sub
-
Re: [Problem] Download Image From Url Using Winsock
Quote:
Originally Posted by
dilettante
Not much to it:
SinkCallbacks.cls
Code:
Option Explicit
Public Event onreadystatechange()
'Mark as default member via Tools|Procedure Attributes... dialog.
Public Sub RSChange()
RaiseEvent onreadystatechange
End Sub
Form1.frm
Code:
Option Explicit
Private HReq As MSXML2.XMLHTTP
Private WithEvents SinkCallbacks As SinkCallbacks
Private Sub Form_Load()
Set HReq = New MSXML2.XMLHTTP
Set SinkCallbacks = New SinkCallbacks
With HReq
.open "GET", "http://www.vbforums.com/icom_includes/footers/img/developer_logo.jpg", True
.onreadystatechange = SinkCallbacks
.send
End With
End Sub
Private Sub SinkCallbacks_onreadystatechange()
Dim Vector As WIA.Vector
If HReq.readyState = 4 Then
Set Vector = New WIA.Vector
With Vector
.BinaryData = HReq.responseBody
Set Picture = .Picture
End With
End If
End Sub
We have covered this here many times.
As a newbie, this WIA.vector is a library or something?
I have downloaded wiaaut.dll and used it in vb6,
it added 3 controls.
And when I run your project it gives me an error saying: user-defined type not defined.
Thanks for helping.
______________________________
Edited: I didn't register the dll using regsvr32, sorry.
-
Re: [Problem] Download Image From Url Using Winsock
Thanks, I didn't know anything about this xmlhttp, I just knew that the only way to send headers is to use winsock.
With this xmlhttp can I parse the responsetext as a html document?
-
Re: [Problem] Download Image From Url Using Winsock
WIA 2.0 has been around since 2006 and ships in all supported versions of Windows. Windows XP isn't supposed to be used by anyone anymore and has become a risk to all users since it is now an open target for spambots and malware distribution agents.
Quote:
Originally Posted by
aliforever
Thanks, I didn't know anything about this xmlhttp, I just knew that the only way to send headers is to use winsock.
With this xmlhttp can I parse the responsetext as a html document?
Yes, of course you can. But now it seems that you are writing webscrapers rather than writing client application using HTTP as you suggested.
Webscraping is almost always against a site's Terms of Service, and only legal when explicitly permitted. Piracy is a crime and I don't assist criminals.
-
Re: [Problem] Download Image From Url Using Winsock
Quote:
Originally Posted by
dilettante
WIA 2.0 has been around since 2006 and ships in all supported versions of Windows. Windows XP isn't supposed to be used by anyone anymore and has become a risk to all users since it is now an open target for spambots and malware distribution agents.
Yes, of course you can. But now it seems that you are writing webscrapers rather than writing client application using HTTP as you suggested.
Webscraping is almost always against a site's Terms of Service, and only legal when explicitly permitted. Piracy is a crime and I don't assist criminals.
Thanks for solving my problem.
It is not what you think,
So far, I wanted to write an activation for my projects; which connects to a php written script and activates program.
Getting sufficient result is possible by parsing the response text.
So don't see me a criminal :D
by the way thanks to all who helped me, I learned about bytes.