Results 1 to 29 of 29

Thread: [RESOLVED] [Problem] Download Image From Url Using Winsock

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2013
    Posts
    33

    Resolved [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
    So the thing is how can I save the DataArrival As an Image?

    Thanks in Advance.
    Last edited by aliforever; Jul 28th, 2014 at 01:12 AM.

  2. #2
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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.

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2013
    Posts
    33

    Re: [Problem] Download Image From Url Using Winsock

    Quote Originally Posted by jmsrickland View Post
    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".

  4. #4
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    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

  5. #5

    Thread Starter
    Member
    Join Date
    Aug 2013
    Posts
    33

    Re: [Problem] Download Image From Url Using Winsock

    Quote Originally Posted by Nightwalker83 View Post
    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.

  6. #6
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: [Problem] Download Image From Url Using Winsock

    Winsock Making HTTP Post/Get Requests demo by VisualAd might include a download method.
    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

  7. #7
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: [Problem] Download Image From Url Using Winsock

    Quote Originally Posted by aliforever View Post
    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.

  8. #8

    Thread Starter
    Member
    Join Date
    Aug 2013
    Posts
    33

    Re: [Problem] Download Image From Url Using Winsock

    Quote Originally Posted by Nightwalker83 View Post
    Winsock Making HTTP Post/Get Requests demo by VisualAd might include a download method.
    I'm afraid I couldn't find anything about downloading.

    Quote Originally Posted by jmsrickland View Post
    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
    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.

  9. #9
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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
    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.

  10. #10

    Thread Starter
    Member
    Join Date
    Aug 2013
    Posts
    33

    Unhappy Re: [Problem] Download Image From Url Using Winsock

    Quote Originally Posted by jmsrickland View Post
    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.

  11. #11
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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.

  12. #12
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: [Problem] Download Image From Url Using Winsock

    Quote Originally Posted by jmsrickland View Post
    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 View Post
    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.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    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

  13. #13
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: [Problem] Download Image From Url Using Winsock

    Quote Originally Posted by jmsrickland View Post

    This is an invalid URL for Winsock

    http://games5.sepanta.com/forum/uplo...to-thumb-2.jpg
    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
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    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

  14. #14

    Thread Starter
    Member
    Join Date
    Aug 2013
    Posts
    33

    Re: [Problem] Download Image From Url Using Winsock

    Quote Originally Posted by Niya View Post
    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 View Post
    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.

  15. #15
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: [Problem] Download Image From Url Using Winsock

    Quote Originally Posted by Niya View Post
    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.

  16. #16
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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.

  17. #17
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    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:
    1. Private strReceivedData As String
    2.  
    3. Private Sub Form_Load()
    4.  
    5.  Winsock1.Connect "www.games5.sepanta.com", 80
    6.  
    7. End Sub
    8.  
    9. Private Sub Winsock1_Close()
    10.     Debug.Print strReceivedData
    11. End Sub
    12.  
    13. Private Sub Winsock1_Connect()
    14.     Dim strHeader As String
    15.    
    16.     strHeader = "GET /forum/uploads/profile/photo-thumb-2.jpg HTTP/1.1" & vbCrLf & vbCrLf
    17.    
    18.     Winsock1.SendData strHeader
    19. End Sub
    20.  
    21. Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    22.     Dim strn As String
    23.    
    24.     Winsock1.GetData strn, vbString, bytesTotal
    25.     strReceivedData = strReceivedData + strn
    26.  
    27. End Sub
    28.  
    29. 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)
    30.     MsgBox "Winsock error:" + Description
    31. End Sub
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    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

  18. #18

    Thread Starter
    Member
    Join Date
    Aug 2013
    Posts
    33

    Re: [Problem] Download Image From Url Using Winsock

    Quote Originally Posted by Niya View Post
    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:
    1. Private strReceivedData As String
    2.  
    3. Private Sub Form_Load()
    4.  
    5.  Winsock1.Connect "www.games5.sepanta.com", 80
    6.  
    7. End Sub
    8.  
    9. Private Sub Winsock1_Close()
    10.     Debug.Print strReceivedData
    11. End Sub
    12.  
    13. Private Sub Winsock1_Connect()
    14.     Dim strHeader As String
    15.    
    16.     strHeader = "GET /forum/uploads/profile/photo-thumb-2.jpg HTTP/1.1" & vbCrLf & vbCrLf
    17.    
    18.     Winsock1.SendData strHeader
    19. End Sub
    20.  
    21. Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    22.     Dim strn As String
    23.    
    24.     Winsock1.GetData strn, vbString, bytesTotal
    25.     strReceivedData = strReceivedData + strn
    26.  
    27. End Sub
    28.  
    29. 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)
    30.     MsgBox "Winsock error:" + Description
    31. End Sub
    ow, sorry I forgot to edit my post, that website wasn't suitable.
    So I changed my testing Image .
    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.

  19. #19
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: [Problem] Download Image From Url Using Winsock

    Quote Originally Posted by aliforever View Post
    sorry I forgot to edit my post, that website wasn't suitable.
    So I changed my testing Image .
    As I mentioned in the 10th post:
    http://www.vbforums.com/showthread.p...=1#post4729297
    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:
    1. '
    2. Private Sub Form_Load()
    3.  
    4.  Winsock1.Connect "www.vbforums.com", 80
    5.  
    6. End Sub
    7.  
    8.  
    9. Private Sub Winsock1_Connect()
    10.     Dim strHeader As String
    11.    
    12.     strHeader = "GET /icom_includes/footers/img/developer_logo.jpg HTTP/1.1" & vbCrLf & vbCrLf
    13.    
    14.     Winsock1.SendData strHeader
    15. End Sub
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    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

  20. #20

    Thread Starter
    Member
    Join Date
    Aug 2013
    Posts
    33

    Unhappy Re: [Problem] Download Image From Url Using Winsock

    I discovered something weird,
    Name:  sample.jpg
Views: 851
Size:  36.6 KB

    As you can see there are some differences between both files.
    I think the problem has to do with DataArrival.
    Any Ideas?

  21. #21

    Thread Starter
    Member
    Join Date
    Aug 2013
    Posts
    33

    Re: [Problem] Download Image From Url Using Winsock

    Quote Originally Posted by Niya View Post
    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:
    1. '
    2. Private Sub Form_Load()
    3.  
    4.  Winsock1.Connect "www.vbforums.com", 80
    5.  
    6. End Sub
    7.  
    8.  
    9. Private Sub Winsock1_Connect()
    10.     Dim strHeader As String
    11.    
    12.     strHeader = "GET /icom_includes/footers/img/developer_logo.jpg HTTP/1.1" & vbCrLf & vbCrLf
    13.    
    14.     Winsock1.SendData strHeader
    15. 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

  22. #22
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: [Problem] Download Image From Url Using Winsock

    Quote Originally Posted by aliforever View Post
    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.

  23. #23
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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.

  24. #24
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    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
    Name:  sshot.png
Views: 882
Size:  20.3 KB


    We have covered this here many times.
    Attached Files Attached Files

  25. #25
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    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

  26. #26

    Thread Starter
    Member
    Join Date
    Aug 2013
    Posts
    33

    Re: [Problem] Download Image From Url Using Winsock

    Quote Originally Posted by dilettante View Post
    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
    Name:  sshot.png
Views: 882
Size:  20.3 KB


    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.

  27. #27

    Thread Starter
    Member
    Join Date
    Aug 2013
    Posts
    33

    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?

  28. #28
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    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 View Post
    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.

  29. #29

    Thread Starter
    Member
    Join Date
    Aug 2013
    Posts
    33

    Re: [Problem] Download Image From Url Using Winsock

    Quote Originally Posted by dilettante View Post
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width