登录到PureVolume.com编程! [英] logged in to PureVolume.com programatically !
问题描述
我已经试了很多方法,但未能登录purevolume.com编程!
I have tried so many ways but failed to logged in purevolume.com programatically!!!
登录网址: http://www.purevolume.com/login
有谁能够给它一个尝试,并提供样品code来实现这一目标。
can anybody give it a try and provide sample code to achieve this.
推荐答案
这样的事情应该工作...你需要做一个HTTP POST请求,并发送你的用户名和密码。为了保持登录你需要一个cookie的容器,你然后再用所有的其他请求。
Something like this should work... You need to do an http post request and send your username and password. To remain "logged in" you need a cookie container that you then reuse for all other requests.
编辑:更新code被测试。需要最初的GET请求/登录有做POST登录前的会话cookie。我还添加了一些HTTP头,使其等同于从浏览器中的正常的要求。
Updated code that is tested. An initial GET request to /login was needed to have the session cookie before doing the POST login. I've also added some HTTP headers to make it identical to a "normal" request from a browser.
void Main()
{
//We need a container to store the cookies in.
CookieContainer cookies = new CookieContainer();
//Request login page to get a session cookie
GETHtml("http://www.purevolume.com/login", cookies);
//Now we can do login
Login("some-user-name", "some-password", cookies);
}
public bool Login(string Username, string Password, CookieContainer cookies)
{
string poststring = string.Format("username={0}&password={1}&user_login_button.x=63&user_login_button.y=13&user_login_button=login",
Username, Password);
byte[] postdata = Encoding.UTF8.GetBytes(poststring);
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://www.purevolume.com/login");
webRequest.CookieContainer = cookies;
webRequest.Method = "POST";
webRequest.Referer = "http://www.purevolume.com/login";
webRequest.Headers.Add("origin", "http://www.purevolume.com");
webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.5.21022;";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.ContentLength = postdata.Length;
using (Stream writer = webRequest.GetRequestStream())
writer.Write(postdata, 0, postdata.Length);
using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
{
//We need to add any response cookies to our cookie container
cookies.Add(webResponse.Cookies);
//Only for debug
using (var stream = new StreamReader(webResponse.GetResponseStream()))
System.Diagnostics.Debug.WriteLine(stream.ReadToEnd());
return (webResponse.StatusCode == HttpStatusCode.OK);
}
}
public string GETHtml(string url, CookieContainer cookies)
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.CookieContainer = cookies;
webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.5.21022;";
webRequest.Referer = "http://www.purevolume.com/login";
using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
{
//We need to add any response cookies to our cookie container
cookies.Add(webResponse.Cookies);
using (var stream = new StreamReader(webResponse.GetResponseStream()))
return stream.ReadToEnd();
}
}
这篇关于登录到PureVolume.com编程!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!