无法使用HTTP POST从安全网站下载文件

时间:2019-01-19 16:39:57

标签: c# http networking webbrowser-control

我正在尝试使用c#中的https通过Web服务器从Web服务器下载pdf文件,并获得HTTP OK响应,但是文件流没有响应。我在做什么错了?

这是我后面的代码:

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
    ServicePointManager.ServerCertificateValidationCallback = (s, cert, ch, sec) => { return true; };


    HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create("https://services.dps.ohio.gov/CrashOnline/CrashRetrieval/OhioCrashReportRetrieval/GetReport");
    myRequest.Method = "POST";
    myRequest.CookieContainer = cookies;
    myRequest.ContentType = "application/x-www-form-urlencoded";
    myRequest.Accept = myRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
    myRequest.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate, br");
    myRequest.Headers.Add(HttpRequestHeader.AcceptLanguage, "en-US,en;q=0.9");
    myRequest.Referer = "https://services.dps.ohio.gov/CrashOnline/CrashRetrieval/OhioCrashReportRetrieval/Search";
    myRequest.UserAgent = "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36";

    byte[] bytes = Encoding.Default.GetBytes(content);
    myRequest.ContentLength = bytes.Length;
    Stream dataStream = myRequest.GetRequestStream();        
    dataStream.Write(bytes, 0, bytes.Length);          
    dataStream.Close();



    byte[] result = null;
    byte[] buffer = new byte[2048];

    using (HttpWebResponse response = (HttpWebResponse)myRequest.GetResponse())

从chrome下载文件工作正常,这是来自浏览器的提琴手RAW http数据包:

POST https://services.dps.ohio.gov/CrashOnline/CrashRetrieval/OhioCrashReportRetrieval/GetReport HTTP/1.1
Host: services.dps.ohio.gov
Connection: keep-alive
Content-Length: 177
Cache-Control: max-age=0
Origin: https://services.dps.ohio.gov
Upgrade-Insecure-Requests: 1
Content-Type: application/x-www-form-urlencoded
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Referer: https://services.dps.ohio.gov/CrashOnline/CrashRetrieval/OhioCrashReportRetrieval/Search
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Cookie: ASP.NET_SessionId=t1nddo51g0jotl51cqev15rp; __RequestVerificationToken_L0NyYXNoT25saW5l0=iMy0lJ3OviGs0iXame8ebp8IdS6K9wpc8tFinYd0nhNnySTLlii-yHYU5tDJjv49Fqi29M_31mPiPdKWWtJJPWSmp4qGGvna9lW2rkDHUAs1

__RequestVerificationToken=eTJN31araRPeB-bhQaeU3awBDsCwR9BpkIYNqpsakTPRT1w1nXtbGFcfVRZS776ZW0NGv1aKahx3WJv5PNQ199iM7GF9Y9f7ws5KTG4gI3I1&id=NjgzMTIwOQ%3D%3D&btn_NjgzMTIwOQ%3D%3D=

这是我的应用程序的http数据包:

POST https://services.dps.ohio.gov/CrashOnline/CrashRetrieval/OhioCrashReportRetrieval/GetReport HTTP/1.1
Accept: text/html, application/xhtml+xml, image/jxr, */*
Referer: https://services.dps.ohio.gov/CrashOnline/CrashRetrieval/OhioCrashReportRetrieval/Search
Accept-Language: en-GB,es-ES;q=0.8,es;q=0.6,en-US;q=0.4,en;q=0.2
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Host: services.dps.ohio.gov
Content-Length: 177
Connection: Keep-Alive
Cache-Control: no-cache

__RequestVerificationToken=eTJN31araRPeB-bhQaeU3awBDsCwR9BpkIYNqpsakTPRT1w1nXtbGFcfVRZS776ZW0NGv1aKahx3WJv5PNQ199iM7GF9Y9f7ws5KTG4gI3I1&id=NjgzMTIwOQ%3D%3D&btn_NjgzMTIwOQ%3D%3D=

0 个答案:

没有答案