WPF - Web 请求被截断

发布于 2024-10-06 18:01:10 字数 882 浏览 0 评论 0原文

我正在使用 bing api 请求一些结果。当我运行代码时,响应字符串被截断,因此缺少前 10-50 个字符。当我在浏览器中粘贴完全相同的请求时,它返回结果好吧..

这是我的代码..我做错了什么?

            string AppId = "My APP ID HERE :O ";
        string url = "http://api.search.live.net/xml.aspx?Appid={0}&sources={1}&query={2}";
        string completeUri = String.Format(url, AppId, "web", validateforweb(Artist) + "%20" + validateforweb(Song) + "%20" + "Lyrics");
        HttpWebRequest webRequest = null;
        webRequest = (HttpWebRequest)WebRequest.Create(completeUri);
        HttpWebResponse webResponse = null;
        webResponse = (HttpWebResponse)webRequest.GetResponse();
        XmlReader xmlReader = null;
        Stream s = webResponse.GetResponseStream();
        xmlReader = XmlReader.Create(s);
        StreamReader reader;
        reader = new StreamReader(s);
        string str = reader.ReadToEnd();

I'm using the bing api to request some results.. when I run my code the response string is truncated so that its missing the first 10-50 characters.. when I paste the exact same request in the browser it returns the results just fine..

Here is my code.. what am I doing wrong?

            string AppId = "My APP ID HERE :O ";
        string url = "http://api.search.live.net/xml.aspx?Appid={0}&sources={1}&query={2}";
        string completeUri = String.Format(url, AppId, "web", validateforweb(Artist) + "%20" + validateforweb(Song) + "%20" + "Lyrics");
        HttpWebRequest webRequest = null;
        webRequest = (HttpWebRequest)WebRequest.Create(completeUri);
        HttpWebResponse webResponse = null;
        webResponse = (HttpWebResponse)webRequest.GetResponse();
        XmlReader xmlReader = null;
        Stream s = webResponse.GetResponseStream();
        xmlReader = XmlReader.Create(s);
        StreamReader reader;
        reader = new StreamReader(s);
        string str = reader.ReadToEnd();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜唯美灬不弃 2024-10-13 18:01:10

我怀疑这与您在流上创建 2 个读取器(XmlReaderStreamReader)有关。一旦创建流,XmlReader 就会开始缓冲流中的数据,因此,当 StreamReader 开始从同一个流中读取数据时,它会错过已被读取的数据部分。由 XmlReader 缓冲。

您不能在同一个流上使用 2 个读取器,它们会相互冲突。

I suspect it's related to the fact you're creating 2 readers on the stream (XmlReader and StreamReader). The XmlReader starts buffering data from the stream as soon as you create it, so when the StreamReader starts reading from the same stream, it misses the part of the data that has been buffered by the XmlReader.

You can't use 2 readers on the same stream, they will conflict with each other.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文