【发布时间】:2019-03-27 03:33:57
【问题描述】:
我正在尝试从服务器端向 Facebook 的 Oembed 端点发出 Get 请求。当我在浏览器中访问 URL 时,它可以工作(单击此链接:https://www.facebook.com/plugins/video/oembed.json/?url=https%3A%2F%2Fwww.facebook.com%2Ffacebook%2Fvideos%2F10153231379946729%2F)
但是,当从服务器端进行调用时,响应不是 json 对象。
static void Main(string[] args)
{
string uri2 = "https://www.facebook.com/plugins/video/oembed.json/?url=https%3A%2F%2Fwww.facebook.com%2Ffacebook%2Fvideos%2F10153231379946729%2F";
CreateLinkRequest call2 = new CreateLinkRequest();
call2 = Get<CreateLinkRequest>(uri2);
Console.ReadLine();
}
public static T Get<T>(string uri)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
string json = reader.ReadToEnd();
Object result = new JavaScriptSerializer().Deserialize<T>(json);
return (T)result;
}
}
}
public class CreateLinkRequest
{
public string provider_name { get; set; }
public string author_name { get; set; }
public string width { get; set; }
public string author_url { get; set; }
public string title { get; set; }
public string type { get; set; }
public string version { get; set; }
public string thumbnail_width { get; set; }
public string provider_url { get; set; }
public string thumbnail_height { get; set; }
public string html { get; set; }
public int height { get; set; }
public string thumbnail_url { get; set; }
}
【问题讨论】:
-
当我对该 URL 发出“裸”服务器端请求时,我得到了他们的“升级您的浏览器”页面。尝试将 User-Agent 标头添加到与当前浏览器匹配的请求中。
-
想通了,你在正确的道路上,但我必须在 HttPWebRequest 上设置 UserAgent 和 Refer 属性。我猜 Facebook 想确保你不是机器人。