基本上我在下面有这段代码,最后一步 FinalStepGetReportData() 在返回时抛出异常 (HttpWebResponse)request.GetResponse(); 如果提琴手被关闭?
除了最后一步之外,当 Fiddler 关闭时,所有其他操作都有效。当我在最后一步打开 Fiddler 时有效吗?
有任何想法吗?我正用头撞墙。
谢谢
public class TrustAllCertificatePolicy : System.Net.ICertificatePolicy
{
public TrustAllCertificatePolicy() { }
public bool CheckValidationResult(ServicePoint sp,
X509Certificate cert,
WebRequest req,
int problem)
{
return true;
}
}
public void Download()
{
string cookiesInRawFormat = null;
NameValueCollection headers = null;
HttpWebResponse response = null;
request = (HttpWebRequest)HttpWebRequest.Create(Config.ReportUrl);
System.Net.ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy();
request.Accept = "image/gif, image/jpeg, image/pjpeg, application/x-ms-application, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-ms-xbap, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
request.KeepAlive = true;
request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10 (.NET CLR 3.5.30729)";
headers = new NameValueCollection()
{
{ "Accept-Language", "en-us,en;q=0.5" }
};
request.Headers.Add(headers);
request.CookieContainer = cookies;
response = (HttpWebResponse) request.GetResponse();
cookiesInRawFormat = response.Headers["Set-Cookie"];
if (!String.IsNullOrEmpty(cookiesInRawFormat))
{
ParseCookiesFromResponseHeader(cookiesInRawFormat);
}
string location = response.ResponseUri.AbsolutePath;
using (response = SignIn(location))
{
using (Stream dataStream = FinalStepGetReportData(response, location).GetResponseStream())
{
// Open the stream using a StreamReader for easy access.
using (StreamReader reader = new StreamReader(dataStream))//, Encoding.UTF8))
{
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
Console.WriteLine(responseFromServer);
}
}
}
}
private HttpWebResponse SignIn(string referrerLocation)
{
request = (HttpWebRequest)HttpWebRequest.Create(Config.SignInUrl);
request.Accept = "image/gif, image/jpeg, image/pjpeg, application/x-ms-application, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-ms-xbap, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
request.KeepAlive = true;
request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10 (.NET CLR 3.5.30729)";
request.ContentType = "application/x-www-form-urlencoded";
request.Referer = referrerLocation;
System.Net.ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy();
NameValueCollection headers = new NameValueCollection()
{
{ "Accept-Language", "en-us,en;q=0.5" },
{ "Cache-Control", "no-cache" }
};
request.Method = HttpMethod.POST.ToString();
request.Headers.Add(headers);
request.CookieContainer = cookies;
request.AllowAutoRedirect = false;
PostDataBuilder builder = new PostDataBuilder() {
{ "action", "sign-in" },
{ "disableCorpSignUp", String.Empty },
{ "email", Config.UserName },
{ "metadata1", "Firefox 3.0.10 Windows" },
{ "metadata2", "Mozilla Default Plug-in Java(TM) Platform SE 6 U12 QuickTime Plug-in 7.6 Windows Genuine Advantage 19000912007 Microsoft Office system Shockwave Flash 10012iTunes Application Detector Silverlight Plug-In 20401150Windows Presentation Foundation RealPlayer(tm) G2 LiveConnect-Enabled Plug-In (32-bit) RealPlayer Version Plugin 601269Java(TM) Platform SE 6 U12 16012||1280-1024-971-32-*-*-*" },
{ "metadata3", "timezone: -2 execution time: 3" },
{ "metadataf1", String.Empty },
{ "mode", "1" },
{ "pageAction", "****THE HTML PAGE*****" },
{ "password", Config.Password },
{ "path", "****THE HTML PAGE*****" },
{ "protocol", "https" },
{ "query", String.Empty },
{ "redirectProtocol", String.Empty },
{ "useRedirectOnSuccess", "0" },
{ "x", "134" },
{ "y", "15" } };
byte[] postDataBytes = builder.Build(Encoding.UTF8);
request.ContentLength = postDataBytes.Length;
using (Stream stream = request.GetRequestStream())
{
stream.Write(postDataBytes, 0, postDataBytes.Length);
}
return (HttpWebResponse)request.GetResponse();
}
private HttpWebResponse FinalStepGetReportData(HttpWebResponse response, string referer)
{
request = (HttpWebRequest)HttpWebRequest.Create(response.Headers["Location"]);
request.Accept = "image/gif, image/jpeg, image/pjpeg, application/x-ms-application, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-ms-xbap, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10 (.NET CLR 3.5.30729)";
request.ContentType = "text/xml; encoding='utf-8'";
request.Method = HttpMethod.GET.ToString();
request.Referer = referer;
request.KeepAlive = true;
request.ProtocolVersion = HttpVersion.Version10;
request.Headers.Add(HttpRequestHeader.Cookie, response.GetResponseHeader("Set-Cookie"));
System.Net.ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy();
NameValueCollection headers = new NameValueCollection()
{
{"Accept-Language", "en-us,en;q=0.5" },
{"Cache-Control", "no-cache" },
{"Accept-Encoding", "deflate"},
};
request.Headers.Add(headers);
return (HttpWebResponse)request.GetResponse();
}