|
https页面,POST或者直接GET页面都会报错 :基础连接已经关闭;发送时发生错误. 纠结了很多天了也搞不定,我怀疑是证书的问题,但是证书我已经处理过了,可以帮我定位问题出在哪里吗 我的代码如下
public CookieContainer _Cookie = new CookieContainer();
public string LoginWeb(string PostData)
{
string str = string.Empty;
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("https://secure.runescape.com/m=email-register/forgotLogin");
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
req.AllowAutoRedirect = true;
req.Timeout = 60 * 1000; // req的超时按毫秒计算
req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; //接受任意文件
req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36";
req.ContentType = "application/x-www-form-urlencoded";
req.KeepAlive = false;
req.CookieContainer = _Cookie;
if (!string.IsNullOrEmpty(PostData))
{
req.Method = "POST";
byte[] b = Encoding.Default.GetBytes(PostData);
req.ContentLength = b.Length;
System.IO.Stream sw = null;
try
{
sw = req.GetRequestStream();
sw.Write(b, 0, b.Length);
}
catch (System.Exception ex)
{
if (!(ex is System.Threading.ThreadAbortException)) // 如果不是线程的中止异常,则显示给用户
return ex.Message;
}
finally
{
if (sw != null)
{
try
{
sw.Close();
}
catch
{
}
}
}
}
HttpWebResponse rep = null;
System.IO.StreamReader sr = null;
try
{
req.Method = "GET";
rep = (HttpWebResponse)req.GetResponse();
sr = new System.IO.StreamReader(rep.GetResponseStream(), Encoding.UTF8);
str = sr.ReadToEnd();
if (sr != null) sr.Close();
}
catch (Exception e)
{
if (!(e is System.Threading.ThreadAbortException)) // 如果不是线程的中止异常,则显示给用户
return e.Message;
}
return str;
}
POST,GET调用
string PostData = "email=dsfgerthdfgh@qq.com&submit=Recover";
string loginpage = LoginWeb(string.Empty);
|
|