请教如何下载得到某个图片呢?
图片地址:
http://photo.pchome.com.tw/s13/c/h/chencan0814/book301/p130133146978.jpg
上面图片直接用浏览器打开是无法显示的,会提示“您所查询的网页不存在”,可是如果先打开网址
http://photo.pchome.com.tw/chencan0814/130133146978,然后再去打开
http://photo.pchome.com.tw/s13/c/h/chencan0814/book301/p130133146978.jpg(注意:貌似要将图片地址复制到浏览器地址栏回车才行的)这样就可以显示图片了。
所以面临的问题是,用urldownload、xmlhttp等方法下载http://photo.pchome.com.tw/s13/c/h/chencan0814/book301/p130133146978.jpg都会提示“您所查询的网页不存在”。
如果用webbrowser的话太慢了,而且还不稳定。对于这样的情况,各位高手有什么好办法根据图片url直接下载得到图片文件吗?
[解决办法]
'窗体中一个Winsock1,一个Command1Option ExplicitDim Buff() As ByteDim IsData As Boolean '是否是数据Private Sub Command1_Click() Winsock1.Connect "photo.pchome.com.tw", 80End SubPrivate Sub Winsock1_Connect() Dim HTTPStr As String HTTPStr = "GET http://photo.pchome.com.tw/s13/c/h/chencan0814/book301/p130133146978.jpg HTTP/1.1" & vbCrLf & _ "Connection: Close" & vbCrLf & _ "Host: photo.pchome.com.tw" & vbCrLf & _ "Accept: */*" & vbCrLf & _ "Pragma: no -cache" & vbCrLf & _ "cache -Control: no -cache" & vbCrLf & _ "Referer: http://photo.pchome.com.tw/chencan0814/130133146978" & vbCrLf & _ "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2)" & _ vbCrLf & vbCrLf Winsock1.SendData HTTPStrEnd SubPrivate Sub Winsock1_DataArrival(ByVal bytesTotal As Long) 'HTTP/1.1 200 OK 'Date: Tue, 10 May 2011 13:15:45 GMT 'Server: Apache 'Last-Modified: Mon, 28 Mar 2011 16:57:49 GMT 'ETag: "10fe2916-1b265-49f8dd800c042" 'Accept -Ranges: bytes 'Content-Length: 111205 'Connection: Close 'Content-Type: image/jpeg Dim tmpByte() As Byte, I As Long, J As Long, K As Long If bytesTotal > 0 Then Winsock1.GetData tmpByte If IsData = False Then Debug.Print StrConv(tmpByte(), vbUnicode) For I = 0 To UBound(tmpByte) - 3 If tmpByte(I) = 13 And tmpByte(I + 1) = 10 And tmpByte(I + 2) = 13 And tmpByte(I + 3) = 10 Then '两个回车是HTTP头结束的标志. IsData = True Open "d:\11.jpg" For Binary As #1 '打开文件 If I + 4 < UBound(tmpByte) - 1 Then '如果后面还有数据就写入 I = I + 4 K = -1 For J = I To UBound(tmpByte) K = K + 1 tmpByte(K) = tmpByte(J) Next ReDim Preserve tmpByte(K) Put #1, , tmpByte() End If Exit For End If Next Else Put #1, , tmpByte() End If End IfEnd SubPrivate Sub Winsock1_Close() Close #1End Sub
[解决办法]