HTMLAgilityPack POST / w HTTPClient

时间:2015-11-02 20:07:13

标签: c# post html-agility-pack

尝试解析由于回复后加载的网页。但它不起作用我想我错过了什么,但我不确定是什么......

HTML CODE

    <input type="submit" name="ctl00$plhBodyArea$btnAccept" value="Accept" id="plhBodyArea_btnAccept">

    <div class="aspNetHidden">

        <input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="9373CF0E">
        <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="feTZ9oqpykfw9OnH.... more characters">
    </div>
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="yawbftCbDoV0i4ULGUh4Lh... shortten">
</div>

所以这是Chrome开发人员的HAR

{
  "log": {
    "version": "1.2",
    "creator": {
      "name": "WebInspector",
      "version": "537.36"
    },
    "pages": [
      {
        "startedDateTime": "2015-11-02T20:00:35.567Z",
        "id": "page_3",
        "title": "http://app.alea.gov/Community/wfSexOffenderSearch.aspx",
        "pageTimings": {
          "onContentLoad": 1149.1310000419617,
          "onLoad": 1187.5710000749677
        }
      }
    ],
    "entries": [
      {
        "startedDateTime": "2015-11-02T20:00:35.567Z",
        "time": 879.9570000264794,
        "request": {
          "method": "POST",
          "url": "http://app.alea.gov/Community/wfSexOffenderSearch.aspx",
          "httpVersion": "HTTP/1.1",
          "headers": [
            {
              "name": "Pragma",
              "value": "no-cache"
            },
            {
              "name": "Origin",
              "value": "http://app.alea.gov"
            },
            {
              "name": "Accept-Encoding",
              "value": "gzip, deflate"
            },
            {
              "name": "Host",
              "value": "app.alea.gov"
            },
            {
              "name": "Accept-Language",
              "value": "en-US,en;q=0.8"
            },
            {
              "name": "Upgrade-Insecure-Requests",
              "value": "1"
            },
            {
              "name": "User-Agent",
              "value": "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36"
            },
            {
              "name": "Content-Type",
              "value": "application/x-www-form-urlencoded"
            },
            {
              "name": "Accept",
              "value": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
            },
            {
              "name": "Cache-Control",
              "value": "no-cache"
            },
            {
              "name": "Referer",
              "value": "http://app.alea.gov/Community/wfSexOffenderSearch.aspx"
            },
            {
              "name": "Connection",
              "value": "keep-alive"
            },
            {
              "name": "Content-Length",
              "value": "5844"
            }
          ],
          "queryString": [],
          "cookies": [],
          "headersSize": 611,
          "bodySize": 5844,
          "postData": {
            "mimeType": "application/x-www-form-urlencoded",
            "text": "__VIEWSTATE=yawbftCbDoV0i4ULGUh4Lh... characters here ....%3D%3D&ctl00%24plhBodyArea%24btnAccept=Accept",
            "params": [
              {
                "name": "__VIEWSTATE",
                "value": "yawbftCbDoV0i4ULGUh4LhJOy4KV.. shorten"
              },
              {
                "name": "__VIEWSTATEGENERATOR",
                "value": "9373CF0E"
              },
              {
                "name": "__EVENTVALIDATION",
                "value": "feTZ9oqpykfw9OnH0%2BzFkSbybPCH9afrkS5eyt4VwnpFvSEk7I9VjiRjNMn%2BCC3gsDInBy2Xk3Vfd33RWvfbdMuB6axwyKoIoqifKWog4FOVTxLnKMe8SWen%2Bhuu2fBrrPNDdVP4y3pixjJPRb1Vqg%3D%3D"
              },
              {
                "name": "ctl00%24plhBodyArea%24btnAccept",
                "value": "Accept"
              }
            ]
          }
        },
        "response": {
          "status": 200,
          "statusText": "OK",
          "httpVersion": "HTTP/1.1",
          "headers": [
            {
              "name": "Date",
              "value": "Mon, 02 Nov 2015 20:01:10 GMT"
            },
            {
              "name": "Cache-Control",
              "value": "private"
            },
            {
              "name": "Server",
              "value": "Microsoft-IIS/8.0"
            },
            {
              "name": "X-AspNet-Version",
              "value": "4.0.30319"
            },
            {
              "name": "X-Powered-By",
              "value": "ASP.NET"
            },
            {
              "name": "Content-Length",
              "value": "25003"
            },
            {
              "name": "Content-Type",
              "value": "text/html; charset=utf-8"
            }
          ],
          "cookies": [],
          "content": {
            "size": 25003,
            "mimeType": "text/html",
            "compression": 0
          },
          "redirectURL": "",
          "headersSize": 222,
          "bodySize": 25003,
          "_transferSize": 25225
        },
        "cache": {},
        "timings": {
          "blocked": 0.465000048279762,
          "dns": -1,
          "connect": -1,
          "send": 0.38799992762506,
          "wait": 813.6800001375382,
          "receive": 65.42399991303637,
          "ssl": -1
        },
        "connection": "404",
        "pageref": "page_3"
      },
      {
        "startedDateTime": "2015-11-02T20:00:36.459Z",
        "time": 105.35899992100894,
        "request": {
          "method": "GET",
          "url": "http://app.alea.gov/Community/App_Themes/pubMetroBlue/Standard.css",
          "httpVersion": "HTTP/1.1",
          "headers": [
            {
              "name": "Pragma",
              "value": "no-cache"
            },
            {
              "name": "Accept-Encoding",
              "value": "gzip, deflate, sdch"
            },
            {
              "name": "Host",
              "value": "app.alea.gov"
            },
            {
              "name": "Accept-Language",
              "value": "en-US,en;q=0.8"
            },
            {
              "name": "User-Agent",
              "value": "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36"
            },
            {
              "name": "Accept",
              "value": "text/css,*/*;q=0.1"
            },
            {
              "name": "Referer",
              "value": "http://app.alea.gov/Community/wfSexOffenderSearch.aspx"
            },
            {
              "name": "Connection",
              "value": "keep-alive"
            },
            {
              "name": "Cache-Control",
              "value": "no-cache"
            }
          ],
          "queryString": [],
          "cookies": [],
          "headersSize": 442,
          "bodySize": 0
        },
        "response": {
          "status": 200,
          "statusText": "OK",
          "httpVersion": "HTTP/1.1",
          "headers": [
            {
              "name": "Date",
              "value": "Mon, 02 Nov 2015 20:01:10 GMT"
            },
            {
              "name": "ETag",
              "value": "\"8cce9ded51ed11:0\""
            },
            {
              "name": "Last-Modified",
              "value": "Sat, 24 Oct 2015 11:48:41 GMT"
            },
            {
              "name": "Server",
              "value": "Microsoft-IIS/8.0"
            },
            {
              "name": "X-Powered-By",
              "value": "ASP.NET"
            },
            {
              "name": "Content-Type",
              "value": "text/css"
            },
            {
              "name": "Accept-Ranges",
              "value": "bytes"
            },
            {
              "name": "Content-Length",
              "value": "1804"
            }
          ],
          "cookies": [],
          "content": {
            "size": 1804,
            "mimeType": "text/css",
            "compression": 0
          },
          "redirectURL": "",
          "headersSize": 246,
          "bodySize": 1804,
          "_transferSize": 2050
        },
        "cache": {},
        "timings": {
          "blocked": 2.3289998061955,
          "dns": -1,
          "connect": -1,
          "send": 6.469000130891799,
          "wait": 79.94799991138281,
          "receive": 16.61300007253884,
          "ssl": -1
        },
        "connection": "404",
        "pageref": "page_3"
      },
      {
        "startedDateTime": "2015-11-02T20:00:36.460Z",
        "time": 237.4349997844547,
        "request": {
          "method": "GET",
          "url": "http://app.alea.gov/Community/WebResource.axd?d=fqV81KWLWhVg-lLAb4IT69DJXeWasNBvYIYrQPNzfPOWvC9XLYWCfpkhdhr8N9xULvv8F_E-W1b-bLJi69Y22i6EGBLr3P2_Mc9o7OFFOiA1&t=635588402815688065",
          "httpVersion": "HTTP/1.1",
          "headers": [
            {
              "name": "Pragma",
              "value": "no-cache"
            },
            {
              "name": "Accept-Encoding",
              "value": "gzip, deflate, sdch"
            },
            {
              "name": "Host",
              "value": "app.alea.gov"
            },
            {
              "name": "Accept-Language",
              "value": "en-US,en;q=0.8"
            },
            {
              "name": "User-Agent",
              "value": "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36"
            },
            {
              "name": "Accept",
              "value": "*/*"
            },
            {
              "name": "Referer",
              "value": "http://app.alea.gov/Community/wfSexOffenderSearch.aspx"
            },
            {
              "name": "Connection",
              "value": "keep-alive"
            },
            {
              "name": "Cache-Control",
              "value": "no-cache"
            }
          ],
          "queryString": [
            {
              "name": "d",
              "value": "fqV81KWLWhVg-lLAb4IT69DJXeWasNBvYIYrQPNzfPOWvC9XLYWCfpkhdhr8N9xULvv8F_E-W1b-bLJi69Y22i6EGBLr3P2_Mc9o7OFFOiA1"
            },
            {
              "name": "t",
              "value": "635588402815688065"
            }
          ],
          "cookies": [],
          "headersSize": 538,
          "bodySize": 0
        },
        "response": {
          "status": 200,
          "statusText": "OK",
          "httpVersion": "HTTP/1.1",
          "headers": [
            {
              "name": "Date",
              "value": "Mon, 02 Nov 2015 20:01:10 GMT"
            },
            {
              "name": "Last-Modified",
              "value": "Fri, 06 Feb 2015 23:24:41 GMT"
            },
            {
              "name": "Server",
              "value": "Microsoft-IIS/8.0"
            },
            {
              "name": "X-AspNet-Version",
              "value": "4.0.30319"
            },
            {
              "name": "X-Powered-By",
              "value": "ASP.NET"
            },
            {
              "name": "Content-Type",
              "value": "application/x-javascript"
            },
            {
              "name": "Cache-Control",
              "value": "public"
            },
            {
              "name": "Content-Length",
              "value": "27735"
            },
            {
              "name": "Expires",
              "value": "Tue, 01 Nov 2016 16:52:25 GMT"
            }
          ],
          "cookies": [],
          "content": {
            "size": 27735,
            "mimeType": "application/x-javascript",
            "compression": 0
          },
          "redirectURL": "",
          "headersSize": 307,
          "bodySize": 27735,
          "_transferSize": 28042
        },
        "cache": {},
        "timings": {
          "blocked": 9.09299985505641,
          "dns": -1,
          "connect": -1,
          "send": 1.3340001460164892,
          "wait": 217.3029999248681,
          "receive": 9.704999858513702,
          "ssl": -1
        },
        "connection": "402",
        "pageref": "page_3"
      },
      {
        "startedDateTime": "2015-11-02T20:00:36.461Z",
        "time": 292.9279999807477,
        "request": {
          "method": "GET",
          "url": "http://app.alea.gov/Community/Images/ALEALogo.png",
          "httpVersion": "HTTP/1.1",
          "headers": [
            {
              "name": "Pragma",
              "value": "no-cache"
            },
            {
              "name": "Accept-Encoding",
              "value": "gzip, deflate, sdch"
            },
            {
              "name": "Host",
              "value": "app.alea.gov"
            },
            {
              "name": "Accept-Language",
              "value": "en-US,en;q=0.8"
            },
            {
              "name": "User-Agent",
              "value": "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36"
            },
            {
              "name": "Accept",
              "value": "image/webp,image/*,*/*;q=0.8"
            },
            {
              "name": "Referer",
              "value": "http://app.alea.gov/Community/wfSexOffenderSearch.aspx"
            },
            {
              "name": "Connection",
              "value": "keep-alive"
            },
            {
              "name": "Cache-Control",
              "value": "no-cache"
            }
          ],
          "queryString": [],
          "cookies": [],
          "headersSize": 435,
          "bodySize": 0
        },
        "response": {
          "status": 200,
          "statusText": "OK",
          "httpVersion": "HTTP/1.1",
          "headers": [
            {
              "name": "Date",
              "value": "Mon, 02 Nov 2015 20:01:10 GMT"
            },
            {
              "name": "ETag",
              "value": "\"526ea5ef51ed11:0\""
            },
            {
              "name": "Last-Modified",
              "value": "Sat, 24 Oct 2015 11:48:44 GMT"
            },
            {
              "name": "Server",
              "value": "Microsoft-IIS/8.0"
            },
            {
              "name": "X-Powered-By",
              "value": "ASP.NET"
            },
            {
              "name": "Content-Type",
              "value": "image/png"
            },
            {
              "name": "Accept-Ranges",
              "value": "bytes"
            },
            {
              "name": "Content-Length",
              "value": "85816"
            }
          ],
          "cookies": [],
          "content": {
            "size": 85816,
            "mimeType": "image/png",
            "compression": 0
          },
          "redirectURL": "",
          "headersSize": 248,
          "bodySize": 85816,
          "_transferSize": 86064
        },
        "cache": {},
        "timings": {
          "blocked": 9.81800002045929,
          "dns": -1,
          "connect": -1,
          "send": 0.906999921426209,
          "wait": 276.84200019575655,
          "receive": 5.360999843105674,
          "ssl": -1
        },
        "connection": "403",
        "pageref": "page_3"
      },
      {
        "startedDateTime": "2015-11-02T20:00:36.466Z",
        "time": 216.02199994958937,
        "request": {
          "method": "GET",
          "url": "http://app.alea.gov/Community/WebResource.axd?d=pynGkmcFUV13He1Qd6_TZLI7rPI5HHU1xKNAcDuqHmOSqxhhHuJnUQRZoVg0QoxODvhDAFJwQZTcme5SJsVc4w2&t=635588402815688065",
          "httpVersion": "HTTP/1.1",
          "headers": [
            {
              "name": "Pragma",
              "value": "no-cache"
            },
            {
              "name": "Accept-Encoding",
              "value": "gzip, deflate, sdch"
            },
            {
              "name": "Host",
              "value": "app.alea.gov"
            },
            {
              "name": "Accept-Language",
              "value": "en-US,en;q=0.8"
            },
            {
              "name": "User-Agent",
              "value": "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36"
            },
            {
              "name": "Accept",
              "value": "*/*"
            },
            {
              "name": "Referer",
              "value": "http://app.alea.gov/Community/wfSexOffenderSearch.aspx"
            },
            {
              "name": "Connection",
              "value": "keep-alive"
            },
            {
              "name": "Cache-Control",
              "value": "no-cache"
            }
          ],
          "queryString": [
            {
              "name": "d",
              "value": "pynGkmcFUV13He1Qd6_TZLI7rPI5HHU1xKNAcDuqHmOSqxhhHuJnUQRZoVg0QoxODvhDAFJwQZTcme5SJsVc4w2"
            },
            {
              "name": "t",
              "value": "635588402815688065"
            }
          ],
          "cookies": [],
          "headersSize": 517,
          "bodySize": 0
        },
        "response": {
          "status": 200,
          "statusText": "OK",
          "httpVersion": "HTTP/1.1",
          "headers": [
            {
              "name": "Date",
              "value": "Mon, 02 Nov 2015 20:01:10 GMT"
            },
            {
              "name": "Last-Modified",
              "value": "Fri, 06 Feb 2015 23:24:41 GMT"
            },
            {
              "name": "Server",
              "value": "Microsoft-IIS/8.0"
            },
            {
              "name": "X-AspNet-Version",
              "value": "4.0.30319"
            },
            {
              "name": "X-Powered-By",
              "value": "ASP.NET"
            },
            {
              "name": "Content-Type",
              "value": "application/x-javascript"
            },
            {
              "name": "Cache-Control",
              "value": "public"
            },
            {
              "name": "Content-Length",
              "value": "22346"
            },
            {
              "name": "Expires",
              "value": "Tue, 01 Nov 2016 16:53:18 GMT"
            }
          ],
          "cookies": [],
          "content": {
            "size": 22346,
            "mimeType": "application/x-javascript",
            "compression": 0
          },
          "redirectURL": "",
          "headersSize": 307,
          "bodySize": 22346,
          "_transferSize": 22653
        },
        "cache": {},
        "timings": {
          "blocked": 3.37099982425571,
          "dns": -1,
          "connect": -1,
          "send": 0.9840000420808694,
          "wait": 180.08600012399242,
          "receive": 31.580999959260367,
          "ssl": -1
        },
        "connection": "206",
        "pageref": "page_3"
      }
    ]
  }
}

我的代码

var baseAddress1 = new Uri("http://app.alea.gov/Community/wfSexOffenderSearch.aspx");
        using (var client = new HttpClient() { BaseAddress = baseAddress1 })
        {
            var loaded = client.GetAsync(baseAddress1).Result;
            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(loaded.Content.ReadAsStringAsync().Result);
            var viewState = doc.DocumentNode.SelectSingleNode("//input[@id='__VIEWSTATEGENERATOR']");
            var eventValid = doc.DocumentNode.SelectSingleNode("//input[@id='__EVENTVALIDATION']");
            var viewStateid = doc.DocumentNode.SelectSingleNode("//input[@id='__VIEWSTATE']");
            string vsg = viewState.Attributes["value"].Value;
            string ev = eventValid.Attributes["value"].Value;
            string vsid = viewStateid.Attributes["value"].Value;
            var content = new FormUrlEncodedContent(new[]
               {
               new KeyValuePair<string, string>("__VIEWSTATE", vsid),
               new KeyValuePair<string, string>("__VIEWSTATEGENERATOR", vsg),
               new KeyValuePair<string, string>("__EVENTVALIDATION", ev),
               new KeyValuePair<string, string>("ctl00%24plhBodyArea%24btnAccept", "Accept")
             });

            Console.WriteLine(content.ReadAsStringAsync().Result);
            var result = client.PostAsync("http://app.alea.gov/Community/wfSexOffenderSearch.aspx", content).Result;
            result.EnsureSuccessStatusCode();
            File.WriteAllText("result.html", result.Content.ReadAsStringAsync().Result);
        }

所以是的,我发送了帖子回复所需的大部分信息,但结果页面不是下一页,但仍然是相同的目标网页。

我在这里错过了什么?没有与我的代码执行相关的错误,只是结果不像预期的那样。没有任何cookie丢失或任何其他不可能。

1 个答案:

答案 0 :(得分:0)

ctl00%24plhBodyArea%24btnAccept

不是该字段的正确名称...实际上是$而是chrome默认将输出更改为%24抱歉没有注意到......