从Google Image获取结果时,在GetResponseStream()中出现问题

时间:2014-11-16 07:03:46

标签: winforms c#-4.0 google-api

string requestUri = string.Format("http://images.google.com/images?q={0}&ndsp={1}&start={2}&filter={3}&safe={4}",query,RESULTS_PER_QUERY.ToString(),(startPosition+i).ToString(), (filterSimilarResults)?"1":"0",safeSearchStr);

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);
string resultPage = string.Empty;
using (HttpWebResponse httpWebResponse = (HttpWebResponse)request.GetResponse())
{
    using (Stream responseStream = httpWebResponse.GetResponseStream())
    {
        using (StreamReader reader = new StreamReader(responseStream))
        {
            resultPage = reader.ReadToEnd();
        }
    }
}

以上是我的代码,我在下面遇到问题。

responseStream.Length as 'responseStream.Length'抛出类型'System.NotSupportedException'的异常

responseStream.Position as 'responseStream.Position'抛出了'System.NotSupportedException'类型的异常

1 个答案:

答案 0 :(得分:0)

尝试使用WebClient.DownloadData方法,这样更简单。如果问题仍然存在,我建议使用Fiddler来检查是否进行了调用(以及返回的内容)。