gpt4 book ai didi

c# - 调用跨域URL时,HttpWebRequest响应始终为text/html

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

我正在使用 HttpWebRequest 从跨域( https://www.facebook.com/plugins/post/oembed.json/?url=posturl )获取 json 结果。我已将 ContentType 和 Accept 设置为 application/json,但我总是收到 text/html 响应。预期结果是 JSON。

string result = string.Empty;
var request = (HttpWebRequest)WebRequest.Create("https://www.facebook.com/plugins/post/oembed.json/?url=xxxxxx");

request.ContentType = "application/json; charset=utf-8";
request.Accept = "application/json";
request.Method = "POST";

var response = (HttpWebResponse)request.GetResponse();
using (var streamReader = new StreamReader(response.GetResponseStream()))
{
result = streamReader.ReadToEnd();
}

最佳答案

我认为您还需要在请求中设置浏览器代理才能获取预期结果,而不是来自 Facebook 的嵌入 HTML。

类似于:

request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";

关于c# - 调用跨域URL时,HttpWebRequest响应始终为text/html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45629442/

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