HttpWebRequest可以用来填充模型吗?

本文关键字:模型 填充 HttpWebRequest | 更新日期: 2023-09-27 18:07:10

我想知道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不能使用,是否有一种方法可以工作?还是我完全走错了方向?

HttpWebRequest可以用来填充模型吗?

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