HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://pinterest.com/login/?next=%2F"); request.Method = "GET"; request.CookieContainer = cc; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); string encoding = response.ContentEncoding; if (encoding == null || encoding.Length < 1) { encoding = "UTF-8"; // 默认编码 } StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding)); string data = reader.ReadToEnd(); string token= RegexMatch(data,@"(?<=csrfmiddlewaretoken' value=')([\s\S]*?)(?=')"); string ch =RegexMatch(data,@"(?<=_ch' value=')([\s\S]*?)(?=')"); response.Close(); request = (HttpWebRequest)WebRequest.Create("https://pinterest.com/login/?next=%2Flogin%2F"); ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); request.Method = "POST"; request.AllowAutoRedirect = true; request.PreAuthenticate = true; request.Credentials = CredentialCache.DefaultCredentials; request.Accept = "image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"; request.UserAgent = "Mozilla/4.0 (compatible; MSIE 9.0; qdesk 2.5.1270.201; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; Alexa Toolbar)"; request.Referer = "https://pinterest.com/"; request.ContentType = "application/x-www-form-urlencoded"; request.CookieContainer = cc; data = String.Format("email={0}&password={1}&next=%2F&csrfmiddlewaretoken={2}&_ch={3}", textBox2.Text, textBox3.Text,token,ch); request.ContentLength = data.Length; StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.ASCII); writer.Write(data); writer.Flush(); response = (HttpWebResponse)request.GetResponse();
我 想用HTTPWEBREQUEST方式登录pinterest,可是用HTTPS方式返回的数据总是未登录 求解 谷歌百度想了一下午了
将HttpWebResquest实例的ProtocolVersion属性设置为HttpVersion.Version10试试呢
.net版本问题 换成.net 4.0 就解决了