gpt4 book ai didi

c# - 在C#中从Youtube读取ajax响应

转载 作者:行者123 更新时间:2023-12-03 06:33:06 25 4
gpt4 key购买 nike

我正在尝试在YouTube上发布评论并获取xml响应(这样我就可以获取评论ID,或者如果我需要输入验证码或其他内容),但是我只能发布评论。由于某些原因,我无法使用response.GetResponseStream()读取xml响应。当我尝试将响应输出到控制台时,我什么也没得到。而且,我已经嗅探了我的程序使用Wireshark发送和接收的请求和响应,并且我可以看到xml位于响应中。

这是我用来读取响应的代码:

using (HttpWebResponse response = MakeRequest(request, cookies, post))
{
using (var reader = new System.IO.StreamReader(response.GetResponseStream(), UTF8Encoding.UTF8))
{
string xml = reader.ReadToEnd();
Console.WriteLine(xml);
}
}

和MakeRequest函数
private static HttpWebResponse MakeRequest(HttpWebRequest request, CookieContainer SessionCookieContainer, Dictionary<string, string> parameters = null)
{
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.52 Safari/536.5Accept: */*";
request.Accept = "text/html,text/xml,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.CookieContainer = SessionCookieContainer;
request.AllowAutoRedirect = false;
request.KeepAlive = true;

if (proxy != "") request.Proxy = myproxy;

if (parameters != null) request.Method = "POST";

request.ContentType = "application/x-www-form-urlencoded";
string postData = string.Empty;

if (parameters != null)
{
postData = getPostData(parameters);


byte[] postBuffer = UTF8Encoding.UTF8.GetBytes(postData);
using (Stream postStream = request.GetRequestStream())
{
postStream.Write(postBuffer, 0, postBuffer.Length);
}
}

HttpWebResponse response = request.GetResponse() as HttpWebResponse;
SessionCookieContainer.Add(response.Cookies);

while (response.StatusCode == HttpStatusCode.Found)
{
response.Close();
request = GetNewRequest(response.Headers["Location"], SessionCookieContainer);
response = (HttpWebResponse)request.GetResponse();
SessionCookieContainer.Add(response.Cookies);
}

return response;
}

关于为何无法解决问题以及如何解决此问题的任何想法?

最佳答案

我认为切换到HttpClient可以解决问题。

关于c# - 在C#中从Youtube读取ajax响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18273371/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com