J
Joey Wu
Dear All.
I always got "WebException" or "NullReferenceException" errors when I try to
use internet functions.
In this case , I can get the file size and type.
Please help thanks!
======================================
Public Function picDownload(ByVal txUrl As String) As String
' Create and start an asynchronous web request
Dim req As HttpWebRequest
Dim u As New Uri(txUrl)
req = HttpWebRequest.Create(u.ToString)
req.Method = "GET"
Cursor.Current = Cursors.WaitCursor
Dim ar As IAsyncResult = req.BeginGetResponse(AddressOf
OnResponseReceived, req)
'MessageBox.Show(txUrl, "txUrl")
' Indicate that we are performing a lengthy operation
picDownload = "ok"
End Function
Private Sub OnResponseReceived(ByVal ar As IAsyncResult)
' Restore the cursor
'Cursor.Current = Cursors.Default
Me.Invoke(New EventHandler(AddressOf RestoreCursor))
' Get the web response. If connection to the web server has failed,
this is where we will get the exception
Dim rsp As WebResponse
Try
rsp = CType(ar.AsyncState, HttpWebRequest).EndGetResponse(ar)
Catch ex As WebException
MessageBox.Show(ex.ToString(), "Exception")
Exit Sub
End Try
MessageBox.Show(String.Format("Received responce. Type: {0}, Length:
{1}", rsp.ContentType, rsp.ContentLength))
Dim st As Stream = rsp.GetResponseStream()
Dim cbRead As Int32 = 0
Dim FileName As String = "temp.jpg" ' default file name in case one
was not supplied
Try
' Extract file name from Content-Disposition header
Dim hdrDisp As String = rsp.Headers.Item("Content-disposition")
Dim m As Match
For Each part As String In hdrDisp.Split(";")
m = Regex.Match(part, "filename=(.+)")
If m.Success Then
FileName = m.Groups(1).Value
Exit For
End If
Next
Dim wrt As FileStream = New FileStream("\disk\" & FileName,
FileMode.Create)
' Keep reading from the network stream until it is empty
Dim data(1024) As Byte
Do
cbRead = st.Read(data, 0, 1024)
If cbRead = 0 Then Exit Do
wrt.Write(data, 0, cbRead)
Loop
' Close the file. We are done
wrt.Close()
MessageBox.Show("File saved")
Dim MyImage As Bitmap
MyImage = New Bitmap("\disk\" & FileName)
Me.picLook.Image = CType(MyImage, Image)
Catch aax As NullReferenceException
MessageBox.Show(aax.ToString, "Exception")
End Try
End Sub
I always got "WebException" or "NullReferenceException" errors when I try to
use internet functions.
In this case , I can get the file size and type.
Please help thanks!
======================================
Public Function picDownload(ByVal txUrl As String) As String
' Create and start an asynchronous web request
Dim req As HttpWebRequest
Dim u As New Uri(txUrl)
req = HttpWebRequest.Create(u.ToString)
req.Method = "GET"
Cursor.Current = Cursors.WaitCursor
Dim ar As IAsyncResult = req.BeginGetResponse(AddressOf
OnResponseReceived, req)
'MessageBox.Show(txUrl, "txUrl")
' Indicate that we are performing a lengthy operation
picDownload = "ok"
End Function
Private Sub OnResponseReceived(ByVal ar As IAsyncResult)
' Restore the cursor
'Cursor.Current = Cursors.Default
Me.Invoke(New EventHandler(AddressOf RestoreCursor))
' Get the web response. If connection to the web server has failed,
this is where we will get the exception
Dim rsp As WebResponse
Try
rsp = CType(ar.AsyncState, HttpWebRequest).EndGetResponse(ar)
Catch ex As WebException
MessageBox.Show(ex.ToString(), "Exception")
Exit Sub
End Try
MessageBox.Show(String.Format("Received responce. Type: {0}, Length:
{1}", rsp.ContentType, rsp.ContentLength))
Dim st As Stream = rsp.GetResponseStream()
Dim cbRead As Int32 = 0
Dim FileName As String = "temp.jpg" ' default file name in case one
was not supplied
Try
' Extract file name from Content-Disposition header
Dim hdrDisp As String = rsp.Headers.Item("Content-disposition")
Dim m As Match
For Each part As String In hdrDisp.Split(";")
m = Regex.Match(part, "filename=(.+)")
If m.Success Then
FileName = m.Groups(1).Value
Exit For
End If
Next
Dim wrt As FileStream = New FileStream("\disk\" & FileName,
FileMode.Create)
' Keep reading from the network stream until it is empty
Dim data(1024) As Byte
Do
cbRead = st.Read(data, 0, 1024)
If cbRead = 0 Then Exit Do
wrt.Write(data, 0, cbRead)
Loop
' Close the file. We are done
wrt.Close()
MessageBox.Show("File saved")
Dim MyImage As Bitmap
MyImage = New Bitmap("\disk\" & FileName)
Me.picLook.Image = CType(MyImage, Image)
Catch aax As NullReferenceException
MessageBox.Show(aax.ToString, "Exception")
End Try
End Sub