VB6中 纯api GET 一张图片
各位,我是一个初学者.
由于个人喜好,想用 纯 winsock api 去下载文件 (非控件)
用了一个模块/类模块. 对于普通的 String 类( .html ) 程序可以正确得到 完整文本文件.
但若GET 一个图片的时候. 返回的 数组里只包含 HTTP头. 未包含 HTTP主体(图片数据)
用了抓包软件可以得知已经成功接收. 何解 ?
如果能给出例子就最好了.
[解决办法]
可能在网页中的图片只是一个链接导致的,图片文件应该jpg等结尾,不是html
[解决办法]
我刚才试了一下是可以的,可能你的HTTP协议的GET请求写错了,或者数据没有接收完整,比如我的测试代码:
Option Explicit
Private Declare Function SafeArrayGetDim Lib "oleaut32.dll" (ByRef saArray() As Any) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Sub Form_Load()
Dim objSock As Winsocket
Dim strCommand As String
'实例化对象
Set objSock = New Winsocket
'连接HTTP服务器
Debug.Print objSock.Connect("avatar.csdn.net", 80)
'发送GET命令请求
strCommand = "GET /3/5/1/1_lyserver.jpg HTTP/1.1" & vbCrLf
strCommand = strCommand & "Accept:image/gif,image/x-xbitmap,image/jpeg,image/pjpeg,*/*" & vbCrLf
strCommand = strCommand & "Accept-Language:zh-cn" & vbCrLf
strCommand = strCommand & "Accept-Encoding:gzip,deflate" & vbCrLf
strCommand = strCommand & "Host:avatar.csdn.net" & vbCrLf
strCommand = strCommand & "Connection:Keep-Alive" & vbCrLf & vbCrLf
Debug.Print objSock.SendData(strCommand)
'获得GET命令返回的结果(注意,收到的数据包括两部分)
Dim bytBuffer() As Byte, bytResult() As Byte
Dim nBufferLen As Long, nResultLen As Long
Do
Erase bytBuffer
objSock.GetData bytBuffer
If SafeArrayGetDim(bytBuffer) = 0 Then Exit Do
nBufferLen = UBound(bytBuffer) + 1
ReDim Preserve bytResult(nResultLen + nBufferLen - 1)
CopyMemory bytResult(nResultLen), bytBuffer(0), nBufferLen
nResultLen = nResultLen + nBufferLen
Loop
Debug.Print StrConv(bytResult, vbUnicode)
'断开连接
objSock.Disconnect
'销毁对象
Set objSock = Nothing
End Sub