我正在尝试从网页中检索数据。在网页上,只有句子。检索句子的正确方法是什么?我没试过就试过这个
Dim webClient As New System.Net.WebClient
Dim result As String = webClient.DownloadString("http://example.org")
MsgBox(result)
答案 0 :(得分:1)
好的,我认为你没有从HTTP调用中获得结果。这就是我的所作所为 - 对不起它的C#,你必须翻译,但这可能接近你的需要:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream receiveStream = response.GetResponseStream();
StreamReader rdr = new StreamReader(receiveStream, Encoding.UTF8);
try
{
//Check that page downloaded OK
if (response.StatusCode == HttpStatusCode.OK)
{
//Do something
}
}
catch (Exception xx)
{
//handle error
}
finally
{
response.Close();
}
答案 1 :(得分:1)
我曾经这样做过:
Imports System.Net
Imports System.IO
Imports System.IO.Compression
和
Dim urlUri As New System.Uri("http://www.example.org/")
Dim strResult as string = String.Empty
Dim webHTTP As HttpWebRequest
Try
webHTTP = WebRequest.Create(urlUri)
webHTTP.KeepAlive = False
Catch ex As Exception
Throw New Exception("An error occured while trying to create the request:" & vbCrLf & ex.Message)
Exit Sub
End Try
Dim WebResponse As HttpWebResponse
Try
WebResponse = webHTTP.GetResponse()
Catch ex As Exception
Throw New Exception("An error occured while retrieving the response:" & vbCrLf & ex.Message)
Exit Sub
End Try
Dim responseStream As Stream
Try
responseStream = WebResponse.GetResponseStream()
Catch ex As Exception
Throw New Exception("An error occured while retrieving the response stream:" & vbCrLf & ex.Message)
Exit Sub
End Try
If (WebResponse.ContentEncoding.ToLower().Contains("gzip")) Then
responseStream = New GZipStream(responseStream, CompressionMode.Decompress)
ElseIf (WebResponse.ContentEncoding.ToLower().Contains("deflate")) Then
responseStream = New DeflateStream(responseStream, CompressionMode.Decompress)
End If
Dim CHUNK_SIZE As Long = 3072
Dim read(CHUNK_SIZE) As [Char]
Dim reader As StreamReader
Try
reader = New StreamReader(responseStream, Encoding.Default)
Catch ex As Exception
Throw New Exception("An error occured while creating the streamreader:" & vbCrLf & ex.Message)
Exit Sub
End Try
Dim count As Integer = reader.Read(read, 0, CHUNK_SIZE)
Do While count > 0
Dim str As New [String](read, 0, count)
strResult &= str
ReDim read(CHUNK_SIZE)
count = reader.Read(read, 0, CHUNK_SIZE)
Loop
reader.Close()
responseStream.Close()
WebResponse.Close()
Messagebox.Show(strResult)