Created
November 6, 2014 06:36
-
-
Save guange2015/dd5c282fac23d8c371ff to your computer and use it in GitHub Desktop.
c#登录知乎
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Data; | |
using System.Drawing; | |
using System.Linq; | |
using System.Text; | |
using System.Windows.Forms; | |
using System.Net; | |
using System.IO; | |
using System.Text.RegularExpressions; | |
namespace FakeWeb | |
{ | |
public partial class Form1 : Form | |
{ | |
public Form1() | |
{ | |
InitializeComponent(); | |
} | |
public static CookieContainer CookieContainer { get; set; } | |
private void button1_Click(object sender, EventArgs e) | |
{ | |
new Action(delegate() | |
{ | |
login(); | |
getMain(); | |
getDraft(); | |
}).BeginInvoke(null, null); | |
} | |
string getXSRF() | |
{ | |
string retString = HttpGet("http://www.zhihu.com"); ; | |
string xsrf = null; | |
Regex regex = new Regex("<input type=\"hidden\" name=\"_xsrf\" value=\"(?<xsrf>.+?)\"/>"); | |
Match mc = regex.Match(retString); | |
if (mc != null) | |
{ | |
xsrf = mc.Groups["xsrf"].Value; | |
} | |
return xsrf; | |
} | |
bool login() | |
{ | |
string xsrf = getXSRF(); | |
if (xsrf != null) | |
{ | |
string postData = string.Format("_xsrf={0}&email=test%40gmail.com&password=123456", | |
xsrf); | |
byte[] postDataBytes = Encoding.UTF8.GetBytes(postData); | |
HttpWebRequest request = WebRequest.Create("http://www.zhihu.com/login") as HttpWebRequest; | |
request.Method = "POST"; | |
request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8"; | |
request.ContentLength = postDataBytes.Length; | |
request.Headers.Add("x-requested-with", "XMLHttpRequest"); | |
request.AllowAutoRedirect = false; | |
if (CookieContainer == null) | |
{ | |
CookieContainer = new CookieContainer(); | |
} | |
request.CookieContainer = CookieContainer; | |
Stream wStream = request.GetRequestStream(); | |
wStream.Write(postDataBytes, 0, postDataBytes.Length); | |
wStream.Close(); | |
HttpWebResponse response = request.GetResponse() as HttpWebResponse; | |
Stream stream = response.GetResponseStream(); | |
StreamReader rs = new StreamReader(stream, Encoding.UTF8); | |
string retString = rs.ReadToEnd(); | |
rs.Close(); | |
stream.Close(); | |
} | |
return false; | |
} | |
string getMain() | |
{ | |
return HttpGet("http://www.zhihu.com"); | |
} | |
string getDraft() | |
{ | |
return HttpGet("http://www.zhihu.com/draft"); | |
} | |
private string HttpGet(string url) | |
{ | |
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; | |
request.Method = "GET"; | |
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; | |
request.CookieContainer = CookieContainer; | |
HttpWebResponse response = request.GetResponse() as HttpWebResponse; | |
Stream stream = response.GetResponseStream(); | |
StreamReader rs = new StreamReader(stream, Encoding.GetEncoding("utf-8")); | |
string retString = rs.ReadToEnd(); | |
rs.Close(); | |
stream.Close(); | |
return retString; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment