如何输入登录详细信息并获取登录页面后面的数据

时间:2017-10-05 12:20:28

标签: c# cookies webclient html-agility-pack

所以我正在寻找一些修复工具来让我的代码工作但我找不到任何有用的东西。我必须登录才能从其他页面获取数据。

这是我的代码:

string pageSource;
string formUrl = "http://aldoc.tc/Sys/Login.srf?Page=DefaultLogin&Cult=en";
string formParams = string.Format("catalogus={0}&gebruikersnaam={1}&wachtwoord={2}", "blabla", "blabla", "blabla");
string cookieheader;

CookieContainer cookies = new CookieContainer();
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(formUrl);
req.CookieContainer = cookies;
req.AllowAutoRedirect = false;
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(formParams);
req.ContentLength = bytes.Length;
using (Stream os = req.GetRequestStream())
{
    os.Write(bytes, 0, bytes.Length);
}
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
cookieheader = resp.Headers["Set-Cookie"];

using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
{
    pageSource = sr.ReadToEnd();
}

string getUrl = "http://aldoc.tc/Sys/AldocSys.srf";
HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(getUrl);
// [HK] use the same cookiecontainer as on the first request - correct
getRequest.CookieContainer = cookies;
getRequest.Method = "GET";
getRequest.AllowAutoRedirect = false;
HttpWebResponse getResponse = (HttpWebResponse)getRequest.GetResponse();
// [HK] no need to add cookies, they should be there already
//cookies.Add(getResponse.Cookies);
using (StreamReader sr = new StreamReader(getResponse.GetResponseStream()))
{
    pageSource = sr.ReadToEnd();
}
// [HK] no need to add cookies, they should be there already
// cookies.Add(getResponse.Cookies);



var httpClient = new HttpClient();
var html = await httpClient.GetStringAsync(getUrl);

var htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(html);

try
{
    foreach (var cell4 in htmlDocument.DocumentNode.SelectNodes("//h3"))
    {


        string Test = cell4.InnerText;
        Console.WriteLine(Test);

    }
}
catch (Exception e)
{
    Console.WriteLine(e.Message);
}

我测试了代码,我发现cookieheader = resp.Headers["Set-Cookie"];为空。我认为这就是问题。

有人可以帮我吗?

编辑:

我在httpclient中添加了一个cookie,但它仍然无效。 我添加了这样的cookie:

Uri uri = new Uri("http://aldoc.tc/Sys/AldocSys.srf");
HttpClientHandler handler = new HttpClientHandler();
handler.CookieContainer = new CookieContainer();

handler.CookieContainer.Add(uri, new Cookie("name", "value")); // Adding a Cookie
HttpClient client = new HttpClient(handler);
HttpResponseMessage response = await client.GetAsync(uri);
CookieCollection collection2 = handler.CookieContainer.GetCookies(uri); // Retrieving a Cookie

编辑2:

我修复它以便我拥有我想要的页面的html,但现在我看不到框架内的东西。

        string pageSource;
        //inlogpagina
        string formUrl = "http://aldoc.tc/Sys/Login.srf?Page=DefaultLogin&Cult=en";
        string formParams = string.Format("login={0}&password={1}", "blabla", "blabla");

        CookieContainer cookies = new CookieContainer();
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(formUrl);
        req.CookieContainer = cookies;
        req.AllowAutoRedirect = false;
        req.ContentType = "application/x-www-form-urlencoded";
        req.Method = "POST";
        byte[] bytes = System.Text.Encoding.ASCII.GetBytes(formParams);
        req.ContentLength = bytes.Length;
        using (Stream os = req.GetRequestStream())
        {
            os.Write(bytes, 0, bytes.Length);
        }
        HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

        CookieCollection collection = cookies.GetCookies(req.RequestUri);

        using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
        {
            pageSource = sr.ReadToEnd();
        }



        //Haalt de SysSession value op
        HttpClientHandler handler = new HttpClientHandler();
        handler.CookieContainer = new CookieContainer();

        HttpClient client = new HttpClient(handler);

        var html = pageSource;

        var htmlDocument = new HtmlDocument();
        htmlDocument.LoadHtml(html);

        var cell4 = htmlDocument.DocumentNode.SelectSingleNode("//script");

        string Test = cell4.InnerText;
        //SysSession value
        Test = Test.Substring(27, 16);




        //Sessiepagina
        string getUrl = "http://aldoc.tc/Sys/Info.srf?SysSession=" + Test + "";
        string formParams2 = string.Format("SysSession={0}", Test);
        HttpWebRequest req2 = (HttpWebRequest)WebRequest.Create(getUrl);

        req2.CookieContainer = cookies;
        req2.AllowAutoRedirect = false;
        req2.ContentType = "application/x-www-form-urlencoded";
        req2.Method = "POST";
        byte[] bytes2 = System.Text.Encoding.ASCII.GetBytes(formParams2);
        req2.ContentLength = bytes2.Length;
        using (Stream os = req2.GetRequestStream())
        {
            os.Write(bytes2, 0, bytes2.Length);
        }
        HttpWebResponse resp2 = (HttpWebResponse)req2.GetResponse();

        CookieCollection collection2 = cookies.GetCookies(req2.RequestUri);

        using (StreamReader sr = new StreamReader(resp2.GetResponseStream()))
        {
            pageSource = sr.ReadToEnd();
        }




        //Mainpagina
        string getUrl2 = "http://aldoc.tc/Sys/AldocSys.srf/";
        string formParams3 = string.Format("SysSession={0}&hostname={1}", Test, "aldoc.tc");
        HttpWebRequest req3 = (HttpWebRequest)WebRequest.Create(getUrl2);

        req3.CookieContainer = cookies;
        req3.AllowAutoRedirect = false;
        req3.ContentType = "application/x-www-form-urlencoded";
        req3.Method = "POST";
        byte[] bytes3 = System.Text.Encoding.ASCII.GetBytes(formParams3);
        req3.ContentLength = bytes3.Length;
        using (Stream os = req3.GetRequestStream())
        {
            os.Write(bytes3, 0, bytes3.Length);
        }
        HttpWebResponse resp3 = (HttpWebResponse)req3.GetResponse();

        CookieCollection collection3 = cookies.GetCookies(req3.RequestUri);

        using (StreamReader sr = new StreamReader(resp3.GetResponseStream()))
        {
            pageSource = sr.ReadToEnd();
        }

        HttpClientHandler handler2 = new HttpClientHandler();
        handler2.CookieContainer = new CookieContainer();

        HttpClient client2 = new HttpClient(handler2);

        var html2 = pageSource;

        var htmlDocument2 = new HtmlDocument();
        htmlDocument2.LoadHtml(html2);

        var cell5 = htmlDocument.DocumentNode.SelectSingleNode("//td");
        Console.WriteLine(cell5.InnerText);

这是pageSource的输出:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN"                   
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>AldocSys</title>
<meta name="vs_targetSchema" content="http://schemas.microsoft.com                    /intellisense/ie5" />
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
<link rel="icon" href="/favicon.ico" type="image/x-icon" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png"  />
<link rel="icon" type="image/png" href="/favicon-32x32.png" sizes="32x32" />
<link rel="icon" type="image/png" href="/favicon-16x16.png" sizes="16x16" />
<link rel="manifest" href="/manifest.json" />
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5" />
<meta name="theme-color" content="#ffffff" />
<script type="text/javascript">
</script>
<script src='/AldocJs/Aldoc.js?Time=1501061949'></script>
<script src='/AldocJs/Car.js?Time=1490343019'></script>
<script src='Xlate.srf?Cult=nl&Page=AldocSys&Time=1504093438'></script>
<script src='/AldocJs/AldocSys.js?Time=1501065406'></script>
<script src='/AldocJs/Srvr/AldocSys.js?Time=1495005368'></script>
<script src='/AldocJs/AldocSysJN.js?Time=1436857551'></script>
<!-- InvalidHandle (2l) D:\Sys\Js\AldocSysMR.js -->
<!-- InvalidHandle (2l) D:\Sys\Js\AldocSysMR.js -->

<!-- <script src="/MMT/PicModelGet.ashx?V=20160704"></script> -->
<script>
//<![CDATA[
var   Session="gCQ8LN9mxSordc3E",NonPersist=0,Product="AldocSys",Product2="AldocSys  ";
var Login="tc•blabla",Cult="nl",DebNr=81214,Secur=8;
var MenuName="TC-SA 2017",MenuTime=1507560094,Immat=0;
var   CustID="711003",LastName="",Company="KCPerformance",Email="",Email2="",OrderE  ml="";
var KTypMask=65536,NTypMask=131072;
var bExpBox=false,SysUri="/Sys",bAtz=false,TruckUri=" /Truck",PicUri="https://pic2.aldoc.eu/PicData/";
var bCatBoxHandler=false;
var  ShowFlags=19,ShowFilter=1,ShowGnrs=2,ShowVnrs=4,ShowOnrs=8,ByArtGrp=16,HidePr ys0=32,HidePrys1=64,HidePrys2=128;
oCntxt["SysSession"]="gCQ8LN9mxSordc3E";
oCntxt["hostname"]="aldoc.tc";

var ueSession=encodeURIComponent(Session);
oNoteDef.bKent=true;
Last.newsdate=forceInt(1457533934);
Last.newsdelay=forceInt(1476182219);
Last.newsupdate=forceInt(1457533934);
Time=1507705603;Last.tick=Time;

try{ServerScript();}catch(e){;}
var CatCultDebNr="Cat"+Cult+"/c"+(("00000"+DebNr).slice(-5))+"/";
fnCntxt();window.setTimeout("fnFill();",200);
var LoginCat=(Login.split("•"))[0];
var LoginUsr=(Login.split("•"))[1];
//]]>
</script>
</head>
<frameset id="lings" cols="185,*,0,0,0,0" frameborder="0"  framespacing="0" border="0">
<frame name="SuperMenu" id="SuperMenu" scrolling="auto" noresize="noresize" src="S.srf?Page=Blank" />
<frameset id="regs" rows="*,0" frameborder="0" framespacing="0" border="0">
<frame name="Content" id="Content" scrolling="auto" noresize="noresize"  src="S.srf?Page=Blank" />

                           

0 个答案:

没有答案