我想登录到特定网站,但如果要这样做,我需要从网上获取一些隐藏数据。在所有下一个部分中,它不同,所以我必须保持sesion活着我想。 要检查我是否已登录,我想转到此特定网站的另一个页面(您必须登录才能看到它)
好的,我收到回复,我的信息为LOGIN,HIDDEN_NAME和HIDDEN_VALUE为2 然后我想通过邮寄方式登录表格,传递和登录所需的数据。
这就是我这样做的方式,它不起作用:
import org.apache.http.*;
import org.apache.http.client.CookieStore;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.DefaultRedirectStrategy;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/**
* A example that demonstrates how HttpClient APIs can be used to perform
* form-based logon.
*/
public class webparser
{
static String EMAIL;
static String PASS;
static String LOGIN = null;
static String HIDDEN_NAME[] = new String[2];
static String HIDDEN_VALUE[] = new String[2];
//static ClientConnectionManager connManager = new PoolingClientConnectionManager();
//static DefaultHttpClient httpclient = new DefaultHttpClient(connManager);
static DefaultHttpClient httpclient = new DefaultHttpClient();
static HttpResponse response;
static HttpEntity entity;
static List<Cookie> cookies;
static CookieStore cookieStore = new BasicCookieStore();
static HttpContext httpContext = new BasicHttpContext();
public static void main(String[] args) throws Exception {
clientFormLogin();
}
public static void clientFormLogin() throws Exception {
httpclient.setRedirectStrategy(new DefaultRedirectStrategy() {
public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) {
boolean isRedirect=false;
try {
isRedirect = super.isRedirected(request, response, context);
} catch (ProtocolException e) {
e.printStackTrace();
}
if (!isRedirect) {
int responseCode = response.getStatusLine().getStatusCode();
if (responseCode == 301 || responseCode == 302) {
return true;
}
}
return false;
}
});
httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
getLoginData(httpGet("https://bannersbroker.com/user/login"));
setLoginData(null, null);
displayResponse(httpPostLogin("https://bannersbroker.com/user/login?event=doLogin"));
//displayResponse(httpGet("http://bannersbroker.com/adpubcombo_dashboard/dashboard"));
displayResponse(httpGet("https://bannersbroker.com/adpubcombo_ewallet/ewallet"));
httpclient.getConnectionManager().shutdown();
}
public static void setLoginData(String EMAIL, String PASS) {
if (EMAIL != null)
webparser.EMAIL = EMAIL;
else
webparser.EMAIL = "my@email.com";
if (PASS != null)
webparser.PASS = PASS;
else
webparser.PASS = "my_pass";
}
public static String httpPostLogin(String postHttp) {
String responseBody = null;
try {
HttpPost httpost = new HttpPost(postHttp +
"&email=" + EMAIL +
"&pass=" + PASS + "&"+
LOGIN + "=Login&" +
HIDDEN_NAME[0] + "=" + HIDDEN_VALUE[0] + "&"+
HIDDEN_NAME[1] + "=" + HIDDEN_VALUE[1]);
System.out.println("executing request " + httpost.getURI());
ResponseHandler<String> responseHandler = new BasicResponseHandler();
responseBody = httpclient.execute(httpost, responseHandler);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("email", EMAIL));
nvps.add(new BasicNameValuePair("pass", PASS));
nvps.add(new BasicNameValuePair(LOGIN, "Login"));
nvps.add(new BasicNameValuePair(HIDDEN_NAME[0], HIDDEN_VALUE[0]));
nvps.add(new BasicNameValuePair(HIDDEN_NAME[1], HIDDEN_VALUE[1]));
httpost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
response = httpclient.execute(httpost, httpContext);
entity = response.getEntity();
System.out.println("Login form get: " + response.getStatusLine());
//DisplayPage(entity.getContent());
EntityUtils.consume(entity);
System.out.println("Post logon cookies:");
cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
System.out.println("None");
} else {
for (int i = 0; i < cookies.size(); i++) {
System.out.println("- " + cookies.get(i).toString());
}
}
} catch (IOException e) {
e.printStackTrace();
}
return responseBody;
}
public static String httpGet(String urlAddres) {
String responseBody = null;
try {
HttpGet httpget = new HttpGet(urlAddres);
System.out.println("executing request " + httpget.getURI());
ResponseHandler<String> responseHandler = new BasicResponseHandler();
responseBody = httpclient.execute(httpget, responseHandler);
response = httpclient.execute(httpget, httpContext);
entity = response.getEntity();
System.out.println("Login form get: " + response.getStatusLine());
//DisplayPage(entity.getContent());
EntityUtils.consume(entity);
System.out.println("Initial set of cookies:");
cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
System.out.println("None");
} else {
for (int i = 0; i < cookies.size(); i++) {
System.out.println("- " + cookies.get(i).toString());
}
}
} catch (IOException e) {
e.printStackTrace();
}
return responseBody;
}
public static void DisplayPage(InputStream is) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String str = "";
while ((str = br.readLine()) != null) {
System.out.println("" + str);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void displayResponse(String responseBody) {
System.out.println("----------------------------------------");
System.out.println(responseBody);
System.out.println("----------------------------------------");
}
public static void getLoginData(String responseBody) {
int length, temp, counter = 0;
length = responseBody.length();
for (int i = 0; i < length; i++) {
if (responseBody.startsWith("hidden", i)) {
temp = i;
HIDDEN_NAME[counter] = responseBody.substring(temp + 13, temp + 81);
HIDDEN_VALUE[counter] = responseBody.substring(temp + 90, temp + 123);
System.out.println("HIDDEN_NAME[" + counter + "] = " + HIDDEN_NAME[counter]);
System.out.println("HIDDEN_VALUE[" + counter + "] = " + HIDDEN_VALUE[counter]);
counter++;
}
}
for (int i = 0; i < length; i++) {
if (responseBody.startsWith("document.write", i)) {
temp = i;
LOGIN = responseBody.substring(temp + 41, temp + 144);
System.out.println("LOGIN = " + LOGIN);
}
}
}
}
以下是我的控制台的样子:
"C:\Program Files\Java\jdk1.6.0_39\bin\java" -Didea.launcher.port=7532 "-Didea.launcher.bin.path=E:\Program Files (x86)\JetBrains\IntelliJ IDEA 12.0.4\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.6.0_39\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.6.0_39\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.6.0_39\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.6.0_39\jre\lib\jce.jar;C:\Program Files\Java\jdk1.6.0_39\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.6.0_39\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.6.0_39\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.6.0_39\jre\lib\resources.jar;C:\Program Files\Java\jdk1.6.0_39\jre\lib\rt.jar;C:\Program Files\Java\jdk1.6.0_39\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.6.0_39\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.6.0_39\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.6.0_39\jre\lib\ext\sunmscapi.jar;E:\solsoft\BannersBrokerStatistic\target\classes;C:\Users\Rafał\.m2\repository\org\jibx\schema\net\java\org.jibx.schema.net.java.jnlp_6_0\1.0.8\org.jibx.schema.net.java.jnlp_6_0-1.0.8.jar;C:\Users\Rafał\.m2\repository\org\jibx\jibx-run\1.2.4.5\jibx-run-1.2.4.5.jar;C:\Users\Rafał\.m2\repository\joda-time\joda-time\2.1\joda-time-2.1.jar;C:\Users\Rafał\.m2\repository\xpp3\xpp3\1.1.3.4.O\xpp3-1.1.3.4.O.jar;C:\Users\Rafał\.m2\repository\org\jibx\jibx-extras\1.2.4.5\jibx-extras-1.2.4.5.jar;C:\Users\Rafał\.m2\repository\dom4j\dom4j\1.6.1\dom4j-1.6.1.jar;C:\Users\Rafał\.m2\repository\xml-apis\xml-apis\1.0.b2\xml-apis-1.0.b2.jar;C:\Users\Rafał\.m2\repository\org\jdom\jdom\1.1.3\jdom-1.1.3.jar;C:\Users\Rafał\.m2\repository\org\apache\httpcomponents\httpclient\4.3-alpha1\httpclient-4.3-alpha1.jar;C:\Users\Rafał\.m2\repository\org\apache\httpcomponents\httpcore\4.3-alpha1\httpcore-4.3-alpha1.jar;C:\Users\Rafał\.m2\repository\commons-logging\commons-logging\1.1.1\commons-logging-1.1.1.jar;C:\Users\Rafał\.m2\repository\commons-codec\commons-codec\1.6\commons-codec-1.6.jar;E:\Program Files (x86)\JetBrains\IntelliJ IDEA 12.0.4\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain webparser
executing request https://bannersbroker.com/user/login
Login form get: HTTP/1.1 200 OK
Initial set of cookies:
- [version: 0][name: PHPSESSID][value: kcbjr1br4fmqksb038663eenn3][domain: bannersbroker.com][path: /][expiry: null]
- [version: 0][name: __ct][value: 42d5a032bbbc0fc21d17da8f9ef0a6b7][domain: bannersbroker.com][path: /][expiry: Tue Mar 12 21:52:53 CET 2013]
HIDDEN_NAME[0] = _f6c043c1a1d767f2d447a43c32c878e9[_6911a65a36fbc4ec93ccccf911ead848]
HIDDEN_VALUE[0] = _fb664a024bce8eb09e9c63f41a60287c
HIDDEN_NAME[1] = _f6c043c1a1d767f2d447a43c32c878e9[_fda5de586c4828b18cb01983cc8e2961]
HIDDEN_VALUE[1] = _33b0402e97f2be1aca4e672dc3c23384
LOGIN = _f6c043c1a1d767f2d447a43c32c878e9[_0a1012405121479093b7fa787480e330][_03cede135af4bcc837cf48b43265795d]
Login form get: HTTP/1.1 200 OK
Post logon cookies:
- [version: 0][name: PHPSESSID][value: kcbjr1br4fmqksb038663eenn3][domain: bannersbroker.com][path: /][expiry: null]
- [version: 0][name: __ct][value: 79fa3d8c1e5d3d823c9740c0894d479f][domain: bannersbroker.com][path: /][expiry: Tue Mar 12 21:52:53 CET 2013]
----------------------------------------
<!DOCTYPE html>
it show source of https://bannersbroker.com/user/login
so wrong becous it should redictered to http://bannersbroker.com/adpubcombo_dashboard/dashboard
</html>
----------------------------------------
executing request https://bannersbroker.com/adpubcombo_ewallet/ewallet
----------------------------------------
null
----------------------------------------
org.apache.http.client.HttpResponseException: Moved Permanently
at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:67)
at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:53)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:134)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:116)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:109)
at webparser.httpGet(webparser.java:145)
at webparser.clientFormLogin(webparser.java:77)
at webparser.main(webparser.java:48)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
怎么了?为什么不工作?
答案 0 :(得分:0)
我使用Jsoup
而不是HttpClient API我创建了获取登录数据的方法
public Connection.Response getLoginData() {
Connection.Response res = null;
try {
String html;
int length, counter;
res = Jsoup
.connect("https://bannersbroker.com//user/login?event=doLogin")
.execute();
html = res.parse().toString();
length = html.length();
counter = 0;
for (int i = 0; i < length; i++) {
if (html.startsWith("document.write", i)) {
name[counter] = html.substring(i + 41, i + 144);
value[counter] = "Login";
counter++;
}
if (html.startsWith("hidden", i)) {
name[counter] = html.substring(i + 13, i + 81);
value[counter] = html.substring(i + 90, i + 123);
counter++;
}
}
} catch (IOException ioe) {
} catch (NullPointerException ioe) {
JOptionPane.showMessageDialog(RunFrame.getInstance(),
"Could not open connection to Banners Broker!",
"Error",
JOptionPane.ERROR_MESSAGE);
}
return res;
}
然后我用其他方法登录
public Connection.Response doLogin(Connection.Response res, String email, String pass) {
Connection.Response res2 = null;
try {
res2 = Jsoup
.connect("https://bannersbroker.com//user/login?event=doLogin")
.cookies(res.cookies())
.data("email", email)
.data("pass", pass)
.data(name[0], value[0])
.data(name[1], value[1])
.data(name[2], value[2])
.method(Connection.Method.POST)
.execute();
} catch (IOException ioe) {
} catch (IllegalArgumentException ioe) {
JOptionPane.showMessageDialog(RunFrame.getInstance(),
"Could not open connection to Banners Broker!",
"Error",
JOptionPane.ERROR_MESSAGE);
} catch (NullPointerException ioe) {
JOptionPane.showMessageDialog(RunFrame.getInstance(),
"Could not open connection to Banners Broker!",
"Error",
JOptionPane.ERROR_MESSAGE);
}
return res2;
}
我在我的类的construcxtor中调用方法,并从一个响应发送到另一个。
当我登录时,我处理来自方法doLogin()
的重新定位
这是我如何解析数据
doc = Jsoup
.connect(pageAddres)
.cookie("PHPSESSID", res.cookie("PHPSESSID"))
.get();