.NET HTTP POST方法 - Cookies AirFrames.org

时间:2013-04-05 23:38:14

标签: c# post cookies httpwebrequest

尝试使用C#将HttpWebRequest POST到airframes.org中以获取飞机信息。这是我用于许多其他POST请求的代码,没有任何问题(与其他网址一起使用),但它/我无法使用ICAO24编号(A64294)加载airframes.org页面。

        var cookies = new CookieContainer();
        ServicePointManager.Expect100Continue = false;

        var request = (HttpWebRequest)WebRequest.Create("http://www.airframes.org/");
        request.CookieContainer = cookies;
        request.Method = "POST";
        request.KeepAlive = true;

        request.ContentType = "application/x-www-form-urlencoded";
        using (var requestStream = request.GetRequestStream())
        using (var writer = new StreamWriter(requestStream))
        {
            writer.Write("reg=&selcal=&icao24=A64294&submit=submit");
        }

        using (var responseStream = request.GetResponse().GetResponseStream())
        using (var reader = new StreamReader(responseStream))
        {
            var result = reader.ReadToEnd();
            Console.WriteLine(result);

        }

1 个答案:

答案 0 :(得分:0)

请注意,该网站有no-bots policy,这就是您的请求无效的原因。

话虽如此,如果您仍然希望请求该页面,添加用户代理字符串(因此您的请求看起来像是来自浏览器)可以解决问题:

request.UserAgent = "Mozilla/6.0 (Windows NT 6.2; WOW64; rv:16.0.1) Gecko/20121011 Firefox/16.0.1";

尊重网站政策通常是个好主意。上述代码仅用于教育目的。