如何从流中读取字符串

时间:2013-02-24 14:13:58

标签: string stream

        Dim adult = webClient.DownloadString("http://googleads.g.doubleclick.net/apps/domainpark/domainpark.cgi?callback=_google_json_callback&output=js&client=ca-dp-godaddy2_xml&domain_name=" + CurrentBlog.Domain)
        'ias.NavigateTillComplete("https://api.bodis.com/domainclassification?domain=" + CurrentBlog.Domain)
        Dim result = ""
        Dim url = "http://api.bodis.com/domainclassification?domain=" + CurrentBlog.Domain
        Dim webRequest = DirectCast(System.Net.HttpWebRequest.Create(url), System.Net.HttpWebRequest)
        Dim response = DirectCast(webRequest.GetResponse(), HttpWebResponse)
        Dim responseStream = response.GetResponseStream
        Dim responseString = responseStream.readAll? 'MethodAccessException doesn't exist


        Dim responseString = responseStream.readAll? 'MethodAccessException doesn't exist

我应该如何替换Dim responseString = responseStream.readAll

实际命令是什么?

1 个答案:

答案 0 :(得分:1)

您可以将响应流传递给StreamReader实例,然后使用ReadToEnd方法:

Dim adult = webClient.DownloadString("http://googleads.g.doubleclick.net/apps/domainpark/domainpark.cgi?callback=_google_json_callback&output=js&client=ca-dp-godaddy2_xml&domain_name=" + CurrentBlog.Domain)
'ias.NavigateTillComplete("https://api.bodis.com/domainclassification?domain=" + CurrentBlog.Domain)
Dim result = ""
Dim url = "http://api.bodis.com/domainclassification?domain=" + CurrentBlog.Domain
Dim webRequest = DirectCast(System.Net.HttpWebRequest.Create(url), System.Net.HttpWebRequest)
Using Dim response = DirectCast(webRequest.GetResponse(), HttpWebResponse)
    Using Dim responseStream = response.GetResponseStream
        Using Dim responseReader = new StreamReader(responseStream)
            Dim responseString = responseReader.ReadToEnd()
        End Using
    End Using
End Using

但是,当您可以直接使用WebClient时,为什么还要使用HttpWebRequest:

Dim adult = webClient.DownloadString("http://googleads.g.doubleclick.net/apps/domainpark/domainpark.cgi?callback=_google_json_callback&output=js&client=ca-dp-godaddy2_xml&domain_name=" + CurrentBlog.Domain)
'ias.NavigateTillComplete("https://api.bodis.com/domainclassification?domain=" + CurrentBlog.Domain)
Dim result = ""
Dim url = "http://api.bodis.com/domainclassification?domain=" + CurrentBlog.Domain
Dim responseString = webClient.DownloadString(url)