0

我在 Unix 机器上安装了 scrapyd 和 spider,运行时一切正常

curl http://localhost:6800/schedule.json -d project=myproject -d spider=somespider

我可以在 scrapyd API 的 Web 界面上看到作业状态、日志和项目。简而言之,一切都按预期工作。

现在我想通过使用 C# 向 ASP.Net 中的 API 发布 http 以编程方式启动蜘蛛,因为 scrapyd 将成为我的 .NET 项目的一部分,但我得到了

{"status": "error", "message": "'project'"}

我找到了一个示例http://mahmoud.abdel-fattah.net/2012/07/04/super-simple-and-basic-scrapyd-web-interface/comment-page-1/它是一个 Jquery 帖子和这个示例适用于我,但以下一个不适用于我

public void StartCrawler()
        {
            var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://mydomain.com:6800/schedule.json");
            httpWebRequest.ContentType = "application/json; charset=utf-8";
            //httpWebRequest.ContentType = "text/json;; charset=utf-8";
            httpWebRequest.Method = "POST";

            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                string json = "{\"project\":\"projectname\",\"spider\":\"spidername\"}";

                streamWriter.Write(json);
            }

            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var responseText = streamReader.ReadToEnd();
            }
        }

请告诉我我做错了什么

4

1 回答 1

0

我已经解决了

public static string StartCraling(string URI, string Parameters)
        {
            WebRequest req = WebRequest.Create(URI);

            req.ContentType = "application/x-www-form-urlencoded";
            req.Method = "POST";

            byte[] bytes = System.Text.Encoding.ASCII.GetBytes(Parameters);
            req.ContentLength = bytes.Length;

            using (Stream os = req.GetRequestStream())
            {
                os.Write(bytes, 0, bytes.Length); //Push it out there
            }

            using (WebResponse resp = req.GetResponse())
            {
                using (StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream()))
                {
                    return sr.ReadToEnd().Trim();
                }
            }
        }
于 2013-11-07T13:51:21.883 回答