我是 VB 新手...我正在使用以下代码发出 HTTP 请求并将响应缓冲到字符串中:
Try
myHttpWebRequest = CType(WebRequest.Create(strUrl), HttpWebRequest)
myHttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
receiveStream = myHttpWebResponse.GetResponseStream()
encode = System.Text.Encoding.GetEncoding("utf-8")
sr = New StreamReader(receiveStream, encode)
Do Until sr.Peek = -1
strLine = String.Concat(strLine, sr.ReadLine)
arrBuff.Add(strLine)
Loop
Catch ex As System.Net.WebException
MsgBox(ex.Message)
Finally
myHttpWebResponse.Close()
End Try
sr.Close()
这工作正常,但错误处理不好,例如,如果请求触发 500 响应,则 VB 代码遇到未处理的异常。关于如何使这段代码更好的任何想法?