Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WinRT网页解析/DocumentNode.InnerHtml=";URI“;而不是页面html_C#_.net_Microsoft Metro_Html Agility Pack_Winrt Xaml - Fatal编程技术网

C# WinRT网页解析/DocumentNode.InnerHtml=";URI“;而不是页面html

C# WinRT网页解析/DocumentNode.InnerHtml=";URI“;而不是页面html,c#,.net,microsoft-metro,html-agility-pack,winrt-xaml,C#,.net,Microsoft Metro,Html Agility Pack,Winrt Xaml,我正在尝试创建一个metro应用程序,其中包含我大学的课程表。我使用HAP+Fizzler解析页面并获取数据 计划链接给我@太多自动重定向@错误。 我发现CookieContainer可以帮助我,但不知道如何实现它 CookieContainer cc = new CookieContainer(); request.CookieContainer = cc; 我的代码: public static HttpWebRequest requ

我正在尝试创建一个metro应用程序,其中包含我大学的课程表。我使用HAP+Fizzler解析页面并获取数据

计划链接给我@太多自动重定向@错误。 我发现CookieContainer可以帮助我,但不知道如何实现它

        CookieContainer cc = new CookieContainer();
        request.CookieContainer = cc;
我的代码:

            public static HttpWebRequest request;
    public string Url = "http://cist.kture.kharkov.ua/ias/app/tt/f?p=778:201:9421608126858:::201:P201_FIRST_DATE,P201_LAST_DATE,P201_GROUP,P201_POTOK:01.09.2012,31.01.2013,2423447,0:";
    public SampleDataSource()
    {

        HtmlDocument html = new HtmlDocument();
        request = (HttpWebRequest)WebRequest.Create(Url);
        request.Proxy = null;
        request.UseDefaultCredentials = true;
        CookieContainer cc = new CookieContainer();
        request.CookieContainer = cc;
        html.LoadHtml(request.RequestUri.ToString());
        var page = html.DocumentNode;

String ITEM_CONTENT = null;
foreach (var item in page.QuerySelectorAll(".MainTT")) 
{
    ITEM_CONTENT = item.InnerHtml;
}
      }

使用CookieContainer,我不会得到错误,但是DocumentNode.InnerHtml由于某种原因会得到我的URI值,而不是页面html。

您只需要更改一行即可

替换

 html.LoadHtml(request.RequestUri.ToString());

编辑

首先将方法标记为
async

request.CookieContainer = cc;
var resp = await request.GetResponseAsync();
html.LoadHtml(new StreamReader(resp.GetResponseStream()).ReadToEnd());

你只需要换一行

替换

 html.LoadHtml(request.RequestUri.ToString());

编辑

首先将方法标记为
async

request.CookieContainer = cc;
var resp = await request.GetResponseAsync();
html.LoadHtml(new StreamReader(resp.GetResponseStream()).ReadToEnd());

如果要下载网页代码,请尝试使用此方法(通过使用):

公共异步任务下载HTMLCODE(字符串url)
{
HttpClientHandler handler=newHttpClientHandler{UseDefaultCredentials=true,AllowAutoRedirect=true};
HttpClient=新的HttpClient(处理程序);
HttpResponseMessage response=wait client.GetAsync(url);
response.EnsureSuccessStatusCode();
string responseBody=wait response.Content.ReadAsStringAsync();
返回响应体;
}

如果要下载网页代码,请尝试使用此方法(通过使用):

公共异步任务下载HTMLCODE(字符串url)
{
HttpClientHandler handler=newHttpClientHandler{UseDefaultCredentials=true,AllowAutoRedirect=true};
HttpClient=新的HttpClient(处理程序);
HttpResponseMessage response=wait client.GetAsync(url);
response.EnsureSuccessStatusCode();
string responseBody=wait response.Content.ReadAsStringAsync();
返回响应体;
}

如果要解析下载的HTML代码,可以使用Regex或LINQ。我有一些使用LINQ解析html代码的例子,但在您应该使用库将代码加载到HtmlDocument之前。然后可以通过这种方式加载:
html.LoadHtml(temphtml)
执行此操作时,您可以解析HtmlDocument:

//This is for img links parse-example:
IEnumerable<HtmlNode> imghrefNodes = html.DocumentNode.Descendants().Where(n => n.Name == "img");
foreach (HtmlNode img in imghrefNodes)
{
   HtmlAttribute att = img.Attributes["src"];
   //in att.Value you can find your img url
   //Here you can do everything what you want with all img links by editing att.Value
}
//这是img链接解析示例:
IEnumerable imghrefNodes=html.DocumentNode.subjects()。其中(n=>n.Name==“img”);
foreach(imghrefNodes中的HtmlNode img)
{
HtmlAttribute att=img.Attributes[“src”];
//在att.Value中,您可以找到您的img url
//在这里,您可以通过编辑att.Value来完成所有img链接的所有操作
}

如果要解析下载的HTML代码,可以使用Regex或LINQ。我有一些使用LINQ解析html代码的例子,但在您应该使用库将代码加载到HtmlDocument之前。然后可以通过这种方式加载:
html.LoadHtml(temphtml)
执行此操作时,您可以解析HtmlDocument:

//This is for img links parse-example:
IEnumerable<HtmlNode> imghrefNodes = html.DocumentNode.Descendants().Where(n => n.Name == "img");
foreach (HtmlNode img in imghrefNodes)
{
   HtmlAttribute att = img.Attributes["src"];
   //in att.Value you can find your img url
   //Here you can do everything what you want with all img links by editing att.Value
}
//这是img链接解析示例:
IEnumerable imghrefNodes=html.DocumentNode.subjects()。其中(n=>n.Name==“img”);
foreach(imghrefNodes中的HtmlNode img)
{
HtmlAttribute att=img.Attributes[“src”];
//在att.Value中,您可以找到您的img url
//在这里,您可以通过编辑att.Value来完成所有img链接的所有操作
}

检查,也许你有一些
GetResponseAsync
等。我现在无法在Metro应用程序中测试它。我在Win7和works中测试了上述代码。这里的关键是你必须得到响应流并重新处理它,我来处理它。但是GetResponseAsync没有GetResponseStream()或类似的东西。@MaksMartynov
Async
方法返回
Task
s。我想你看到的是它的方法。查看编辑。检查,可能您有一些
GetResponseAsync
等。我现在无法在Metro应用程序中测试它。我在Win7和works中测试了上述代码。这里的关键是你必须得到响应流并重新处理它,我来处理它。但是GetResponseAsync没有GetResponseStream()或类似的东西。@MaksMartynov
Async
方法返回
Task
s。我想你看到的是它的方法。请参见编辑。