我有一個WebRequest
在C#,我試圖用來從Instagram檢索數據。 WebRequest拋出The remote server returned an error: (403) Forbidden.
,但cURL命令返回HTML。實際上,我的POST表單數據會更長,並返回JSON。C#WebRequest但不cURL給出錯誤403
C#
String uri = "https://www.instagram.com/query/";
var request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "POST";
string postData = "q=ig_user(1118028333)";
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] byte1 = encoding.GetBytes(postData);
// Set the content type of the data being posted.
request.ContentType = "application/x-www-form-urlencoded";
// Set the content length of the string being posted.
request.ContentLength = byte1.Length;
using (var dataStream = request.GetRequestStream())
{
dataStream.Write(byte1, 0, byte1.Length);
}
try
{
var x = (HttpWebResponse)request.GetResponse();
}
catch (WebException wex)
{
String wMessage = wex.Message;
}
拋出錯誤403
捲曲(在Windows中)
curl "https://www.instagram.com/query/" --data "q=ig_user(1118028333)"
返回HTML。
FireFox的請求體,方法= POST,無頭
q=ig_user(1118028333)
返回HTML
爲什麼會產生的WebRequest拋出錯誤403,但不捲曲或Firefox?我還有什麼可以在C#中獲取數據?