0

我想知道是否可以使用 HttpWebRequest 填充 MVC 模型?我正在尝试构建一个 MVC 4 应用程序,我从大学课程列表页面获取数据并在我的视图中以几种不同的方式对其进行按摩。我在周围看到的示例都采用了响应流并返回了一个字符串,或者没有为 MVC 格式化(使用 console.write)。另外,据我了解,返回的数据不是 JSON 或 XML 格式。到目前为止,这是我的控制器...

public ActionResult Index()
{
    string postData = "semester=20143Fall+2013+++++++++++++++++++++++++++++++&courseid=&subject=IT++INFORMATION+TECHNOLOGY&college=&campus=1%2C2%2C3%2C4%2C5%2C6%2C7%2C9%2CA%2CB%2CC%2CI%2CL%2CM%2CN%2CP%2CQ%2CR%2CS%2CT%2CW%2CU%2CV%2CX%2CZ&courselevel=&coursenum=&startTime=0600&endTime=2359&days=ALL&All=All+Sections";
    byte[] dataArray = Encoding.UTF8.GetBytes (postData);
    HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://www3.mnsu.edu/courses/selectform.asp");
    myRequest.Method = "POST";
    myRequest.ContentType = "application/x-www-form-urlencoded";
    myRequest.ContentLength = dataArray.Length;
    using (WebResponse response = myRequest.GetResponse())
    {
        using (var reader = new StreamReader(response.GetResponseStream()))
        {
            //insert into a model? Maybe?
        }
    }
    return View();
}

如果 HttbWebRequest 不能使用,有没有办法可行?还是我完全走错了方向?

4

1 回答 1

1

您可以使用 HttpWebRequest 和 WebResponse 从大学课程的网站获取流。然后使用HtmlAgilityPack解析流中的废料并将所需的值插入模型

于 2013-04-02T17:02:17.020 回答