簡體   English   中英

調用跨域URL時,HttpWebRequest響應始終為text / html

[英]HttpWebRequest response is always text/html when calling cross domain URL

我正在使用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";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM