Java Orkut 登录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1525697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Orkut Login
提问by Yatendra Goel
I want the pagesource of home page of ORKUT (http://www.ORKUT.com) in java.
我想要Java中 ORKUT ( http://www.ORKUT.com)主页的页面源。
But it needs to be logged in to the ORKUT before accessing any page of it. How can I do it. It should not involve browser in between
但它需要先登录到 ORKUT,然后才能访问它的任何页面。我该怎么做。它不应该涉及浏览器之间
采纳答案by Daff
You should have a look at the Commons HTTP Client. With it you can send a POST request with your login data and then use the session ID for further processing.
您应该看看Commons HTTP Client。有了它,您可以发送带有登录数据的 POST 请求,然后使用会话 ID 进行进一步处理。
回答by TraderJoeChicago
Two ways of doing that:
这样做的两种方法:
1) Buy Octazen that will do that for you and keep the library updated every time Orkut changes something.
1) 购买 Octazen 会为您做到这一点,并在 Orkut 每次更改某些内容时保持库更新。
2) Use watir to hiHyman the browser.
2)使用watir劫持浏览器。
Doing with HTTP Client is like fixing a watch with boxing gloves under the water. It does not support JS, you have to work your way through the cookies, parsing, etc.
使用 HTTP 客户端就像在水下用拳击手套修理手表一样。它不支持 JS,你必须自己处理 cookie、解析等。
回答by Rubens Farias
If you don't mind to read C# code:
如果您不介意阅读 C# 代码:
string orkutSite = "http://www.orkut.com/Login.aspx"; // enter correct address
string formPage = "";
string afterLoginPage = "";
// Get postback data and cookies
CookieContainer cookies = new CookieContainer();
HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(orkutSite);
getRequest.CookieContainer = cookies;
getRequest.Method = "GET";
HttpWebResponse form = (HttpWebResponse)getRequest.GetResponse();
using (StreamReader response =
new StreamReader(form.GetResponseStream(), Encoding.UTF8))
{
formPage = response.ReadToEnd();
}
Dictionary<string, string> inputs = new Dictionary<string,string>();
inputs.Add("__EVENTTARGET", "");
inputs.Add("__EVENTARGUMENT", "");
foreach (Match input in
Regex.Matches(formPage,
@"<input.*?name=""(?<name>.*?)"".*?(?:value=""(?<value>.*?)"".*?)? />",
RegexOptions.IgnoreCase | RegexOptions.ECMAScript))
{
inputs.Add(input.Groups["name"].Value, input.Groups["value"].Value);
}
inputs["username"] = "xxxxx"; // *please*, check for \
inputs["password"] = "yyyyy"; // correct field names \
byte[] buffer =
Encoding.UTF8.GetBytes(
String.Join("&",
Array.ConvertAll<KeyValuePair<string, string>, string>(
inputs.ToArray(),
delegate(KeyValuePair item)
{
return item.Key + "=" + HttpUtility.UrlEncode(item.Value);
})));
HttpWebRequest postRequest = (HttpWebRequest)WebRequest.Create(orkutSite);
postRequest.CookieContainer = cookies;
postRequest.Method = "POST";
postRequest.ContentType = "application/x-www-form-urlencoded";
// send username/password
using (Stream stream = postRequest.GetRequestStream())
{
stream.Write(buffer, 0, buffer.Length);
}
// get response from login page
using (StreamReader reader = new StreamReader(
postRequest.GetResponse().GetResponseStream(), Encoding.UTF8))
{
afterLoginPage = reader.ReadToEnd();
}