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
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
Re: [Problem] Download Image From Url Using Winsock
Why are you trying to use Winsock instead of the Inet control?
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu. https://get.cryptobrowser.site/30/4111672
Re: [Problem] Download Image From Url Using Winsock
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.
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu. https://get.cryptobrowser.site/30/4111672
Re: [Problem] Download Image From Url Using Winsock
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
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
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
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.
Last edited by aliforever; Jul 28th, 2014 at 01:16 PM.
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
Last edited by jmsrickland; Jul 28th, 2014 at 03:51 PM.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
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.
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
Last edited by jmsrickland; Jul 28th, 2014 at 08:14 PM.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
Re: [Problem] Download Image From Url Using Winsock
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.
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.
C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter
There's just no reason to use garbage like InputBox. - jmcilhinney
The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber
C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter
There's just no reason to use garbage like InputBox. - jmcilhinney
The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber
Re: [Problem] Download Image From Url Using Winsock
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.
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?
Re: [Problem] Download Image From Url Using Winsock
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
Last edited by jmsrickland; Jul 28th, 2014 at 08:37 PM.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
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
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
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)
C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter
There's just no reason to use garbage like InputBox. - jmcilhinney
The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber
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)
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.
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>
C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter
There's just no reason to use garbage like InputBox. - jmcilhinney
The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber
Re: [Problem] Download Image From Url Using Winsock
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>
Re: [Problem] Download Image From Url Using Winsock
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
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
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
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
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.
Last edited by aliforever; Jul 28th, 2014 at 11:31 PM.
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.
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
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
by the way thanks to all who helped me, I learned about bytes.