.net 模拟登陆 post https 请求跳转页面 - 拾不起的回忆 - 博客园

来源: .net 模拟登陆 post https 请求跳转页面 – 拾不起的回忆 – 博客园

AllowAutoRedirect property is true, the Referer property is set automatically when the request is redirected to another site.”>如果 AllowAutoRedirect 属性为 true,则 Referer 属性在请求被重定向到另一个站点时自动设置。

Referer HTTP header, set the Referer property to null.”>若要清除 RefererHTTP 标头,请将 Referer 属性设置为 null。
如果设置了  Referer 则 指定到某个站点. 被这东西小坑了一下 涨姿势。


private bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
return true; //总是接受安全证书
}

private void test()
{
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
CookieCollection _cookies = null; //后续使用
var addRess = "https://xxxxxxxxxx";
var data = "account=xxxxxxx&password=xxxxx";

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(addRess);
request.CookieContainer = new CookieContainer();
request.Timeout = 1000 * 60;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.AllowAutoRedirect = true;
request.Referer = addRess;

byte[] bs = Encoding.UTF8.GetBytes(data);
HttpWebResponse response = null;
StreamReader myStreamReader = null;
Stream myRequestStream = null;
request.ContentLength = bs.Length;
string retString = string.Empty;
try
{
using (myRequestStream = request.GetRequestStream())
{
myRequestStream.Write(bs, 0, bs.Length);
myRequestStream.Close();
}
response = (HttpWebResponse)request.GetResponse();
_cookies = response.Cookies;
using (Stream myResponseStream = response.GetResponseStream())
{
using (myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("BIG5")))
{
retString = myStreamReader.ReadToEnd();
}
}
}
catch (Exception)
{

throw;
}
finally
{
if (myRequestStream != null)
{
myRequestStream.Close();
}
if (response != null)
{
response.Close();
}
if (myStreamReader != null)
{
myStreamReader.Close();
}
}
}
赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏