WPF - Web 请求被截断
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我怀疑这与您在流上创建 2 个读取器(
XmlReader
和StreamReader
)有关。一旦创建流,XmlReader
就会开始缓冲流中的数据,因此,当StreamReader
开始从同一个流中读取数据时,它会错过已被读取的数据部分。由XmlReader
缓冲。您不能在同一个流上使用 2 个读取器,它们会相互冲突。
I suspect it's related to the fact you're creating 2 readers on the stream (
XmlReader
andStreamReader
). TheXmlReader
starts buffering data from the stream as soon as you create it, so when theStreamReader
starts reading from the same stream, it misses the part of the data that has been buffered by theXmlReader
.You can't use 2 readers on the same stream, they will conflict with each other.